Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
How to Print PDF that is located in the Application Source Location in ASP.NET either by using client side coding or code behind coding


What I have tried:

C#
protected void PrintPDF(string path)
   {
       try
       {
           using (Process printjob = new Process())
           {

               printjob.StartInfo.FileName = path;

               printjob.StartInfo.Verb = "Print";

               printjob.StartInfo.CreateNoWindow = true;

               printjob.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

               PrinterSettings setting = new PrinterSettings();

               setting.DefaultPageSettings.Landscape = true;

               printjob.Start();
           }
       }
       catch (Exception ex)
       {
           MessageBox.Show(ex.Message);
       }
   }
Posted
Comments
F-ES Sitecore 10-Aug-16 4:21am    
Is the file stored on the client or the server? Also the code you pasted won't work from asp.net, that is winforms code. Also if your intention is to print automatically from a website then you can't. Download the file to the client (if it's on the server) and they'll just have to print it themselves.
JanardhanSharma 10-Aug-16 5:00am    
but it's working for asp.net as well. by default it's allowing the pdf to be opened in adobe reader but in a while it's also getting opened in OneNote.. once it's displaying in OneNote then the pdf file getting closed.. this is all happening in seconds. i dont understand this abnormal behaviour.
Richard Deeming 10-Aug-16 8:08am    
If you're running the code in Visual Studio, it might appear to work; but that's only because the server and the client are the same computer in that specific case.

As soon as you deploy to a real server, the code will stop working. The MessageBox.Show will either throw an exception, or display the message on the server and hang waiting for someone to press the button.

And even if you could get the server-side code to print, which would involve changing permissions and editing the registry, the output would come out of the server's printer, not the client's printer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900