0

So basicly, I made a simple asp.net website which prints out the page when you click on the button. When you are printing out it asks me which printer and which options will be used. My aim is to directly print the page out without asking me printers and options.Thank you for your help.

Im running this program on visual studio 2015.

c# asp.net

function printpage() {

   var getpanel = document.getElementById("<%= Panel1.ClientID%>");
   var MainWindow = window.open('', '', 'height=500,width=800');
   MainWindow.document.write('<html><head><title>Print Page</title>');
   MainWindow.document.write('</head><body>');
   MainWindow.document.write(getpanel.innerHTML);
   MainWindow.document.write('</body></html>');
   MainWindow.document.close();

   setTimeout(function ()
   {
    MainWindow.print();
   }, 500);

   return false;
}

HTML (aspx)

<asp:Button ID="Button1" runat="server" OnClientClick="return printpage();"  Text="Print Page" />

I do not have any errors program is running clearly.

Mustapha Larhrouch
  • 3,373
  • 3
  • 14
  • 28
Aksel482
  • 46
  • 9
  • See this: https://stackoverflow.com/questions/17590896/auto-print-without-dialog – sr28 Jul 02 '19 at 14:47
  • 4
    You cannot control the printer from a webpage. It will always be up to the user. @sr28, that question is about winforms I think. – VDWWD Jul 02 '19 at 14:52
  • The above link is for user space .net applications not web pages. as @VDWWD stated you can't directly call the printer due to the browser sand boxing. – Matthew Whited Jul 02 '19 at 14:54
  • I suppose you _could_ work-around it by using an ActiveX control or Java applet with access to native platform APIs - assuming the browser won't block those too, of course. – Dai Jul 02 '19 at 15:05
  • 2
    Is there a particular reason you do not want your users to be able to decide which of their "printers" they may want to use - they may want to use something other than the default printer eg creating a PDF file with a suitable virtual printer driver, rather than hard copy. – PaulF Jul 02 '19 at 15:33

1 Answers1

2

This is not possible for the general case. It is blocked as a security measure, to keep your printer from collecting the same kind of junk that used to pollute fax machines.

The only work-arounds are via browser plugins (flash, silverlight, unity, java, etc), or if your web server happens to be on the same local network as the end-user's browser.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • it can be achievable by browser extension. printer properties controlled by user, so user need to accept it (security), then you can call it and open print window outside of screen dimensions and print. – Power Mouse Jul 02 '19 at 15:20
  • @PowerMouse the op wants to skip the print dialog. There's no way to do that with a stock web browser. – Joel Coehoorn Jul 02 '19 at 15:25
  • -i made it working in 2003, by using ActiveX plugin for browser. and open print window outside screen boundaries. set to landscape and send to printer. – Power Mouse Jul 02 '19 at 15:29
  • @PowerMouse ... which is why I included plugins as a potential work-around. But ActiveX is no longer an option. – Joel Coehoorn Jul 02 '19 at 15:37
  • agreed. beside technology it can be achieved if user have permissions to install plugins. so in kiosk mode it might not work. – Power Mouse Jul 02 '19 at 15:41