How to Generate PDF File in Visual Basic (VB.NET)

In this Vb.net tutorial, let’s create a simple desktop application in VB.net to generate pdf files.

How to Create PDF Files in VB.NET

Portable Document Format (PDF) is a file format that represents all the properties of a printed document that we can read, write print and forward to someone else. We can create PDF files with the help of programming from our VB.NET Applications easily. PDFsharp is the Open Source library that easily creates and allows us to create PDF files directly from our VB.Net Applications.

we can freely download the Assemblies version from the following link: Download PDFsharp Assemblies-1_31.zip

Follow the simple steps for creating a PDF file by code in VB.net.

Step 1:

  •  Download the Required Assemblies from the above-mentioned URL.
  • Extract the .zip file into your System (filename :PDFsharp-MigraDocFoundation-Assemblies-1_31.zip)

Step 2:

Open your Visual Studio > Go to File Menu > Create a New Project > Select n Visual Basic Windows Application> Name the Project then Click on OK.

Step 3:

  • Now Add the  PDFsharp Assemblies in your VB.Net Project
  •  Go to Solution Explorer, then right-click on the project name and click on Add Reference. Here we are using GDI+ libraries.

  • Add Reference dialogue box will appear, Click on the Browse Tab.

  • choose the Assemblies File in Look In textbox where you had saved it.
  • Select all files and Click on OK.
  • After adding the References your Solution Explorer will look something like this. Now your project is ready to add code.

Step 4:

Import the following Namespaces on top of the Form1.vb.

Imports PdfSharp
Imports PdfSharp.Drawing
Imports PdfSharp.Pdf

Step 5:

Add one button control from toolbox. Name it Show PDF. Double click on the button and add the below code there.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
Dim pdf As PdfDocument = New PdfDocument
 
pdf.Info.Title = "My First PDF"
 
Dim pdfPage As PdfPage = pdf.AddPage
 
Dim graph As XGraphics = XGraphics.FromPdfPage(pdfPage)
 
Dim font As XFont = New XFont("Verdana", 20, XFontStyle.Bold)
 
graph.DrawString("Hello, This is my first PDF document", font, XBrushes.Black, New XRect(0, 0, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.Center)
 
Dim pdfFilename As String = "firstpage.pdf"
 
pdf.Save(pdfFilename)
 
Process.Start(pdfFilename)
 
End Sub

Our full code will look like this.

Imports PdfSharp
Imports PdfSharp.Drawing
Imports PdfSharp.Pdf

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim pdf As PdfDocument = New PdfDocument
pdf.Info.Title = "My First PDF"
Dim pdfPage As PdfPage = pdf.AddPage
Dim graph As XGraphics = XGraphics.FromPdfPage(pdfPage)
Dim font As XFont = New XFont("Verdana", 20, XFontStyle.Bold)
graph.DrawString("Hello, This is my first PDF document", font, XBrushes.Black, New XRect(0, 0, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.Center)
Dim pdfFilename As String = "firstpage.pdf"
pdf.Save(pdfFilename)
Process.Start(pdfFilename)
End Sub
End Class

Step  6:

Now Press F5  and run the Application.

Output:

First the Form1 will show to us here when we click on the Show PDF button.

The PDF file will generate and save with name and formatting which we have given in the program. and the PDF document will be displayed to us as shown below.

you can set any name and formatting as you want and also add the text as per your choice.

So with the help of these steps, you can also create the PDF file in VB.NET.