VB.Net Print PDF Document

Portable Document Format (PDF) is a file format used to present documents in a manner independent of application software, hardware, and operating systems. Each PDF file encapsulates a complete description of a fixed-layout flat document, including the text, fonts, graphics, and other information needed to display it.

In some applications, web or desktop there are several options where you can convert or Print a PDF version of a form or document you're reading. So, the question is how is it done and is it possible to create your own PDF document from scratch? We'll you're still lucky as I am.

In this tutorial you'll be able to learn on how can VB.Net Print PDF Documents.

Requirements

1.) CodePlex.com provides a freely downloadable version of the PDFSharp-MigraDocFoundation-Assemblies.
2.) Extract the downloaded .zip file to your project folder. The file name if not updated is PDFsharp-MigraDocFoundation-Assemblies-1_31.zip.


Creating the Project

1.) Start you Microsoft Visual Basic 2010 Express.
2.) Create a new Windows Application Project.
3.) In the Solution Explorer Panel, Right Click on your Project's Name. Then choose Add Reference.

4.) In the Add Reference dialog window, Click the Browse Tab. Locate the folder where you extracted the PDFSharp Assemblies.
5.)Select All of the .dll files then Click OK.

Once the done, Click the small arrow to expand the references in Solution Explorer Panel and it will most likely look as the image below.


Design Mode

Add a Button Control on the form.

Coding your Project

Double Click the button control then Copy and paste the following codes in your Code Editor.
Imports PdfSharp
Imports PdfSharp.Drawing
Imports PdfSharp.Pdf

Public Class Form1
    Private Sub Button1_Click_1(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 Document"
        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("You've just created your first PDF document.", font, XBrushes.Black, _
        New XRect(0, 0, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.Center)
        Dim pdfFileName as String = "C:\MyFirstPdfDocument.pdf"
        pdf.Save(pdfFileName)
        Process.Start(pdfFileName)
    End Sub
End Class

Explaining each Line of Codes

Import the required namespaces.
        Imports PdfSharp
        Imports PdfSharp.Drawing
        Imports PdfSharp.Pdf
Create a PDF document object.
        Dim pdf As PdfDocument = New PdfDocument
Create a new page.
        Dim pdfPage As PdfPage = pdf.AddPage
Set the title of the PDF document.
        pdf.Info.Title = "My First PDF Document"
Create a Graphics Object.
        Dim graph As XGraphics = XGraphics.FromPdfPage(pdfPage)
Create and Set a font style from XFont.
        Dim font As XFont = New XFont("Tahoma", 24, XFontStyle.Bold)
Adding content to PDF document and Add it to Center.
        graph.DrawString("Congratulations! You've just created your first PDF document.", font, XBrushes.Black, _
        New XRect(0, 0, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.Center)
Save the PDF Document at your most preferred location.
        Dim pdfFileName as String = "C:\MyFirstPdfDocument.pdf"
        pdf.Save(pdfFileName)
Launch the newly create PDF document.
        Process.Start(pdfFileName)

PDF Output

When you click the button, it will create the pdf page and open the pdf file. As you can see below.

Now, you've just learned how to create your first pdf document. Download the VB.Net Print PDF Project.

Comments

Post a Comment

Popular posts from this blog

How to Create a Configuration.INI Files in VB6

How to Set Windows Form Always on Top of Other Applications in VB6

How to Minimize/Send an App Icon to System Tray in Visual Basic 6