Click here to Skip to main content
15,884,388 members
Articles / General Programming / Printing
Tip/Trick

How to Silently Print PDFs using Adobe Reader and C#

Rate me:
Please Sign up or sign in to vote.
4.72/5 (13 votes)
28 May 2013CPOL2 min read 261.2K   25   28
Silently print PDFs.

Introduction

This tip is merely to show a way in which you can launch Adobe and send a PDF straight to the printer in one fail swoop without using a third party solution (PdfSharp/iTextSharp...etc...).

Background 

After reviewing some of Adobe's documentation on command line switches, I was able to throw together a bit of code that would send a PDF straight to the printer without any user interaction. Attempting to send a post script straight to the printer can get nasty/complicated so this is meant to be an easy solution on how to take a newly created PDF/existing PDF from your app and send it directly to the printer.

The Code Explained  

The code below is used in a console application. I know it could be easily incorporated into any GUI based desktop application as well.

Usage

  1. Print all PDFs from a defined directory:
  2. C#
    string[] files = Directory.GetFiles(@"c:\temp");
    foreach (string file in files.Where(file => file.ToUpper().Contains(".PDF")))
    {
         Pdf.PrintPDFs(file);
    }
  3. Simply print the PDF file:
  4. C#
    Pdf.PrintPDFs(filename); 

Explained: 

I am going to make an assumption that the basics of using the Process class are understood. 

C#
//Define location of adobe reader/command line switches to launch adobe in "print" mode
proc.StartInfo.FileName = @"C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe";
proc.StartInfo.Arguments = String.Format(@"/p /h {0}", pdfFileName);
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;   
  1. proc.StartInfo.FileName
  2. The value should be the absolute path to your Adobe Reader instance (should work with Acrobat as well). As far as I can tell in my research, most of the current versions of Adobe Reader should support the command line switches

  3. proc.StartInfo.Arguments
  4. These are your command line switches to be applied to Adobe Reader:

    • /p <filename>    => means open and go straight to print dialog
    • /h    => open adobe reader as a minimized window
  5. KillAdobe("AcroRd32");
  6. Occasionally in my usage of this code, Adobe Reader likes to stick around for whatever reason. I guess it appears proc.Close() does not have any affect on the reader, my thinking is that this is due to it trying to close in the middle of printing a file...however my brute force way to make sure the reader doesn't linger is to kill it.

So you grab all the processes that start with your process name (in our case AcroRdr32) and call the .Kill method for the process.

C#
foreach (Process clsProcess in Process.GetProcesses().Where(
          clsProcess => clsProcess.ProcessName.StartsWith(name)))
{
     clsProcess.Kill();
     return true;
}

Code in its Entirety

C#
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PdfPrinter
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] files = Directory.GetFiles(@"c:\temp");
            foreach (string file in files.Where(
                        file => file.ToUpper().Contains(".PDF")))
            {
                 Pdf.PrintPDFs(file);
            } 
        }
    }//END Class

    public class Pdf
    {
        public static Boolean PrintPDFs(string pdfFileName)
        {
            try
            {
                Process proc = new Process();
                proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                proc.StartInfo.Verb = "print";

                //Define location of adobe reader/command line
                //switches to launch adobe in "print" mode
                proc.StartInfo.FileName = 
                  @"C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe";
                proc.StartInfo.Arguments = String.Format(@"/p /h {0}", pdfFileName);
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.CreateNoWindow = true;

                proc.Start();
                proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                if (proc.HasExited == false)
                {
                    proc.WaitForExit(10000);
                }

                proc.EnableRaisingEvents = true;

                proc.Close();
                KillAdobe("AcroRd32");
                return true;
            }
            catch
            {
                return false;
            }
        }

        //For whatever reason, sometimes adobe likes to be a stage 5 clinger.
        //So here we kill it with fire.
        private static bool KillAdobe(string name)
        {
            foreach (Process clsProcess in Process.GetProcesses().Where(
                         clsProcess => clsProcess.ProcessName.StartsWith(name)))
            {
                 clsProcess.Kill();
                 return true;
            }
            return false;
        }
    }//END Class
}//END Namespace

Points of Interest

This is my first article/tip on CodeProject so I appreciate any feedback on how to improve this.

History 

  • 05/27/2013 - Published article.
  • 05/27/2013 - Fixed minor code sample issue.

License

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


Written By
Software Developer
United States United States
I'm a software developer and enjoy .net, angular, mvc, and many other different languages.

Comments and Discussions

 
QuestionHow can you specify a output file? Pin
jonelster21-Apr-21 10:27
jonelster21-Apr-21 10:27 
SuggestionGood post Pin
RodrigoVip728-Jan-20 1:34
RodrigoVip728-Jan-20 1:34 
PraiseIt is working Pin
Member 102515811-Jun-17 19:24
Member 102515811-Jun-17 19:24 
QuestionNot working when I host it into web server!!! Pin
Member 1312081911-Apr-17 0:51
Member 1312081911-Apr-17 0:51 
QuestionHow to print from specific page to page? Pin
Member 1301003122-Feb-17 0:59
Member 1301003122-Feb-17 0:59 
AnswerRe: How to print from specific page to page? Pin
David_Wimbley22-Feb-17 17:33
professionalDavid_Wimbley22-Feb-17 17:33 
QuestionThat's not working in a windows services Pin
sebisebo626-Apr-16 23:28
sebisebo626-Apr-16 23:28 
QuestionWay to kill adobe instance after print is done Pin
Anuj45929-Mar-16 1:31
Anuj45929-Mar-16 1:31 
QuestionIt would not close adobe - my solution included Pin
sewgttg18-Mar-16 12:01
sewgttg18-Mar-16 12:01 
QuestionLandscape Pin
blastnsmash12-Oct-15 13:29
blastnsmash12-Oct-15 13:29 
QuestionIt does not work on projects published in IIS Pin
RodrigoZotto10-Jun-15 4:04
RodrigoZotto10-Jun-15 4:04 
AnswerRe: It does not work on projects published in IIS Pin
David_Wimbley12-Jun-15 19:38
professionalDavid_Wimbley12-Jun-15 19:38 
SuggestionKilling Instance of Acrord32 Pin
Anuj45919-Jan-15 0:56
Anuj45919-Jan-15 0:56 
QuestionHow do I do in case of to choose the printer /t instead /p Pin
Alexssandro Rocha20-Nov-14 3:07
Alexssandro Rocha20-Nov-14 3:07 
QuestionHow do you wait for PDF to actually hit the print spooler/buffer before continuing code execution? Pin
Lance_Barrett21-Oct-14 6:17
Lance_Barrett21-Oct-14 6:17 
SuggestionBeware of silent printing with Adobe Reader. Pin
ZamirF13-Jun-14 8:05
ZamirF13-Jun-14 8:05 
QuestionCan't get any of these to work or even pop an Adobe Window Pin
Member 252639329-May-14 4:25
Member 252639329-May-14 4:25 
AnswerRe: Can't get any of these to work or even pop an Adobe Window Pin
David_Wimbley30-May-14 6:30
professionalDavid_Wimbley30-May-14 6:30 
SuggestionKill Adobe Reader based on proc.MainWindowTitle Pin
Chinyong Lim22-Mar-14 8:18
Chinyong Lim22-Mar-14 8:18 
QuestionAcrobat Window Popup? Pin
danteprax7-Mar-14 5:11
danteprax7-Mar-14 5:11 
QuestionHow to print PDF without Adobe Reader or Acrobat Pin
Mikhael-B15-Feb-14 21:59
Mikhael-B15-Feb-14 21:59 
AnswerRe: How to print PDF without Adobe Reader or Acrobat Pin
PabloInNz9-Mar-14 12:38
PabloInNz9-Mar-14 12:38 
QuestionSet the desired printer Pin
Member 86412729-Jan-14 18:51
Member 86412729-Jan-14 18:51 
GeneralMy vote of 5 Pin
Oleg A.Lukin27-May-13 19:22
Oleg A.Lukin27-May-13 19:22 
GeneralRe: My vote of 5 Pin
David_Wimbley28-May-13 3:27
professionalDavid_Wimbley28-May-13 3:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.