MigraDoc: Visual Basic .NET Sample

Not many Visual Basic samples come with PDFsharp and MigraDoc. As the name PDFsharp implies, those libraries were developed by C# fans.
Here is a Visual Basic sample for MigraDoc. It should be enough to help you understand the C# samples for MigraDoc and translate them to Visual Basic when needed.

Add the MigraDoc library

The first step: add the MigraDoc library. I created a new console application because I do not need a user interface for this demo. But you can use any type of application.

Then I used NuGet to add MigraDoc. Right-click the application project in the Solution Explorer and select “Manage NuGet Packages…”. In the left window, select “Online” and make sure “nuget.org” is selected. Make sure it says “Include Prerelease” at the top of the dialogue window, otherwise click “Stable Only” and select “Include Prerelease”. Type “migradoc” into the Search field.
I selected “PDFsharp + MigraDoc (WPF)”. As of today the latest version was “1.50.3638-beta”, but newer versions will arrive from time to time.
You can also stick to “Stable Only” and get version 1.32.2608.

Import the namespaces

Import statements at the top of your module can make your code much shorter and easier to read and maintain.
Here are the Import statements I used:

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

Ready to use MigraDoc

One important hint: many MigraDoc methods will return an object. Sometimes you should store that object in a variable as it allows to call methods or change properties.
The Document is the root object of a MigraDoc document. It has sections and styles. The sections contain paragraphs and tables – and the paragraphs use the styles.

Here’s the first part of my code:

Dim myDoc As Document = New Document
Dim mySect As Section = myDoc.AddSection()

' Properties set for "Normal" will be inherited by all other styles.
Dim myStyle As Style = myDoc.Styles.Item(StyleNames.Normal)
myStyle.Font.Name = "Book Antiqua"

' The font set for "Heading1" will be inherited by all other headings.
myStyle = myDoc.Styles.Item(StyleNames.Heading1)
myStyle.Font.Name = "Arial"
myStyle.Font.Size = 16
myStyle.Font.Bold = True

Dim myPara As Paragraph = mySect.AddParagraph("My Heading")
myPara.Style = StyleNames.Heading1

Here I invoke a method without storing the return value. You need the return value if you want to set the style of the paragraph or add more text to it. You can call “AddFormattedText()” to add text with different attributes to a paragraph – this allows to mix regular, bold, italic text or different text sizes in a single paragraph.

 mySect.AddParagraph("Lorem ipsum dolor sit amet, consectetur adipiscing elit. " & _
                     "Donec sodales nunc quis vehicula faucibus. Fusce cursus " & _
                     "fermentum magna, id porttitor nisi feugiat sit amet. Nunc " & _
                     "ligula purus, porta ac ligula id, placerat tempus est. " & _
                     "Duis mollis venenatis libero, eget luctus risus accumsan et. " & _
                     "Donec finibus ultricies lacus porta auctor. Etiam tristique " & _
                     "urna sit amet faucibus placerat. Mauris ut est vel urna " & _
                     "sodales bibendum a bibendum nisi. In condimentum odio eu " & _
                     "quam pretium, et feugiat est accumsan. Aenean lacinia massa " & _
                     "quam, sit amet rhoncus mauris pulvinar sed. Nullam sed lacus " & _
                     "a nunc interdum dignissim sed at nunc. Interdum et malesuada " & _
                     "fames ac ante ipsum primis in faucibus. Duis a sodales ex. " & _
                     "Nulla leo est, laoreet nec est a, mollis maximus dui.")

The final stage for my document: now we create a PDF file from the MigraDoc document:

Dim myRenderer As PdfDocumentRenderer = New PdfDocumentRenderer(True, PdfFontEmbedding.Always)
myRenderer.Document = myDoc

Const filename As String = "Demo1.pdf"



Note: This sample was written by a C# fan. I am not familiar with VB naming conventions and such. I hope you still find the sample useful.

3 thoughts on “MigraDoc: Visual Basic .NET Sample”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.