VB.NET PDF Printing

Posted on 2005-04-26
Medium Priority
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 ( http://www.cs.wisc.edu/~ghost/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 http://support.microsoft.com/kb/q230743/ ).  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
  • 4
  • 4

Author Comment

ID: 13873647
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

ID: 13880046
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

ID: 13881163
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.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 28

Expert Comment

ID: 13885040
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

ID: 13891355
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

ID: 13894293
go to Printers and right click on Adobe PDF and then go to Properties

Author Comment

ID: 13912652
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

iboutchkine earned 2000 total points
ID: 13916865
GO to Start-Control Panel - Printers

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I. Introduction In a previous article (http://www.experts-exchange.com/Web_Development/Document_Imaging/A_6537-PaperPort-Upgrade-How-to-download-and-install-updated-versions-of-PaperPort-11-and-12.html) (now deprecated), I discussed how to upgrad…
PaperPort has a feature called the "Send To Bar". It provides a convenient, drag-and-drop interface for using other installed software, such as Microsoft Office. However, this article shows that the latest Office 2016 apps (installed with an Office …
This video Micro Tutorial is the first in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 (http://www.experts-exchange.com/articles/17490/). But the ability to create custom scanning profiles al…
This video Micro Tutorial is the second in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 (http://www.experts-exchange.com/articles/17490/). But the ability to create custom scanning profiles a…
Suggested Courses

850 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