Background Printing PDFs

A common task that developers attempt to do is print PDF files from code. Unfortunately, there is no simple and easy way of doing this. However, I found a work around using the Acrobat Type Library that can assist with this. It's by no means the best solution, but will allow you to perform background printing:


    Public Shared Function PrintPDF(ByVal Filename As String) As Boolean
        'Declare Variables
        Dim AcrobatApp As New AcroApp
        Dim AcrobatDoc As New AcroAVDoc
        Dim AcrobatPDFDoc As New AcroPDDoc
        Dim TotalPageCount As Integer
        Dim ResultsBoolean As Boolean
        Dim ReturnBoolean As Boolean = False

        Try
            'Attempt To Open The Report
            AcrobatDoc.Open(Filename, "")

            'Pass the Document to the PDF Handler
            AcrobatPDFDoc = AcrobatDoc.GetPDDoc

            'Grab the total number of pages
            TotalPageCount = AcrobatPDFDoc.GetNumPages

            'Perform a background print using Acrobat
            ResultsBoolean = AcrobatDoc.PrintPagesSilent(0, TotalPageCount - 1, 0, False, True)

            If ResultsBoolean = False Then
                Throw New Exception("Acrobat returned a False value when attempting to print the pages in the background.")
            Else
                ReturnBoolean = True
            End If
        Catch ex As Exception
            'Add In Exception Handling
        End Try

        Return ReturnBoolean
    End Function
Published Friday, May 19, 2006 5:25 PM by Jason N. Gaylord

Comments

# re: Background Printing PDFs

Thursday, May 31, 2007 7:10 AM by Mamtha

Thanks a lot. But When i excute my application, i am getting "COM object with CLSID {..} is either not valid or not registered".

And when i try to register this COM (Interop.Acrobat.dll) object with Regsrv32 again it throws an error  "Interop.Acrobat.dll was loaded, but the DllRegisterServer entry point was not found. This file cannot be registered."

Any suggestion will be highly appreciated.

Thanks

Mamtha

# re: Background Printing PDFs

Thursday, June 21, 2007 12:30 PM by Glitsun Cheeran

I also tested in the similar fashion you did and ended up with CLSID blah blah

The above code works only with Acrobat professional installed on your machine not the Reader .....!

Let me know any other way to achieve this gr8 stuff.

Thanks

Glitsun

# re: Background Printing PDFs

Monday, November 12, 2007 7:56 AM by Wilson Leung

Dear All,

I'm using C#, but I'm assuming you guys can port it over.

Simple and short answer:

Customise the Toolbox and add the Adobe Acrobat Reader ActiveX (.ocx) component. Drag one onto your window form and if you don't want it there, set its Visible = False

In code, (C#) you now have access to the AxAcroPDFLib reference.

DO NOT ADD the reference manually!!! Go Through the Toolbox!!!!

(I spent 5 hours wondering what was wrong <-- .net n00b), and the solution isn't clear on the internet

Wilson

# Printing PDF documents in C#

Monday, April 14, 2008 4:46 PM by Bite my bytes

Printing PDF documents in C#

# Printing PDF documents in C#

Monday, April 14, 2008 5:15 PM by Zunanji viri

I never though about it, but printing (not creating!) PDF documents from code without user intervention

# re: Background Printing PDFs

Friday, July 18, 2008 3:34 AM by Hotel key card printing

Wow,

That is really very amazing.

# re: Background Printing PDFs

Thursday, August 28, 2008 3:44 AM by Raj

Thanks Jason, but I could not get it working with Acrobat reader. So I googled and got another link here thomas-benny.blogspot.com/.../printing-pdfs-in-background-from-code.html.

The code is in c++, I had to port it to c# .

# re: Background Printing PDFs

Thursday, September 25, 2008 11:24 AM by Phil

Raj,

Would you mind sharing the c# port?

# re: Background Printing PDFs

Friday, October 24, 2008 1:07 PM by Stephen

ok if anyone is interested, here is how I battled this, I used the Interop.Acrobat and PdfSharp libraries to accomplish this in C#

PdfSharp.Pdf.PdfDocument rddoc = PdfSharp.Pdf.IO.PdfReader.Open(@"C:\file.pdf", PdfSharp.Pdf.IO.PdfDocumentOpenMode.InformationOnly);

int pgcount = rddoc.PageCount;

rddoc.Close();

Acrobat.AcroAVDocClass doc = new AcroAvDocClass();

doc.Open(@"C:\file.pdf");

doc.PrintPagesSilent(0, pgcount - 1, 0, 0, 1);

This works for me, I wrapped the above in a try block just to catch any unforseen errors, but this should work for you using C#

# re: Background Printing PDFs

Friday, November 28, 2008 3:05 AM by Olgunka-fa

<a href= aseeds.one.angelfire.com >transvestite rockstar</a>

# re: Background Printing PDFs

Friday, November 28, 2008 3:49 PM by Olgunka-xt

<a href= http://fairra.angelfire.com >landls end</a> <a href= http://vonucshka.angelfire.com >chancellor internal med</a>

# re: Background Printing PDFs

Friday, November 28, 2008 8:59 PM by Olgunka-zk

<a href= http://chkola.angelfire.com >avlastkey</a> <a href= http://bustersw.angelfire.com >how to start a strawberry patch in alabama</a>

# re: Background Printing PDFs

Tuesday, December 02, 2008 10:08 PM by Asina

<a href= bestpre.com ></a>

# re: Background Printing PDFs

Saturday, December 06, 2008 5:37 AM by Semil

<a href= spiritez.com ></a>

# re: Background Printing PDFs

Friday, December 26, 2008 9:09 AM by balabo3_ah

<a href= membres.lycos.fr/maffals >genetic disorters</a>

# re: Background Printing PDFs

Wednesday, December 31, 2008 6:41 PM by Aldo

I got semi-silent PDF printing using Acrobat Reader and DDE working, based on this post:

vidmar.net/.../printing-pdf-documents-in-c.aspx

This is my final code, I guess it could use some tweaking to make it more resilient, but I just got it working and moved on to fix other problems in my app.

private void printBlFile(string pdfFilePath)

{

   bool tryStart = false;

   bool connected = false;

   DdeClient client = null;

   do

   {

       try

       {

           client = new DdeClient("Acroview", "Control");

           client.Connect();

           connected = true;

       }

       catch (DdeException)

       {

           System.Diagnostics.Process p = new System.Diagnostics.Process();

           p.StartInfo.FileName = "AcroRd32.exe";

           p.Start();

           p.WaitForInputIdle();

           tryStart = !tryStart;

       }

       if (true == connected)

       {

           try

           {

               client.Execute("[DocOpen(\"" + pdfFilePath + "\")]", 60000);

               //client.Execute("[DocPrint(\"" + pdfFilePath + "\", 1, 1)]", 15000); // Not working...

               client.Execute("[FilePrintSilent(\"" + pdfFilePath + "\")]", 60000);

               client.Execute("[DocClose(\"" + pdfFilePath + "\")]", 15000);

               //client.Execute("[AppExit()]", 60000); // Don't want to close just yet...

               client.Disconnect();

           }

           catch

           {

               // Do something, maybe log to file or DB to register those BL pdf not found?

           }

       }

   }

   while (tryStart && !connected);

}

# re: Background Printing PDFs

Thursday, May 07, 2009 5:38 AM by WoAL

I described a method on how to fuly silent printing PDF files in the background, without the use of Adobe, but using gsprint executable in GhostScript/View...

You can read the post on netchallenge.wordpress.com/.../silent-printing-pdf-in-net-using-gsprint-and-additional-truetype-fonts

# re: Background Printing PDFs

Wednesday, June 24, 2009 11:04 PM by Adam Law

If I am priting a batch of PDF's silently I print to fineprint (www.fineprint.com).  This program collates all the prints allowing me to merge them.  It is the most silent way of printing that I have found with no delay.  I used ghostscript and adobe for a while but all the screen jumping was a nuisance.

# re: Background Printing PDFs

Thursday, August 06, 2009 7:41 AM by WoAl

gsprint doesn't pop up any screens and is able to fully silently print pdf files without any license purchasing required

# re: Background Printing PDFs

Saturday, May 15, 2010 6:00 AM by ink toner

witch type may i care our printers and cartridges

Leave a Comment

(required) 
(required) 
(optional)
(required)