Some test text!

Discord Logo

Chat with us

PDFTron is now Apryse, learn more here.

Print PDF a file in VB

More languages

More languages
C# (.NET Core)

Sample VB code for using PDFTron SDK to print a PDF file using the currently selected default printer. It is possible to use this printing functionality in both client and server applications without dependence on any third party components. Learn more about our VB PDF Library.

Get Started Samples Download

To run this sample, get started with a free trial of Apryse SDK.

' Copyright (c) 2001-2021 by PDFTron Systems Inc. All Rights Reserved.
Imports System
Imports System.Drawing
Imports System.Drawing.Printing

Imports pdftron
Imports pdftron.Common
Imports pdftron.Filters
Imports pdftron.SDF
Imports pdftron.PDF

' The following sample illustrates how to print PDF document using currently selected
' default printer. 
' The first example uses the new PDF::Print::StartPrintJob function to send a rasterization 
' of the document with optimal compression to the printer.  If the OS is Windows 7, then the
' XPS print path will be used to preserve vector quality.' 
' The second example uses PDFDraw send unoptimized rasterized data. 
' If you would like to rasterize page at high resolutions (e.g. more than 600 DPI), you 
' should use PDFRasterizer or PDFNet vector output instead of PDFDraw. 

Namespace PDFViewSimpleTestVB
    Module PDFViewSimpleTestVB
        Dim pdfNetLoader As PDFNetLoader
        Sub New()
            pdfNetLoader = pdftron.PDFNetLoader.Instance()
        End Sub

        Sub Main()
            Dim driver As PDFPrint = New PDFPrint
        End Sub
    End Module
End Namespace

Public Class PDFPrint
    Private doc As PDFDoc = Nothing
    Private pdfdraw As PDFDraw = Nothing
    Private pageitr As PageIterator = Nothing

    Sub Execute()

        ' Optional: Set ICC color profiles to fine tune color conversion 
        ' for PDF 'device' color spaces. You can use your own ICC profiles. 
        ' Standard Adobe color profiles can be download from Adobes site: 
        ' Simply drop all *.icc files in PDFNet resource folder or you specify 
        ' the full pathname.
        ' Try
        ' PDFNet.SetColorManagement()
        ' PDFNet.SetDefaultDeviceCMYKProfile("USWebCoatedSWOP.icc") ' will search in PDFNet resource folder.
        ' PDFNet.SetDefaultDeviceRGBProfile("AdobeRGB1998.icc") 

        ' Optional: Set predefined font mappings to override default font 
        ' substitution for documents with missing fonts. For example:
        ' PDFNet.AddFontSubst("StoneSans-Semibold", "C:/WINDOWS/Fonts/comic.ttf")
        ' PDFNet.AddFontSubst("StoneSans", "comic.ttf")  ' search for 'comic.ttf' in PDFNet resource folder.
        ' PDFNet.AddFontSubst(PDFNet.CharacterOrdering.e_Identity, "C:/WINDOWS/Fonts/arialuni.ttf")
        ' PDFNet.AddFontSubst(PDFNet.CharacterOrdering.e_Japan1, "C:/Program Files/Adobe/Acrobat 7.0/Resource/CIDFont/KozMinProVI-Regular.otf")
        ' PDFNet.AddFontSubst(PDFNet.CharacterOrdering.e_Japan2, "c:/myfonts/KozMinProVI-Regular.otf")
        ' If fonts are in PDFNet resource folder, it is not necessary to specify 
        ' the full path name. For example,
        ' PDFNet.AddFontSubst(PDFNet.CharacterOrdering.e_Korea1, "AdobeMyungjoStd-Medium.otf")
        ' PDFNet.AddFontSubst(PDFNet.CharacterOrdering.e_CNS1, "AdobeSongStd-Light.otf")
        ' PDFNet.AddFontSubst(PDFNet.CharacterOrdering.e_GB1, "AdobeMingStd-Light.otf")
        ' Catch e As PDFNetException
        '   Console.WriteLine(e.Message)
        ' End Try

        ' Relative path to the folder containing test files.
        Dim input_path As String = "../../../../TestFiles/"
            ' Open the PDF document.
            Console.WriteLine("Opening the input file...")
            doc = New PDFDoc(input_path + "tiger.pdf")

            ' Example 1: use the PDF::Print::StartPrintJob interface
            ' This is silent (no progress dialog) and blocks until print job is at spooler
            ' The rasterized print job is compressed before sending to printer
            Console.WriteLine("Printing the input file using PDF.Print.StartPrintJob...")

            ' While you can get the name of the default printer by using:
            ' Dim ps As PrinterSettings = New PrinterSettings
            ' Dim printerName As String = ps.PrinterName()
            ' Print.StartPrintJob can also determine this for you, just pass an empty printer name

            ' To setup printing options:
            Dim printerMode As PrinterMode = New PrinterMode
            printerMode.SetDPI(300)     ' regardless of ordering, an explicit DPI setting overrides the OutputQuality setting
            Dim pagesToPrint As PageSet = New PageSet(1, doc.GetPageCount(), PageSet.Filter.e_all)

            ' Print the document on the default printer, name the print job the name of the 
            ' file, print to the printer not a file, and use default printer options:
            Print.StartPrintJob(doc, "", doc.GetFileName(), "", pagesToPrint, printerMode, Nothing)

            ' Example 2: use the .Net PrintDocument class and PDFDraw rasterizer
            ' This will pop up a progress dialog

            ' Start printing from the first page
            pageitr = doc.GetPageIterator()

            pdfdraw = New PDFDraw

            ' Create a printer
            Dim printer As PrintDocument = New PrintDocument

            ' Set the PrintPage delegate which will be invoked to print each page
            AddHandler printer.PrintPage, AddressOf PrintPage
            printer.Print()          ' Start printing

            pdfdraw.Dispose()            ' Free allocated resources (generally a good idea when printing many documents).
            doc.Close()           ' Close the file
        Catch e As PDFNetException
        End Try
    End Sub

    Private Sub PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
        Dim gr As Graphics = ev.Graphics
        gr.PageUnit = GraphicsUnit.Inch
        Dim rectPage As Rectangle = ev.PageBounds

        Dim dpi As Single = gr.DpiX
        If dpi > 300 Then dpi = 300

        Dim hardMarginX As Integer = 0
        Dim hardMarginY As Integer = 0
        Dim left As Double = (rectPage.Left - hardMarginX) / 100
        Dim right As Double = (rectPage.Right - hardMarginX) / 100
        Dim top As Double = (rectPage.Top - hardMarginY) / 100
        Dim bottom As Double = (rectPage.Bottom - hardMarginY) / 100
        Dim rect As pdftron.PDF.Rect = New Rect(left * 72, bottom * 72, right * 72, top * 72)

            pdfdraw.DrawInRect(pageitr.Current, gr, rect)
        Catch ex As Exception
            Console.WriteLine("Printing Error: " + ex.ToString)
        End Try

        ev.HasMorePages = pageitr.HasNext()
    End Sub
End Class