#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”

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")

        ''//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
                    Doc.Open()

                    ''//Insert a blank page
                    Doc.NewPage()

                    ''//Add a simple paragraph with text
                    Doc.Add(New Paragraph("Hello World"))

                    ''//Close our document
                    Doc.Close()
                End Using
            End Using
        End Using

        Me.Close()
    End Sub
End Class

  • A very excellent teach, thanks