AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=514&pId=-1
CodeSnip: Printing PDF from .NET
page
by Jonathan Cogley
Feedback
Average Rating: 
Views (Total / Last 10 Days): 59344/ 43

Introduction

When automating tedious paper-based processes, you may often have the need to automatically print documents.  One of the most popular formats in online document management is Adobe's Portable Document Format (PDF).  Printing a PDF can be a tricky feat to accomplish in .NET.  After some research, I have yet to find a native approach to send the PDF to a printer in a format that will be printed correctly.

How can it be done?

Adobe Acrobat Reader Does the Dirty Work

Adobe's free Acrobat Reader can be used to do the printing for you!  This has an admittedly less sophisticated feel, but it does get the job done.

The feature we will use in Adobe Acrobat Reader is undocumented and unsupported but widely discussed on the Internet.  It is a commandline switch that only works reliably in Version 4 of the Adobe Acrobat Reader. 

The syntax is:

C:\Program Files\Adobe\Acrobat 4.0\Reader\AcroRd32.exe /t "myexampledocument.pdf" "My Windows Printer Name"

The /t option is an undocumented feature that causes the document to be printed with no prompting dialogs or user input required.

You can install Adobe Acrobat Reader 4.0 from here (The whole version archive is here).  Once installed, you can execute the above command from the command line with your own PDF document and printer name to see it automatically print.

How do you run this command from .NET?

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.



©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-03-19 4:04:17 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search