Tuesday, August 24, 2010

Silent Printing PDF File in C# and closing Adobe Popup

Printing is very common task in Desktop App. In this article i will demonstrate how to print a PDF File Silently. There are two approaches. First We can print the file using PDF Sharp ( a open source library to support PDF Related Operations) and Arobate exe. Second Approach is to intiate a process that call Acrobate now doubt both need at least acrobat installed on machine, as PDF Sharp can not render PDF It self. I will demonstrate the first way.
Add Reference to PDF Sharp. First inform PdfFilePrinter about path of Adobe reader exe on target machine.
PdfFilePrinter.AdobeReaderPath = @"C:\Program Files\Adobe\Reader 8.0\Reader\AcroRd32.exe";

///Then in next step create a instance of the class PdfFilePrinter and provide path of file to print and your default active printer.

try
{
PdfFilePrinter Pdfprinter = new PdfFilePrinter(filetoprint, printer);
Pdfprinter.Print();
}
catch(Exception ex)
{
throw ex;
}
Now get the Adobe Process and kill it if it has finished its job.

foreach (Process proc in Process.GetProcesses())
{
if (proc.ProcessName.StartsWith("Acro"))
{
if (proc.HasExited == false)
{
proc.WaitForExit(10000);
proc.Kill();
break;
}
else
{
proc.Kill();
break;
}
}
}


if you have any question please free to ask. Hope this helps.


7 comments:

  1. Dear Waqar,

    If the printer is not a real printer but needs a provide a file name (like something print to file's type printer driver) then what to do?

    Please help. Thanks

    Yeong Shih

    ReplyDelete
  2. Dear Yeong
    If you want to print using Virtual or Type printer just set that printer as your default printer from printer settings. and run the code it will ask for the file name and location to save

    ReplyDelete
  3. hi i need to print a pdf document without showing a acrobat reader . your solution can lead to a problem because it will kill all the process which are acobat related that i dont want . Can you provide another soltiion

    ReplyDelete
  4. Hi,unfortunately this didn' worked for me, I have tried a few solutions also and the one I like the most is CLPRINT tool. Take a look at http://commandlinepdf.com/

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  5. c# acrobat print pdf with comments is the most helpful software can be download on rasteredge page http://www.rasteredge.com/how-to/csharp-imaging/pdf-annotate-sticky-note/

    ReplyDelete