#1 – VB.Net iTextSharp Tutorial – Hello World

This is the first in the 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 is the starter, the “hello world” program done in VB.Net. The comments in the code should hopefully be enough to explain what’s going on, but after running (and it should run fast, just opens and closes), you should have a PDF on your desktop called “Output.pdf”

<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>        ''//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 />
<p>                    ''//Insert a blank page<br />
<p>                    ''//Add a simple paragraph with text<br />
                    Doc.Add(New Paragraph(&quot;Hello World&quot;))</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</p>