Freelancer Martin Zeller

php asp.net java c# .net c++ xml xhtml seo magento zend framework dotnetnuke tomcat iis sql server mysql oracle typo3 coremedia

PDF: direct printing with .NET

Tags: ,

>> Download Example project (VS 2005)

In this article I will show you the results of my search to find a way to print a pdf with .NET.

My task was to find a simple solution for an intranet web application where the user gets pdf reports (ActiveReports). Depending on configuration settings these pdf files should be displayed in the browser and be printed immediately or just be printed immediatly – to the default printer or to a known network printer.
The existing solution was based on the pdf browser plugins. But it was not satisfactory because the user had to do the following steps: view the pdf file in the browser (pdf browser plugin), click the print button, handle the printer dialog popup, click the OK button to send the document to the printer. And some users had to do this more then one hundred times a day. And some users had to do this more than a hundred times a day!
My (more than revolutionary ;-) idea was, that I may need the pdf browser plugin for viewing pdf files, but not for printing them! So I tried different ways, which I wanted you to show now – I did not bring all of them to an end, as some turned out to be inappropriate for  my intentions very soon.
Important: these solutions are only making sense if you want to print to a known printer (like in intranets) – they will not work on normal, “public” web sites.

You can download the sample project and do your own experiments. Here is a screenshot of the sample project:

PrintingPDF WebApp Screenshot

My results:

  1. Printing a pdf with Ghostscript
  2. Printing a pdf with windows shell command
  3. Printing a pdf with PrintDocument object
  4. Printing a pdf with PDFsharp
  5. Printing a pdf with Acrobat Reader command line

1. Printing a pdf with Ghostscript

First you have to install Ghostscript, an interpreter for the PostScript language and for PDF. Code:

ProcessStartInfo psInfo = new ProcessStartInfo();
psInfo.Arguments = String.Format(” -dPrinted -dBATCH -dNOPAUSE -dNOSAFER -q -dNumCopies=1 -sDEVICE=ljet4 -sOutputFile=\”\\\\spool\\{0}\” \”{1}\”", printerName, pdfFileName);
psInfo.FileName = @”C:\Program Files\gs\gs8.70\bin\gswin32c.exe”;
psInfo.UseShellExecute = false;
Process process = Process.Start(psInfo);

2. Printing a pdf with windows shell command

This is another version I found on the web. The code uses the print/printto command of the DOS shell. In the example project, which you can download above, there are two versions of parameters in the code. I never got it work, but I think, it cannot be done: you cannot print a pdf file via shell – but it will work with raw text files. Please tell me your results!
Try this in your DOS prompt:
print /D:”\\COMPUTERNAME\PRINTERNAME” “PDFFILEPATH”

For those who want to try it themselves

ProcessStartInfo psInfo = new ProcessStartInfo();
psInfo.Verb = “Print”; // or “PrintTo”
psInfo.FileName = pdfFileName;
psInfo.Arguments = String.Format(”/p /h \”{0}\” \”{1}\”", pdfFileName, printerName);
psInfo.WindowStyle = ProcessWindowStyle.Hidden;
psInfo.CreateNoWindow = true;
psInfo.UseShellExecute = true;
Process process = Process.Start(psInfo);

3. Printing a pdf with PrintDocument object

Using the .NET object PrintDocument is another possible way, but you will need third party components to raster the pdf. More information: >>Component for rendering pdf documents

PrintDocument pd = new PrintDocument();
pd.DocumentName = pdfName;
pd.PrinterSettings.PrinterName =printerName;
pd.PrinterSettings.PrintFileName = fileName;
pd.PrintController = new StandardPrintController();
pd.OriginAtMargins = false;
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
pd.Print();

4. Printing a pdf with PDFsharp

PDFsharp is an open source .NET library for processing PDF. You can use it for printing too. Simple usage, but the problem here: the AdobeReader application window appears (not suitable for my intentions). But I found out that you do not need PDFsharp to print out a pdf – see chapter 5

PdfFilePrinter.AdobeReaderPath = @”C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.exe”
PdfFilePrinter printer = new PdfFilePrinter(pdfFileName, printerName);
printer.Print();

5. Printing a pdf with Acrobat Reader command line

This was the best way for me! Although I’ve read that this only works with old versions of Acrobat Reader, it works with version 9.0!
A word to the arguments:
print it to the default printer: /s /o /h /p “pdfFileName”
print it to a defined printer: /s /o /h /t “pdfFileName” “printername” “drivername” “portname”
More information at the Adobe Acrobat Developer FAQ on page 27!
To download the free acrobat reader use this link: Acrobat Reader

ProcessStartInfo psInfo = new ProcessStartInfo();
psInfo.FileName = @”C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.exe”;
psInfo.Arguments = String.Format(”/s /o /h /p{0}”, pdfFileName);
psInfo.WindowStyle = ProcessWindowStyle.Hidden;
psInfo.CreateNoWindow = true;
psInfo.UseShellExecute = true;
Process process = Process.Start(psInfo);

Tags: ,

13 Responses to “PDF: direct printing with .NET”


  1. descargar msn
    on Feb 10th, 2010
    @ 20:04

    Thanks for the tip, Keep up the great work.


  2. Sherly Luangamath
    on Feb 12th, 2010
    @ 23:01

    Very informative text. I’ve found your blog via Bing and I’m really happy about the information you provide in your posts. Btw your sites layout is really messed up on the Chrome browser. Would be great if you could fix that. Anyhow keep up the good work!


  3. Rogelio Contorno
    on Feb 21st, 2010
    @ 06:30

    Good Job on the articles you have here, thank you for putting your time into it!


  4. sandra
    on Feb 27th, 2010
    @ 12:41

    5. Printing a pdf with Acrobat Reader command line

    what i should download inorder to be able to use it?


  5. Martin Zeller
    on Feb 27th, 2010
    @ 13:39

    Hi Sandra,

    just download the acrobat reader:
    http://get.adobe.com/reader/

    Good luck
    Martin


  6. sandra
    on Feb 27th, 2010
    @ 16:54

    hi martin
    thank u about ur answer

    in fact i have visual studio 2008 and its give me error

    can u help me?
    my q2- i should get license for ur article or any files … DLL ..etc that included in ur project ? or all of them are open source?


  7. Martin Zeller
    on Feb 27th, 2010
    @ 17:03

    Hi Sandra,

    The only third party dll I am using in the example project is from pdfsharp. You can download and check other regarding stuff at the pdfsharp homepage:
    http://www.pdfsharp.com/PDFsharp/
    (It is open source!)

    I cannot help you with VS2008, I am still using VS2005.


  8. sandra
    on Feb 27th, 2010
    @ 20:21

    hi martin
    thank u alot about ur reply
    i have download PDFSharp

    but i dont know how to use it in asp.net ( ididnt see any PDFSharp.DLL)

    if u can help me i will be happy , and if u cant thank u alot :)


  9. sandra
    on Feb 27th, 2010
    @ 21:36

    hi,
    thank u alot about answering

    i have download pdfsharp,

    but when i want to add this to my asp.net application , ididnt any PDFSharp.DLL

    can u tell me the steps in order to be able to use it in asp.net ?

    thx


  10. Martin Zeller
    on Feb 27th, 2010
    @ 22:03

    Hi Sandra,

    just use this link to download the zip file with the assemblies – there you will find the dll:

    http://sourceforge.net/projects/pdfsharp/files/

    Martin


  11. Andelia
    on Mrz 10th, 2010
    @ 10:52

    Hi.I like reading your post , keep doing it.


  12. jack parler
    on Mai 13th, 2010
    @ 02:31

    Very good information.


  13. Vijin N
    on Jun 6th, 2010
    @ 11:23

    I got error using Javscript function.How to solve below error

    ERROR: Automation server can’t create object

Leave a Reply

© 2009 Freelancer Martin Zeller. All Rights Reserved.

This blog is powered by Wordpress and Magatheme by Bryan Helmig.