Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing a PDF Silently with Adobe Acrobat

Tags:

c#

acrobat

I'm having 2 issues when trying to print a pdf silently in C# using adobe acrobat. I'm printing the pdfs using Process.Start().

The first issue is that I cannot launch Adobe Acrobat without specifying the full path to the executable. I assume it doesn't add it to your path when you install it. Is there an easy way to launch the newest version of acrobat on a machine without specifying full path names? I'm worried that the client is going to do an update and break my code that launches this. I'm also concerned with them installing this on machines with different versions of windows (install paths are different in 64 bit environment vs. 32 bit).

My second problem is the fact that whenever I launch acrobat and print it still leaves the acrobat window open. I thought that the command line parameters I was using would suppress all of this but apparently not.

I'm trying to launch adobe acrobat from the command line with the following syntax:

C:\Program Files (x86)\Adobe\Reader 10.0\Reader>AcroRd32.exe /t "Label.pdf" "HP4000" "HP LaserJet 4100 Series PCL6" "out.pdf"

It prints out fine but it still leaves the acrobat window up. Is there any other solution besides going out and killing the process programmatically?

like image 869
Cole W Avatar asked Feb 01 '11 23:02

Cole W


People also ask

How do I change Print settings in Adobe Acrobat?

Open the file in its authoring application, and choose File > Print. Choose Adobe PDF from the printers menu. Click the Properties (or Preferences) button to customize the Adobe PDF printer setting.

How do I make sure a PDF is Anonymous?

To check, open your PDF document in Adobe Acrobat. Choose >File; then >Document Properties. You should see information that indicates the "Author" of the document. If you wish to remain anonymous, delete your name and save the document.

What does flattening mean when printing?

If a document contains transparent objects, such as images with transparency, Acrobat flattens the document before printing it. Flattening removes transparency information and converts images to a format that the printer can interpret.


2 Answers

I ended up bailing on Adobe Acrobat here and going with FoxIt Reader (Free pdf reader) to do my pdf printing. This is the code I'm using to print via FoxIt in C#:

Process pdfProcess = new Process();
pdfProcess.StartInfo.FileName = @"C:\Program Files (x86)\Foxit Software\Foxit Reader\Foxit Reader.exe";
pdfProcess.StartInfo.Arguments = string.Format(@"-p {0}", fileNameToSave);
pdfProcess.Start();

The above code prints to the default printer but there are command line parameters you can use to specify file and printer. You can use the following syntax:

Foxit Reader.exe -t "pdf filename" "printer name"

Update:

Apparently earlier versions of acrobat do not have the problem outlined above either. If you use a much older version (4.x or something similar) it does not exhibit this problem.

Some printers do support native pdf printing as well so it's possible to send the raw pdf data to the printer and it might print it. See https://support.microsoft.com/en-us/kb/322091 for sending raw data to the printer.

Update 2

In later versions of our software we ended up using a paid product:

http://www.pdfprinting.net/

like image 98
Cole W Avatar answered Sep 19 '22 14:09

Cole W


Nick's answer looked good to me, so I translated it to c#. It works!

using System.Diagnostics;

namespace Whatever
{
static class pdfPrint
{
    public static void pdfTest(string pdfFileName)
    {
       string processFilename = Microsoft.Win32.Registry.LocalMachine
            .OpenSubKey("Software")
            .OpenSubKey("Microsoft")
            .OpenSubKey("Windows")
            .OpenSubKey("CurrentVersion")
            .OpenSubKey("App Paths")
            .OpenSubKey("AcroRd32.exe")
            .GetValue(String.Empty).ToString();

        ProcessStartInfo info = new ProcessStartInfo();
        info.Verb = "print";
        info.FileName = processFilename;
        info.Arguments = String.Format("/p /h {0}", pdfFileName);
        info.CreateNoWindow = true;
        info.WindowStyle = ProcessWindowStyle.Hidden; 
        //(It won't be hidden anyway... thanks Adobe!)
        info.UseShellExecute = false;

        Process p = Process.Start(info);
        p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

        int counter = 0;
        while (!p.HasExited)
        {
            System.Threading.Thread.Sleep(1000);
            counter += 1;
            if (counter == 5) break;
        }
        if (!p.HasExited)
        {
            p.CloseMainWindow();
            p.Kill();
        }
    }
}

}

like image 39
Phil in Seattle Avatar answered Sep 19 '22 14:09

Phil in Seattle