#3 – VB.Net iTextSharp Tutorial – Add a scaled image to a document

This is part of a series of iTextSharp tutorials for VB 2010 Express. See this post for an overview and to answer any basic questions that you may have.

This post is a followup to the previous one, this time it scales the image based on the document’s size

Option Explicit On
Option Strict On

Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf

Public Class Form1
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        ''//The main folder that we are working in
        Dim WorkingFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)

        ''//The file that we are creating
        Dim WorkingFile = Path.Combine(WorkingFolder, "Output.pdf")

        Dim SampleImage = Path.Combine(WorkingFolder, "IMG_5605.JPG")

        ''//Create our file with an exclusive writer lock
        Using FS As New FileStream(WorkingFile, FileMode.Create, FileAccess.Write, FileShare.None)
            ''//Create our PDF document
            Using Doc As New Document(PageSize.LETTER)
                ''//Bind our PDF object to the physical file using a PdfWriter
                Using Writer = PdfWriter.GetInstance(Doc, FS)
                    ''//Open our document for writing

                    ''//Insert a blank page

                    ''//Create a PDF image object from our physical image
                    Dim ThisImage = iTextSharp.text.Image.GetInstance(SampleImage)

                    ''//Use standard ratio resizing algorithms to calculate new image dimensions based on the documents dimensions. This will shrink or grow documents to fit

                    ''//Will hold our new image dimensions
                    ''//Documents sometimes have margins (and this sample does) so subtract them so that our image in centered in the page
                    Dim NewW, NewH As Single

                    NewW = Doc.PageSize.Width - (Doc.LeftMargin + Doc.RightMargin)
                    NewH = Doc.PageSize.Height - (Doc.TopMargin + Doc.BottomMargin)

                    ''//Scale the image
                    ThisImage.ScaleToFit(NewW, NewH)

                    ''//Add the image to the document

                    ''//Close our document
                End Using
            End Using
        End Using

    End Sub
End Class

3 thoughts on “#3 – VB.Net iTextSharp Tutorial – Add a scaled image to a document

  1. Hi Chris,

    I don’t get:

    NewW = Doc.PageSize.Width
    NewH = (Doc.PageSize.Height * NewW) / Doc.PageSize.Width

    Doesn’t that equate to:
    NewH = Doc.PageSize.Height

    • You’re right! The calculations should have used the image’s height/width. And actually, I updated the code above to remove those calculations completely and just use the ScaleToFit method which does all the work for us!

  2. Hey I have a question I have a loop going on here in this example below, yet when it gets to the first record in the loop of the list box, it bombs out. Any Idea why?

    For i = 0 To lboxconv.Items.Count – 1
    Dim extension = Microsoft.VisualBasic.Right(i, 3)
    Dim doc As New Document()
    Dim File_Type As String
    File_Type = Replace(lboxconv.Items(i), “.png”, “.pdf”, vbTextCompare)
    Dim writer As PdfWriter = PdfWriter.GetInstance(doc, New FileStream(File_Type, FileMode.Create))
    Dim imgpath As String = lboxconv.Items(i)
    Dim png As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(imgpath)
    png.ScaleToFit(“500”, “500”)
    png.Alignment = Element.ALIGN_CENTER

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.