Click here to Skip to main content
13,250,043 members (65,133 online)
Click here to Skip to main content
Add your own
alternative version

Stats

55.3K views
15 bookmarked
Posted 28 Nov 2013

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

, 28 Nov 2013
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

kashif Atiq
Software Developer (Senior) Spctek
Pakistan Pakistan
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionProblem with Printers in other network. Pin
R_042910-Apr-17 21:13
memberR_042910-Apr-17 21:13 
GeneralMy vote of 1 Pin
Sharma Richa19-Jul-14 12:36
memberSharma Richa19-Jul-14 12:36 
BugNot working Pin
Raj_Doshi272-Dec-13 18:54
memberRaj_Doshi272-Dec-13 18:54 
GeneralMy vote of 1 Pin
Member 103573102-Dec-13 0:44
memberMember 103573102-Dec-13 0:44 
GeneralMy vote of 2 Pin
Chris1983.NET1-Dec-13 23:51
memberChris1983.NET1-Dec-13 23:51 
QuestionThis is not not work for me Pin
Sadequzzaman Monoj1-Dec-13 18:27
memberSadequzzaman Monoj1-Dec-13 18:27 
SuggestionSome Alternative and more "involved" options... Pin
MacSpudster29-Nov-13 9:08
memberMacSpudster29-Nov-13 9:08 
GeneralRe: Some Alternative and more "involved" options... Pin
VMAtm3-Dec-13 3:53
memberVMAtm3-Dec-13 3: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171114.1 | Last Updated 28 Nov 2013
Article Copyright 2013 by kashif Atiq
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid