Net-informations.com
SiteMap  | About    

How to create PDF files in vb.net

pdf - Portable Document Format

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.

Add reference to C# projects

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

pdf assembly files

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.

visual studio solution explorer

Now the project is ready to start coding

First step you should Imports the necessary namespaces.

Imports PdfSharp Imports PdfSharp.Drawing Imports PdfSharp.Pdf

Create a PDF document Object

Dim pdf As PdfDocument = New PdfDocument

Next step is to create a an Empty page.

Dim pdfPage As PdfPage = pdf.AddPage

Then create an XGraphics Object

Dim graph As XGraphics = XGraphics.FromPdfPage(pdfPage)

Also create the Font object from XFont

Dim font As XFont = New XFont("Verdana", 20, XFontStyle.Bold)

After that you can add content to the pdf file

graph.DrawString("This is my first PDF document", font, XBrushes.Black, _ New XRect(0, 0, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft)

XStringFormats.Center will place the your content to the center of the PDF page.

Save the document as .pdf at your desired location

pdf.Save("firstpage.pdf");

After save the file , you can double click and open the pdf file. Then you can see the following content in your pdf file.

created 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




net-informations.com (C) 2016    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.