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
REM Wait until file is done copying.
Catch ex As Exception
PrintJob = False
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 PRINTER_ACCESS_ADMINISTER As Int32 = &H4
Public Const STANDARD_RIGHTS_REQUIRED As Int32 = &HF0000
Public Const PRINTER_ACCESS_USE As Int32 = &H8
Public Const PRINTER_ALL_ACCESS As Int32 = STANDARD_RIGHTS_REQUIRED Or _
PRINTER_ACCESS_USE Or PRINTER_ACCESS_ADMINISTER
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
PrinterHandle, PrinterDefs) ' mGhostScriptName
oBinaryReader = New BinaryReader(New FileStream(mBuffer(0), FileMode.Open))
oBinaryWriter = New BinaryWriter(New FileStream(PrinterHandle, FileAccess.Write))
Thanks in advance.