Some test text!

< Windows samples

Insert an Image into a PDF in VB

Sample VB code to use PDFTron SDK for programmatically inserting various raster image formats (e.g. TIFF, JPEG, JPEG2000, JBIG2, GIF, PNG, BMP, etc.) into a PDF document. Learn more about our PDF Editing & Manipulation Library.

Step 1: Get your free trial license key, or sign in

Start Trial
Sign in

Step 2: Add the code:

'
' Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
'

Imports System

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

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

    '-----------------------------------------------------------------------------------
    ' This sample illustrates how to embed various raster image formats 
    ' (e.g. TIFF, JPEG, JPEG2000, JBIG2, GIF, PNG, BMP, etc.) in a PDF document.
    '-----------------------------------------------------------------------------------
    Sub Main()

        PDFNet.Initialize()

        ' Relative path to the folder containing test files.
        Dim input_path As String = "../../../../TestFiles/"
        Dim output_path As String = "../../../../TestFiles/Output/"

        Try


            Using doc As PDFDoc = New PDFDoc
                Using bld As ElementBuilder = New ElementBuilder       ' Used to build new Element objects
                    Using writer As ElementWriter = New ElementWriter      ' Used to write Elements to the page	

                        Dim page As Page = doc.PageCreate()     ' Start a new page 
                        writer.Begin(page)    ' Begin writing to this page

                        ' ----------------------------------------------------------
                        ' Embed a JPEG image to the output document. 

                        Dim img As Image = Image.Create(doc.GetSDFDoc(), input_path + "peppers.jpg")

                        ' You can also directly add any .NET Bitmap. The following commented-out code 
                        ' is equivalent to the above line:
                        Dim bmp As System.Drawing.Bitmap
                        '   bmp = New System.Drawing.Bitmap(input_path + "peppers.jpg")
                        '   Dim img As Image = Image.Create(doc, bmp)

                        Dim element As Element = bld.CreateImage(img, New Matrix2D(200, 0, 0, 250, 50, 500))
                        writer.WritePlacedElement(element)

                        ' ----------------------------------------------------------
                        ' Add a PNG image to the output file
                        img = Image.Create(doc.GetSDFDoc(), input_path + "butterfly.png")
                        element = bld.CreateImage(img, New Matrix2D(img.GetImageWidth(), 0, 0, img.GetImageHeight(), 300, 500))
                        writer.WritePlacedElement(element)

                        ' ----------------------------------------------------------
                        ' Add a GIF image to the output file
                        bmp = New System.Drawing.Bitmap(input_path + "pdfnet.gif")
                        img = Image.Create(doc.GetSDFDoc(), bmp)
                        element = bld.CreateImage(img, New Matrix2D(bmp.Width, 0, 0, bmp.Height, 50, 350))
                        writer.WritePlacedElement(element)

                        ' ----------------------------------------------------------
                        ' Add a TIFF image to the output file
                        bmp = New System.Drawing.Bitmap(input_path + "grayscale.tif")
                        img = Image.Create(doc.GetSDFDoc(), bmp)
                        element = bld.CreateImage(img, New Matrix2D(bmp.Width, 0, 0, bmp.Height, 10, 50))
                        writer.WritePlacedElement(element)

                        ' ----------------------------------------------------------
                        ' Add a BMP image to the output file
                        bmp = New System.Drawing.Bitmap(input_path + "pdftron.bmp")
                        img = Image.Create(doc.GetSDFDoc(), bmp)
                        element = bld.CreateImage(img, New Matrix2D(bmp.Width, 0, 0, bmp.Height, 255, 700))
                        writer.WritePlacedElement(element)

                        writer.End()    ' Finish writing to the page
                        doc.PagePushBack(page)


                        ' ----------------------------------------------------------
                        ' Embed a multi-page TIFF to the output file

                        ' Create a new page 
                        page = doc.PageCreate(New Rect(0, 0, 612, 794))
                        writer.Begin(page)    ' Begin writing to the page

                        ' Embed the first TIFF page. Use JBIG2 Encoding

                        ' Use JBIG2 Encoding


                        Dim objset As ObjSet = New ObjSet
                        Dim jbig2_hint As Obj = objset.CreateName("JBIG2")

                        bmp = New System.Drawing.Bitmap(input_path + "multipage.tif")
                        img = Image.Create(doc.GetSDFDoc(), bmp, jbig2_hint)
                        element = bld.CreateImage(img, New Matrix2D(612, 0, 0, 794, 0, 0))
                        writer.WritePlacedElement(element)

                        writer.End()     ' Finish writing to the page
                        doc.PagePushBack(page)

                        ' Embed the second TIFF frame
                        page = doc.PageCreate()    ' Create a new page 
                        writer.Begin(page)   ' Begin writing to this page

                        ' Select the current TIFF page using SelectActiveFrame
                        bmp.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, 1)

                        img = Image.Create(doc.GetSDFDoc(), bmp, jbig2_hint)
                        element = bld.CreateImage(img, New Matrix2D(612, 0, 0, 794, 0, 0))
                        writer.WritePlacedElement(element)

                        writer.End()     ' save changes to the current page
                        doc.PagePushBack(page)

                        ' ----------------------------------------------------------
                        ' Add a JPEG2000 (JP2) image to the output file

                        ' Create a new page 
                        page = doc.PageCreate()
                        writer.Begin(page)    ' Begin writing to the page

                        ' Embed the image.
                        img = Image.Create(doc.GetSDFDoc(), input_path + "palm.jp2")

                        ' Position the image on the page.
                        element = bld.CreateImage(img, New Matrix2D(img.GetImageWidth(), 0, 0, img.GetImageHeight(), 96, 80))
                        writer.WritePlacedElement(element)

                        ' Write 'JPEG2000 Sample' text string under the image.
                        writer.WriteElement(bld.CreateTextBegin(Font.Create(doc, Font.StandardType1Font.e_times_roman), 32))
                        element = bld.CreateTextRun("JPEG2000 Sample")
                        element.SetTextMatrix(1, 0, 0, 1, 190, 30)
                        writer.WriteElement(element)
                        writer.WriteElement(bld.CreateTextEnd())

                        writer.End()    ' Finish writing to the page
                        doc.PagePushBack(page)

                        ' ----------------------------------------------------------
                        ' The following sample illustrates how to directly embed a JPEG 
                        ' image in PDF without transcoding.

                        page = doc.PageCreate()    ' Create a new page 
                        writer.Begin(page)   ' Begin writing to the page

                        bmp = New System.Drawing.Bitmap(input_path + "peppers.jpg")
                        Dim file As MappedFile = New MappedFile(input_path + "peppers.jpg")
                        Dim reader As FilterReader = New FilterReader(file)

                        img = Image.Create(doc.GetSDFDoc(), reader,
                            bmp.Width, bmp.Height, 8, ColorSpace.CreateDeviceRGB(),
                            Image.InputFilter.e_jpeg)
                        file.Close()

                        element = bld.CreateImage(img, New Matrix2D(bmp.Width * 1.4, 0, 0, bmp.Height * 1.4, 0, 0))
                        writer.WritePlacedElement(element)

                        writer.End()     ' save changes to the current page
                        doc.PagePushBack(page)

                    End Using
                End Using

                doc.Save(output_path + "addimage.pdf", SDF.SDFDoc.SaveOptions.e_linearized)
                Console.WriteLine("Done. Result saved in addimage.pdf...")
            End Using

		Catch e As PDFNetException
			Console.WriteLine(e.Message)
		End Try

    End Sub

End Module