What is the easiest way to print pdf files using Visual Basic 2005? I have some experience programming in VB 6 but I'm haven’t' been using 2005 very long so I’m still quite ignorant. Thanks for any help I can get. If someone could point me in the right direction I sure would appreciate it.

What is the easiest way to print pdf files using Visual Basic 2005? I have some experience programming in VB 6 but I'm haven’t' been using 2005 very long so I’m still quite ignorant. Thanks for any help I can get. If someone could point me in the right direction I sure would appreciate it.

You know I had fought with this too. The simpliest way to print is to simply shell out to the reader and have it print it; however, the directory for acrord32.exe changes from version to version. The next most complicated way, in my opinion, is to spawn off a thread and have it do it.

EXAMPLE
Module modPrintPDF

    Public Function PrintPDF(ByVal PDFFile As String, ByVal Printer As String, ByVal Timeout As Integer) As Integer

        If PrinterName.Trim.Length = 0 Then
            PrinterName = (New System.Drawing.Printing.PrinterSettings).PrinterName
        End If

        Dim Proc As New System.Diagnostics.Process

        Proc.EnableRaisingEvents = True
        Proc.StartInfo.FileName = PDFFile
        Proc.StartInfo.Arguments = Chr(34) + PrinterName + Chr(34)
        Proc.StartInfo.Verb = "PrintTo"
        Proc.StartInfo.WindowStyle = ProcessWindowStyle.Minimized
        Proc.StartInfo.CreateNoWindow = True
        Proc.Start()

        Do While Timeout > 0 AndAlso Not Proc.HasExited
            System.Threading.Thread.Sleep(1000)
            Timeout -= 1
        Loop

        If Not Proc.HasExited Then
            Debug.Print("Killing process")
            Proc.Kill()
        End If

        Debug.WriteLine("Closing process")
        Proc.Close()

        Return 0
    End Function

    Public Function PrintPDF(ByVal PDFFile As String) As Integer
        Return PrintPDF(PDFFile, "", 60)
    End Function
    Public Function PrintPDF(ByVal PDFFile As String, ByVal Timeout As Integer) As Integer
        Return PrintPDF(PDFFile, "", Timeout)
    End Function
End Module

This will launch reader (or whatever is the default acrobat reader is) and ask it to print the PDF. It will wait for the timeout period for the reader to terminate and if it has not closed itsself, then I force it closed.

The following applies to Adobe's Acrobate reader and not necessarily anyone elses PDF reader.
Adobe's reader does not close itself (atleast version 8 & 9 don't) when its done printing. It also does not place nice when you say that the window should be hidden or even minimized. It comes right up in a normal window and displays the PDF and prints. On some machines a timeout of 30 seconds is plenty, some older machines with low memory can take a minute or more (sad but true). A nice trick would be to leave acrobate open only long enough to print the pdf then close it. With the incorperation of an additional library from Merrion Computing called The Printer Monitor you can tell when the job is ready to be printed and you can use as a signal to shut down acrobat. As a bonus, the module is free.

EXAMPLE
Imports PrinterQueueWatch

Module modPrintPDF

    Public Function PrintPDF(ByVal PDFFile As String, ByVal Printer As String, ByVal Timeout As Integer) As Integer

        If PrinterName.Trim.Length = 0 Then
            PrinterName = (New System.Drawing.Printing.PrinterSettings).PrinterName
        End If

        Dim Proc AS New System.Diagnostics.Process

        Proc.EnableRaisingEvents = True
        Proc.StartInfo.FileName = PDFFile
        Proc.StartInfo.Arguments = Chr(34) + PrinterName + Chr(34)
        Proc.StartInfo.Verb = "PrintTo"
        Proc.StartInfo.WindowStyle = ProcessWindowStyle.Minimized
        Proc.StartInfo.CreateNoWindow = True

        ' Now start monitoring the printer's queue.
        Dim Monitor As New PrinterQueueWatch.PrinterMonitorComponent
        Monitor.MonitorJobAddedEvent = False
        Monitor.MonitorJobDeletedEvent = False
        Monitor.MonitorJobSetEvent = True
        Monitor.MonitorJobWrittenEvent = False
        Monitor.MonitorPrinterChangeEvent = False
        AddHandler Monitor.JobSet, AddressOf SetPrintJob
        Monitor.DeviceName = PrinterName

        ' Now that I am watching, start up Acrobat.
        Proc.Start()

        Do While Timeout > 0 AndAlso Not Proc.HasExited
            System.Threading.Thread.Sleep(1000)
            Timeout -= 1
        Loop

        ' Stop monitoring.
        RemoveHandler Monitor.JobSet, AddressOf SetPrintJob
        Monitor = Nothing

        If Not Proc.HasExited Then
            Debug.Print("Killing process")
            Proc.Kill()
        End If

        Debug.WriteLine("Closing process")
        Proc.Close()

        Return 0
    End Function
    Public Function PrintPDF(ByVal PDFFile As String) As Integer
        Return PrintPDF(PDFFile, "", 60)
    End Function
    Public Function PrintPDF(ByVal PDFFile As String, ByVal Timeout As Integer) As Integer
        Return PrintPDF(PDFFile, "", Timeout)
    End Function

    Private Sub SetPrintJob(ByVal sender As Object, ByVal e As System.EventArgs)
        ' Print job is set to print.
        System.Threading.Thread.Sleep(500)
        Proc.Kill()
    End Sub
End Module

This will launch acrobat (or currently registered PDF program) and instruct it to print. It then waits the timeout period or until it finds out that the job has been submitted, then it kills the process and exists.

Now there is a flaw in the logic here. If you are running multiple applications with multiple print jobs and another application adds a print job while you are waiting, then the program closes acrobat prematurely. The printer monitor componant does allow you to do some inspection of the print job and you can use that to differentiate your print job from another application's.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.