Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Send PDF files directly to client printer without dialog box in ASP.NET application

0.00/5 (No votes)
28 Nov 2013 1  
How to send PDF files directly to client printer without dialog box in ASP.NET application

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" // temp/mypdf.pdf 

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here