CodeSnip: Printing PDF from .NET
page 3 of 3
by Jonathan Cogley
Feedback
Average Rating: 
Views (Total / Last 10 Days): 59344/ 40

Executing Adobe Reader from .NET

The .NET Framework provides classes in the System.Diagnostics namespace that can be used to start external processes.  I have used the following code in several projects to start various executables and we can use it to start Adobe Acrobat Reader too.

using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;


private static void RunExecutable(string executable, string arguments) 
  {
   ProcessStartInfo starter = new ProcessStartInfo(executable, arguments);
   starter.CreateNoWindow = true;
   starter.RedirectStandardOutput = true;
   starter.UseShellExecute = false;
   Process process = new Process();
   process.StartInfo = starter;
   process.Start();
   StringBuilder buffer = new StringBuilder();
   using (StreamReader reader = process.StandardOutput) 
   {
    string line = reader.ReadLine();
    while (line != null) 
    {
     buffer.Append(line);
     buffer.Append(Environment.NewLine);
     line = reader.ReadLine();
     Thread.Sleep(100);
    }
   }
   if (process.ExitCode != 0) 
   {
    throw new Exception(string.Format(@"""{0}"" exited with ExitCode {1}. Output: {2}", 
executable, process.ExitCode, buffer.ToString());  
   }
  }

You can print your PDF by incorporating the above code into your project and using it as follows:

string pathToExecutable = "c:\...\acrord32.exe";
RunExecutable(pathToExecutable, @"/t ""mytest.pdf"" ""My Windows PrinterName""");

Thanks to my co-worker and friend, Bob Flanders, for helping find this solution for printing PDF documents.


View Entire Article

User Comments

Title: Doubt   
Name: Arun
Date: 2011-06-01 2:10:09 AM
Comment:
Please tell me how to set the page settings in this methords.
URGENT

Thanks in advance
Arun PS
Title: Ms.   
Name: Deep
Date: 2010-12-09 9:30:59 AM
Comment:
Hello Jonathan, This seems like a good option for me, but when I tried this code in my c# Windows application, it did not work. When I run this, nothing gets printed, and no exception is thrown. Any tips on what needs to be fixed?

Thank You in advance
Title: Learner   
Name: Suhail Akhter
Date: 2010-11-25 9:10:38 AM
Comment:
No comments. grateful to you
Title: Software Developer   
Name: Newbie to Acrobat
Date: 2010-09-01 12:40:46 PM
Comment:
Hi,

I tried your code and it worked fine. But my requirement is little bit different. It is opening Acrobat Reader and asking to save the file.

Is it possible to suppress that and Save pdf file behind the scene to any folder location?

Can you please help me out for it?

Thanks in Advance.
Title: Thanks   
Name: Harold
Date: 2010-08-10 4:47:59 PM
Comment:
This works fine for Acrobat Reader 9. Had it up and running in 5 to 10 minutes. Thanks very much.
Title: Security   
Name: Brian
Date: 2010-05-20 6:55:26 PM
Comment:
I think it's great that this is a plausible solution, but Adobe reader 4.0? Do you have any idea how insecure that program is? You have just told every random programmer who looked up the printing pdf's to put a very old program riddled with software bugs on their web servers. Way to go. Those of you reading this article should not be using Adobe reader version 4.x as it is insecure.
Title: Client side printing   
Name: Raj
Date: 2010-01-06 6:53:49 AM
Comment:
Hi, this seems to be very useful. But my question is, does it work on client machine as well?
Thanks in advance
Title: Programmer   
Name: Gh Jilli
Date: 2009-12-23 9:53:32 AM
Comment:
I have tried to implement above code unfortunatily it is not working i am getting process.ExitCode as -1
Please advise me
Title: Lead Develper   
Name: oe Hardwell
Date: 2009-09-15 7:44:17 PM
Comment:
Look, I'm just trying to take up less paper evertime I want to read a pdf. Thats all. Will WTF do that? What is ther website?
Title: There is easy way to doit   
Name: John LeHawk
Date: 2009-08-28 3:28:29 PM
Comment:
i used forensictools.net 's PDF Image Printer. It is very easy. it is not opening the reader. Just send file bummm.

AND it is FREEE

Just Use it

Time is MONEY
Title: Lead Develper   
Name: Joe Hardwell
Date: 2009-08-13 10:07:16 AM
Comment:
This works pretty good. How can I make a two-sided (front and back) PDF instead of a single sided multi-page pdf? huh?
Title: Dev Prog II   
Name: M Desrosiers
Date: 2008-07-08 11:48:23 AM
Comment:
\
\
Title: PDF8/gsprint in ASP.NET   
Name: John Hopson
Date: 2007-12-12 11:20:27 AM
Comment:
This thread has been very help, so thank everyone for the posts. Has anyone tried pdf8 or gsprint on a live web server using ASP.NET? Both work great in development, but no response when the application is moved to the web server. Perhaps a security/permission issue??
Title: Do your Own Work!   
Name: Frustrated
Date: 2007-12-04 3:57:27 PM
Comment:
To all of those people who asked for Jonathon to 'fix' your code: Do it yourself, this a sample for you to start from, if you want to be a programmer, program it yourself.

And to Todd Simton, you idiot, if you call yourself a Sr Programmer, you should be able to close a process. You deserved to be fired!
Title: Sr Programmer   
Name: Todd Simton
Date: 2007-11-28 2:00:41 PM
Comment:
Hey Jonathan Cogley,

My server crashed because I had 500 windows opened. Why does this code not close the pdf window. You cost me my job.
Title: In Acrobat 8.0 printing not working   
Name: Saravanan
Date: 2007-11-02 5:53:33 AM
Comment:
Could you plz tell me how to print using c# using acrobat 8.0 ? This code is not working for 8.0 version!
Title: Thanks you very much   
Name: Muhammad Adil Aleem
Date: 2007-07-12 1:02:45 PM
Comment:
Thanks for your briliant post. although some code is missing over here like close the acrobat after printing and find printer etc. but the main idea to print PDF programmatically is very nice.
Title: using this code in asp.net with c#   
Name: saurabh kumar
Date: 2007-06-11 7:17:29 AM
Comment:
hi all
By this code print pdf file from local system fine, but print from server on client machine and from client printer does not work.
any suggestion r welcome.Please help me.This is very urgent.

thanks in advance
Title: Business Analyst   
Name: Ashwani Raghav
Date: 2007-05-14 8:14:53 PM
Comment:
Hi there,

This code is working for me from the command line, but does not work from asp.net form. I changed this code in vb.net and used, but did not work.
Please help.

Thanks in advance.
Title: Business Analyst   
Name: Ashwani Raghav
Date: 2007-05-14 8:00:39 PM
Comment:
Hi,

This code does not work for Acrobat reader 8.0. Can anyone provide me the details on how i can use this to print form acrord32.exe for version 8.0.

Thanks,
Ashwani
Title: Make sure you open 4.0 after installing   
Name: MR
Date: 2007-03-14 12:37:31 PM
Comment:
After you download and install Adobe Reader 4.0 from the link in the article, make sure you run the program on the server (go to Start > Programs > Adobe Acrobat 4.0 > Acrobat Reader 4.0). The first time Reader runs, you have to click "OK" on a license screen. If you don't do this, the command line will never run correctly, because it's waiting for you to accept the license, which you can't do because you opened it with the /t switch.
Title: Answers...   
Name: chicks
Date: 2007-01-26 5:14:29 PM
Comment:
\
\
Title: Answers...   
Name: Jonathan Cogley
Date: 2007-01-14 5:31:29 PM
Comment:
Hi everyone! Thanks for all the questions - unfortunately I have not tried many of the things you are asking about ...
For example: duplex printing (probably an Adobe option - check the documentation), getting the client printer name (this should be easy to either scan the local network or ask the user for the printer name - bear in mind that the server will need network access to that printer), printing multiple documents (you can't just loop the code provided?), VB.NET example (this seems trivial and there are many tools to do this on the web ... ). If anyone has answers to these questions or has tried these themselves then please post replies.

Good luck everyone!
Title: Seeking for a vb.net example   
Name: Ashish
Date: 2007-01-08 5:42:38 AM
Comment:
Hi Jonathan,
First I would like to thank you for helping so many people.
But, I worked on your code, but it's not working in my case. The "Line" is blank(""). I think it can't able to read that file.
Can you pl. provide me a Vb.net example of this?
I will be very thank ful..

Ashish
Title: Multiple PDFs for silent printinf   
Name: John
Date: 2007-01-05 2:49:53 AM
Comment:
Hi Jonathan,

Would be great if you could reply my couple of question:

I need to allow user to print multiple document slected by him.
Can I allow user to print on client's printer using this code?
How could I get the client printer name in RunExecutable(pathToExecutable, @"/t ""mytest.pdf"" ""My Windows PrinterName""");

If anybody has any solution to this, please provide.

Thanks,
John
Title: Duplex?   
Name: Anders Pettersson
Date: 2006-11-22 7:44:10 AM
Comment:
What if I want to print it in duplex (double sided)?
Title: Alternative Method   
Name: chicks
Date: 2006-09-30 10:03:27 PM
Comment:
Sorry, entered URL above, figured it would show. Obviously not.

http://www.esnips.com/web/PDFTools
Title: Alternative method   
Name: chicks
Date: 2006-09-30 10:01:52 PM
Comment:
See pdfp here for an alternative PDF printer, which drives Adobe Reader through its published interface, and provides several options.
Title: It worked for me   
Name: Scott Brown
Date: 2006-09-08 4:55:20 PM
Comment:
I got it to work with Acrobat Reader 7.0, but I used:

Dim line As String = process.StandardOutput.ReadToEnd

instead of the reader. It worked nicely and AcroRead32.exe closed down nicely like it should.
Title: Back again   
Name: Steve W.
Date: 2006-09-06 12:38:37 PM
Comment:
Use Acrobat 4.0 to get it to close afterward. Other versions won't. You can have both installed on a machine, so it's a pain, but not impractical. Good luck!
Title: Bug fix?   
Name: Steve W.
Date: 2006-09-06 12:28:45 PM
Comment:
Hi there,
There seem to be a lot of bugs with the process hanging. This occurred for me with a network printer; if you have a network printer, try porting the IP locally and you should be off to the races.
As for shutting down the process once it's open, I'm open to suggestions.
Title: I liked the article, but...   
Name: Andrew. E. Kirin - AKirin@AlleghenyLudlum.com
Date: 2006-09-02 5:56:30 PM
Comment:
I wish this work in version 7.0 I still get the adobe window opening

Thanks
Title: Can't find Adobe 4.0   
Name: Brent
Date: 2006-08-09 3:18:50 PM
Comment:
Anyone know where I can find Adobe 4.0? I've tried this using 7.0 and it leaves an AcroRd32.exe process running. I'm trying to print batches of .pdf files from within a windows service.
I tried the PrintPDf c# code and couldn't get it to work, maybe someone else could ...
http://sourceforge.net/project/showfiles.php?group_id=118184
Title: Thanks a ton!!   
Name: Santhosh
Date: 2006-08-08 6:17:54 PM
Comment:
Thanks a lot for the info. But one thing when my pdf opens and print the file, its not closing the instance and the pdf remains open. If i exit out of acrobat i get the exception. Am i doing some thing different?
Title: Exception line read is null   
Name: Hanaa Al-Ostad
Date: 2006-06-05 3:45:40 AM
Comment:
I have used ur code in ASP.Net application but It raise Exception , where line read is always null, the code is working fine in Windows application, you siad that u used embedded .NET control to use this code in a webpage what do you mean by embedded .NET control
Title: Sr.   
Name: Sergio García
Date: 2006-04-22 6:46:49 AM
Comment:
First of all sorry for my horrible english.

I use this code in VB .Net with Acrobat Reader 7.0 and it doesn´t work fine buy finally I can get it working.

I had some problems: I need to print my PDF document to a specific printer and "to file". Using the "port" argument and writting in it the name of the file it works sometimes. Othertimes the document wasn't printed to the file, it was printed to the printer (which is a fax sender server, really).

My solution: I generate a new printer in the system. This printer is intended to print ALL to file. This way I solved one of my problems.

Another problem (like someone had writed before) is that Acrobat Reader prints the document but it doesn´t close. Until you close manually the Acrobat Reader the process stop.

My solution: two threads. I one thread I call to the acrobat executable like in the above code. In the main thread I wait to the "virtual printer" to finish the job. In this moment I kill the process and all works fine. (If I close the Acrobat Reader after the finish of the job, this one is "cutted").

I hope this idea can help anybody.

Greetings from Spain.
Title: hi sir   
Name: viswanath
Date: 2006-04-04 8:39:36 AM
Comment:
sir,
i saw ur posting,i want to download and print the pdf file silently from net and PDF POPUP WINDOW SHOULD BE CLOSED.

HELP ME
VISWANATH
VISSU.MADISETTY@YAHOO.COM
Title: good work   
Name: viswanath
Date: 2006-04-04 7:07:23 AM
Comment:
hi,
this is a good posting but i want to print the pdf file by down loading from the net and it should close the acrobat window once the document is printed. "
"HELP ME INTHIS ISSUE" ITS URGENT
VISWANATH
VISSU.MADISETTY@YAHOO.COM
Title: one other way   
Name: Bala
Date: 2006-03-13 7:26:45 AM
Comment:
You might also want to checkout http://pdfprn.sf.net.
It's an open-source library dedicated to print PDF files.
Title: You Rock!   
Name: shantzy
Date: 2006-03-02 11:57:00 PM
Comment:
Thank you, thank you, thank you! This is exactly what I've been looking for for about 6 months. I had to convert it to VB.NET, but other than that it was perfect. Thanks!!!
Title: Thanks   
Name: Ruben
Date: 2006-02-22 9:03:03 AM
Comment:
Thanks, this works great!
Title: Adobe Acrobat version   
Name: Jonathan Cogley
Date: 2006-02-18 1:34:51 PM
Comment:
Please note that this article and associated code snippet was written for a specific version of Adobe Acrobat and has not been tested on all versions.

You may get different results with other versions of Adobe Acrobat.

The article specifically says "only works reliably in Version 4".

Good luck!
Title: Its a lie   
Name: Mohit Singhal
Date: 2006-02-18 7:24:44 AM
Comment:
Hi,

I use this code and every time the execution reaches to

'process.Start();' an acrobat reader's instance is created. I'm feeling sick of killing that but its not working at all. Is there some extra code to add then add in ur article.

Regards Mohit
Title: Other options for creating PDF   
Name: Jonathan Cogley
Date: 2006-02-17 9:07:07 AM
Comment:
There are plenty of ways to create PDF from .NET.
http://csharp-source.net/open-source/pdf-libraries
And there are many commercial alternatives too.

We have used iTextSharp in the past with success.
Title: Good Stuff   
Name: arayanaswamy
Date: 2006-02-17 6:41:40 AM
Comment:
s there any way to generate PDF documents without using Adobe Acrobat
Title: Printing PDF from .NET   
Name: Paul W
Date: 2006-02-11 4:41:18 PM
Comment:
Thank you. Succinct and it works - this is somewhat novel - and I didn't have to sign up up for SPAM to get it.

You used the word "reliably" in your article - does it work at all in other versions?
Title: ASK question   
Name: TY
Date: 2005-09-23 12:13:37 AM
Comment:
what is the value of executable and arguments
Title: /t Does not works   
Name: Ajit
Date: 2005-09-07 7:23:28 AM
Comment:
i want a silent print though no dialog box appears but acrobat reader window pops up. kindly suggest.
Title: acrobat.exe behaves weird   
Name: robin mathew
Date: 2005-08-09 6:29:22 PM
Comment:
I am using acrobat.tlb to open a PDF and save the meta data and the save the pdf file. AFter I do a couple of files it seems to hang untill I kill acrobat.exe. any ideas?
Title: About ur article   
Name: Majid Khan
Date: 2005-08-01 7:35:44 AM
Comment:
I have used ur code but It raise Exception , where line read is always null.Please solve my problem as soon as possible.
Title: Need Help   
Name: varada
Date: 2005-07-29 3:58:04 AM
Comment:
It doesnt work for me. I have Acrobat4.0 but nothing happend. it does not even retrun error. can any one help me?
Title: Create and print PDFs with GhostScript free/open source   
Name: John Hebert
Date: 2005-06-10 5:01:11 PM
Comment:
http://www.cs.wisc.edu/~ghost/

You can print PDFs from the command line with gsprint.exe, which is included in the package. You can also create PDFs in a number of ways. Check it out!
Title: Mr   
Name: joe ho
Date: 2005-06-07 11:08:26 AM
Comment:
I tried the above code. nothing happens. i have installed acrobat v4.0. what's wrong?
Title: Problem with web application   
Name: Madhan
Date: 2005-03-13 11:24:18 PM
Comment:
I have tried the same code for my web application. It is initiating the acrobat.exe but printing is not working.

Any idea to fix this?

Thanks,

Madhan
Title: Problem with ASP.NET   
Name: Simon Han
Date: 2005-03-03 8:44:57 PM
Comment:
I have tried this with ASP.NET and it opens the acrord32.exe processor under the ASPNET username which makes it very very slow. However, if i use it for a console application, it is great. Do you have any idea how to fix it?
Title: Paper Size   
Name: Lenus
Date: 2004-12-28 8:38:39 AM
Comment:
Hi,

I've tested the above way of doing, and it works fine.
But the only thing is that my paper size is a custom size.
And i can't seem to make it print the actual size. It will
always be in letter size. Is there any way to change it?

I've even tried different versions of Acrobat Reader. And only version 4 works thru' web. The only thing is the paper size. I've also tried the DDE method. By setting starter.Verb = "Print";

Can anyone help? Thanks a lot!
Title: For Intranet Purposes   
Name: Jonathan Cogley
Date: 2004-10-04 11:30:02 PM
Comment:
Yes, this code is for printing to a local printer. This means that you will need to be either running the .NET code on your server (and printing to a printer on the server's network) or on your client (WindowsForms application or Console or embedded .NET control in a webpage).

The actual implementation that this article's code was originally developed for was a C# console application for printing PDFs obtained from a webservice (WSE 1.0 - DIME) over the Internet and then printed to a local printer.

This code will work in any situation where you are running .NET code and need to print PDFs to a local printer.
Title: For Intranet Purposes   
Name: Jason N. Gaylord
Date: 2004-09-17 8:29:19 AM
Comment:
This article is for Intranet purposes.
Title: Ways to generate PDF from .NET   
Name: Jonathan Cogley
Date: 2004-09-16 9:06:01 AM
Comment:
Anand,

There are many APIs available for generating PDFs from .NET.

There is iTextSharp and ITextdotNET which are both open source ports of iText from Java. (I have used iTextSharp)

There are also several commercially available APIs such as ABDpdf.NET (which I have used)

Find more here:
http://www.google.com/search?hl=en&ie=UTF-8&q=.net+pdf

Regards,
Jonathan
Title: Good Stuff   
Name: Anand Narayanaswamy
Date: 2004-09-16 2:05:25 AM
Comment:
Hi,

Is there any way to generate PDF documents without using Adobe Acrobat?

Regards,
Anand N
Microsoft MVP
NetAns Technologies
www.netans.com
www.learnxpress.com
www.dotnetalbum.com






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-03-18 10:26:32 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search