PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.com/

VB.NET: A MigraDoc Sample
http://forum.pdfsharp.com/viewtopic.php?f=8&t=3207
Page 1 of 1

Author:  TH-Soft [ Sat Oct 03, 2015 8:04 pm ]
Post subject:  VB.NET: A MigraDoc 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:

Code:
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:

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.

Code:
 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:

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

Const filename As String = "Demo1.pdf"

myRenderer.PdfDocument.Save(filename)

Process.Start(filename)


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.

Author:  TH-Soft [ Sat Oct 03, 2015 8:22 pm ]
Post subject:  Re: VB.NET: A MigraDoc Sample

And here is the revisited VB.NET sample, this time using MigraDoc Made EZR.

The Import statements:
Code:
Imports MigraDoc.DocumentObjectModel
Imports MigraDocMadeEZ


And sub Main():
Code:
Sub Main()
    Dim mez As MigraDocMadeEZR = New MigraDocMadeEZR

    mez.InfoAuthor = "TH Software"
    mez.InfoTitle = "Demo Document"
    mez.InfoComment = "Demontration of creating PDF files using MigraDoc Made EZR and VB.NET."
    mez.InfoKeywords = "Demontration of creating PDF files using MigraDoc Made EZR and VB.NET"
    mez.InfoSubject = "Demo Document"

    ' Properties set for "Normal" will be inherited by all other styles.
    mez.Style(StyleNames.Normal).Font("Book Antiqua")

    ' The font set for "Heading1" will be inherited by all other headings.
    mez.Style(StyleNames.Heading1).Font("Arial", 16).Bold(True)

    mez.AddHeading1("My Heading")

    mez.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.")

    ' Create a PDF and open it in the default viewer.
    mez.MakePdf("Demo1.pdf", True)
End Sub

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/