VB.NET PDF Printing

Posted on 2005-04-26
Last Modified: 2013-12-02
I'm trying to create a print application in VB.NET that reads a series of files in sequence and prints them to a network printer.  Intermixed between the PDF files I need to print various TIFs and text files.

Currently, I've managed to accomplish this by using RedMon ( ) with GhostScript to handle getting the PDFs to print.  The problem with this setup is that I send the jobs to the printer in two separate ways (one uses the PrintPage handler and the other copies the file using File.Copy) and so the sequence gets messed up unless I put in a sleep after each File.Copy.  Basically, this is how the print method of my class works now:

Public Function PrintJob() As Boolean
  PrintJob = True

    mPrinter.DocumentName = "AutoPrinter"
    If mDocType <> DocType.PDF Then

      File.Copy(mDocPath, mGSPrinterPath)

      REM Wait until file is done copying.

  Catch ex As Exception
    PrintJob = False

  End Try
End Function

The above code works but the delay makes it run very slowly (especially when there are 100+ docs to print). I've been trying to use the winspool.drv API to do the job but something isn't right.  I've created a local printer on the PC using a port mapped to the network printer ( as per ).  This successfully returns a numeric pointer to use with the OpenPrinter call but the program fails when it gets to creating the binary writer.  Is there anything I can do to get the following code to work or is there an alternative (the only caveat being that I cannot use anything that would require purchasing additional licenses) ?

In the class definition:
Public Declare Auto Function OpenPrinter Lib "winspool.drv" (ByVal pPrinterName As String, _
ByRef phPrinter As IntPtr, ByVal pDefault As PrinterDefaults) As Int32
Public Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As IntPtr) As Int32
Private Declare Function StartDocPrinter Lib "winspool.drv" Alias "StartDocPrinterA" (ByVal hPrinter As IntPtr, _
ByVal Level As Long, ByVal pDocInfo As DOCINFO) As Long
Private Declare Function StartPagePrinter Lib "winspool.drv" (ByVal hPrinter As IntPtr) As Long
Private Declare Function EndDocPrinter Lib "winspool.drv" (ByVal hPrinter As IntPtr) As Long
Private Declare Function EndPagePrinter Lib "winspool.drv" (ByVal hPrinter As IntPtr) As Long

Public Const STANDARD_RIGHTS_REQUIRED As Int32 = &HF0000
Public Const PRINTER_ACCESS_USE As Int32 = &H8

In place of the File.Copy:
REM Copy the file to the printer and resume operation once complete
Dim PrinterHandle As IntPtr
Dim PrinterDefs As New PrinterDefaults
PrinterDefs.permissions = PRINTER_ALL_ACCESS
OpenPrinter("GhostScript", PrinterHandle, PrinterDefs) ' mGhostScriptName
oBinaryReader = New BinaryReader(New FileStream(mBuffer(0), FileMode.Open))
oBinaryWriter = New BinaryWriter(New FileStream(PrinterHandle, FileAccess.Write))


Thanks in advance.
Question by:CoderNicx

    Author Comment

    A few additional things about my previous post:

    "GhostScript" above is the name of the local printer device name that points to the network location, it does return a value in PrinterHandle.

    mBuffer(0) is the full path to the PDF file (mDocPath in the original PrintJob Function)

    The exception message being generated at the New BinaryWriter position is that the intPtr is invalid (even though this is the same pointer being returned from the OpenPrinter call).
    LVL 28

    Expert Comment

    did you try this approach. You can print any file

           Dim p As New System.Diagnostics.ProcessStartInfo()
            p.Verb = "print"
            p.WindowStyle = ProcessWindowStyle.Hidden
            p.FileName = "C:\temp\UserGuide.pdf"
            p.UseShellExecute = True

    Author Comment

    I need the files to print in sync (the PrintPage handler must be called for the text files because I also do some processing on them) the only way to use the process method mentioned is to have it wait for the process to complete (I can do this by creating the process and calling the WaitForExit() method but...) this creates a problem because even with the ProcessWindowStyle.Hidden, Acrobat still opens up for each PDF being printed (I have Acrobat Reader 7). If the Acrobat process would close out of itself for each file this would be an applicable solution, but it waits for user interaction to close, which would require a user manually closing the window for each file.
    LVL 28

    Expert Comment

    you can change properties that Acrobat will not open during printing

    'Adobe PDF-Properties-General Tab-Printing Preferences
    'uncheck View Adobe PDF results

    Author Comment

    I'm unable to find this setting in Adobe Reader 7.  I looked through all the categories listed in Preferences, is this option in version 7?
    LVL 28

    Expert Comment

    go to Printers and right click on Adobe PDF and then go to Properties

    Author Comment

    Where is this 'Printers' I can't find it anywhere in Adobe Reader 7. Could you please give me the exact steps I need to get to this location (including which program it is that I'm looking for this option in)? Thanks
    LVL 28

    Accepted Solution

    GO to Start-Control Panel - Printers

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Power PDF ( is the newest product from the Document Imaging division of Nuance Communications ( It is available in two editions — …
    This article was inspired by a question here at Experts Exchange ( The requirements stated in that question are (1) reduce the file size of a large number of…
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
    Microsoft Office Picture Manager is not included in Office 2013. This comes as quite a surprise to users upgrading from earlier versions of Office, such as 2007 and 2010, where Picture Manager was included as a standard application. This video expla…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now