Introduction
This tip will show you how to send PDF files to a client printer without a dialog box in an ASP.NET application.
Background
A few days ago, one of our clients demanded that PDF reports generating from our web application should be sent to his printer directly without asking him to download those PDFs. So after
two days of searching, I found a breakthrough solution here. But, in this solution, there
is a select printer dialog box asking the user to press "OK" (but our client did not even want to click that single "OK") button, so I modified that solution to suppress the printer selection dialog box, and now when
the user presses the "Save and print" button in our application, the system saves the data into
the database, generates a report file (PDF) on the hard disk, and then sends that PDF
to the client's default printer without any further click.
Using the Code
So here is the trick. Place the following object
tab in your .aspx file:
<object id = "Object1" name="Pdf2"
type="application/pdf" WIDTH="1" HEIGHT="1" >
<PARAM NAME='SRC' VALUE='<%= SReportFileName %>'>
</object>
Here, SReportFileName
is a static
variable on my .cs file in which I will set my PDF file name (because PDF files are generated at run time, they have temporary names).
If you have a static PDF file, then you can write that name directly like this:
<object id = "Object1" name="Pdf2"
type="application/pdf" WIDTH="1" HEIGHT="1" >
<PARAM NAME='SRC' VALUE='myfile.pdf'>
</object>
Now in your CS file, after creating and saving your PDF file on the server, you can set
the SReportFileName
variable and call the <object> printAll()
method to do printing on
the printer.
public static string SReportFileName = "";
SReportFileName = "your file path and name"
ClientScript.RegisterStartupScript(typeof(Page), "MessagePopUp",
"<script language="'JavaScript'">document.Pdf2.printAll()</script>");
Note that the client must have Acrobat Reader installed on his machine for
this solution to work.