[VB.NET] Create PDF Document using PDFSharp & MigraDoc

Lately i help my friend develop an application for education purpose using VB.NET. Those application should have a function to print document to PDF files. After i am doing some research on google (where else could be? :D), i decide to use PDFSharp and Migradoc. There are some step to create PDF document using PDFSharp & Migradoc in VB.NET :

Imports MigraDoc.DocumentObjectModel
Imports MigraDoc.Rendering
Imports PdfSharp.Pdf

Public Class Form1
    Dim document As Document
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        document = CreateDocument(rtb.Text)
        PrintDocument(document, "Hell0.pdf")

    End Sub

    Sub PrintDocument(ByVal document As Document, ByVal fileName As String)
        Const unicode As Boolean = False
        Const embedding As PdfFontEmbedding = PdfFontEmbedding.Always

        Dim pdfRenderer As PdfDocumentRenderer = New PdfDocumentRenderer(unicode, embedding)
        pdfRenderer.Document = document

        pdfRenderer.RenderDocument()

        pdfRenderer.PdfDocument.Save(fileName)

        Process.Start(fileName)

    End Sub

    Function CreateDocument(ByVal text As String) As Document
        Dim document As Document = New Document()
        document.UseCmykColor = True
        Dim section As Section = document.AddSection

        Dim paragrapf As Paragraph = section.AddParagraph

        paragrapf.Format.Font.Color = Colors.Black

        paragrapf.AddFormattedText(text, TextFormat.Underline)

        Return document
    End Function
End Class

That’s it. I hope this post will be useful  someday:-)

By kifni41 on October 31, 2012 · Posted in Computer, Programming, VB.NET

1 Comment | Post Comment

Cendekiawan IT says:

wih, di kasih source code nya juga… keren dah

Posted on November 22nd, 2012