Click here to Skip to main content
15,892,059 members
Articles / Web Development / IIS
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
3.24/5 (12 votes)
28 Nov 2013CPOL1 min read 82K   16   10
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:

XML
<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:

XML
<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.

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Spctek
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionquery Pin
Jitesh Chhaya8-Nov-22 18:33
Jitesh Chhaya8-Nov-22 18:33 
QuestionSend PDF files directly to client printer without dialog box in ASP.NET application - Not working in Chrome Pin
NiteshSlSinghal17-Dec-21 19:36
NiteshSlSinghal17-Dec-21 19:36 
QuestionProblem with Printers in other network. Pin
R_042910-Apr-17 20:13
R_042910-Apr-17 20:13 
GeneralMy vote of 1 Pin
Sharma Richa19-Jul-14 11:36
Sharma Richa19-Jul-14 11:36 
Not Working
BugNot working Pin
Raj_Doshi272-Dec-13 17:54
professionalRaj_Doshi272-Dec-13 17:54 
GeneralMy vote of 1 Pin
athg1-Dec-13 23:44
athg1-Dec-13 23:44 
GeneralMy vote of 2 Pin
Chris1983.NET1-Dec-13 22:51
Chris1983.NET1-Dec-13 22:51 
QuestionThis is not not work for me Pin
Sadequzzaman Monoj1-Dec-13 17:27
professionalSadequzzaman Monoj1-Dec-13 17:27 
SuggestionSome Alternative and more "involved" options... Pin
MacSpudster29-Nov-13 8:08
professionalMacSpudster29-Nov-13 8:08 
GeneralRe: Some Alternative and more "involved" options... Pin
VMAtm3-Dec-13 2:53
VMAtm3-Dec-13 2:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.