How to create PDF files in vb.net

Portable Document Format (PDF) is a file format that represents all the characteristics of a printed document that you can read, write and print or forward to someone else. Each PDF file included a complete description of a fixed-layout flat document, including the text, fonts, graphics, and other information needed to view it. You can create PDF file programmatically from your VB.Net applications very easily. PDFsharp is the Open Source library that easily creates PDF documents from your VB.Net applications. PDFSharp library allows you to create PDF files directly from your VB.Net application.
You can freely download the Assemblies version from the following link: Download PDFsharp Assemblies
The following steps will guide you how to create a pdf file programmatically
1. Download the Assemblies from the above mentioned url.
2. Extract the .zip file to your desired location (filename :PDFsharp-MigraDocFoundation-Assemblies-1_31.zip)
3. Create a New VB.Net Project
4. Add pdfsharp Assemblies in VB.Net Project
5. In Solution Explorer, right-click the project node and click Add Reference. In this project we are using GDI+ libraries.

6. In the Add Reference dialog box, select the BROWSE tab and select the Assebly file location (step 2).

7. Select all files and click OK
After you add the reference files to your VB.Net project , solution explorer look like the following picture.

Now the project is ready to start coding
First step you should Imports the necessary namespaces.
Create a PDF document Object
Next step is to create a an Empty page.
Then create an XGraphics Object
Also create the Font object from XFont
After that you can add content to the pdf file
XStringFormats.Center will place the your content to the center of the PDF page.
Save the document as .pdf at your desired location
After save the file , you can double click and open the pdf file. Then you can see the following content in your pdf file.

Drag a Button on the Form and copy and paste the following code in the button1_Click event.
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("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
- How to VB.NET Directory operations
- How to VB.NET File operations
- How to VB.NET FileStream
- How to VB.NET TextReader
- How to VB.NET Simple TextReader
- How to VB.NET TextWriter
- How to VB.NET BinaryReader
- How to VB.NET BinaryWriter
- How to convert xps file to bmp file
- How to VB.Net Path Class
- How to convert text file to pdf
- Write database to PDF file