#2 – VB.Net iTextSharp Tutorial – Add an 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.

<br />
Option Explicit On<br />
Option Strict On</p>
<p>Imports System.IO<br />
Imports iTextSharp.text<br />
Imports iTextSharp.text.pdf</p>
<p>Public Class Form1<br />
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load<br />
        ''//The main folder that we are working in<br />
        Dim WorkingFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)</p>
<p>        ''//The file that we are creating<br />
        Dim WorkingFile = Path.Combine(WorkingFolder, &quot;Output.pdf&quot;)</p>
<p>        Dim SampleImage = Path.Combine(WorkingFolder, &quot;IMG_0259.JPG&quot;)</p>
<p>        ''//Create our file with an exclusive writer lock<br />
        Using FS As New FileStream(WorkingFile, FileMode.Create, FileAccess.Write, FileShare.None)<br />
            ''//Create our PDF document<br />
            Using Doc As New Document(PageSize.LETTER)<br />
                ''//Bind our PDF object to the physical file using a PdfWriter<br />
                Using Writer = PdfWriter.GetInstance(Doc, FS)<br />
                    ''//Open our document for writing<br />
                    Doc.Open()</p>
<p>                    ''//Insert a blank page<br />
                    Doc.NewPage()</p>
<p>                    ''//Add an image to a document. This does not scale the image or anything so if your image is large it might go off the canvas<br />
                    Doc.Add(iTextSharp.text.Image.GetInstance(SampleImage))</p>
<p>                    ''//Close our document<br />
                    Doc.Close()<br />
                End Using<br />
            End Using<br />
        End Using</p>
<p>        Me.Close()<br />
    End Sub<br />
End Class<br />