PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Wed Feb 22, 2017 10:38 am

All times are UTC

Post new topic Reply to topic  [ 1 post ] 
Author Message
PostPosted: Tue Sep 29, 2015 8:04 am 
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 215
Location: CCAA
Using MigraDoc was always easy, of course.

Currently I am working on a library that makes using MigraDoc even easier. Working title is "MigraDoc Made EZR". In this post I show you some examples.

It is tradition to start with a "Hello, World!" program when learning a new programming language. "MigraDoc Made EZR" is not really a programming language, but here is the "Hello, World!" sample:
namespace MigraDocMadeEZ
    class Program
        static void Main()
            // Instantiate MigraDocMadeEZR.
            var mez = new MigraDocMadeEZR();
            // Add the famous text.
            mez.AddParagraph("Hello, World!");
            // Create a PDF and open it in the default viewer.
            mez.MakePdf("HelloWorld.pdf", true);

And that is the complete contents of the Program.cs file for my console application.

The basic ideas behind "MigraDoc Made EZR":

  • One class giving access to the majority of the functions you need often, easily selectable with Intellisense
  • Describe the document with easily readable code without tons of variables to store elements
  • Easy handling for tables, images, styles, ...

Here is how you change a built-in style when using MigraDoc routines directly:
var style = document.Styles[StyleNames.Heading1];
style.Font.Name = "Arial";
style.Font.Size = 20;
style.ParagraphFormat.SpaceAfter = 6;
style.ParagraphFormat.SpaceBefore = 6;

And here we do the same using "MigraDoc Made EZR":
   .Font("Arial", 20)

OK - it only saves a single line. But I think the lines are more readable. Every change you make to a style object returns the same style object, allowing you to chain together all needed changes in a single statement. This principle is also used when dealing with tables, images, and other objects.

Adding new styles is similar to changing existing styles. Here I create a new style for table headings and set the font to bold. The name of the font is defined as a constant so I have the literal only once in my source code.

The code that creates the table. The row in the middle has a fixed width of 3 cm, the remaining page width will be assigned to the other two columns with a relation of 2:5, indicated by the star "*" which behaves similar to WPF.

When I add the heading row of the table, I have to mark it as heading and assign my heading style.
mez.AddRow("Name", "Value", "Description")

I don't have a style for the table body, so I simply add the data I need.
mez.AddRow("Beetle", "140 km/h", "The car that runs and runs.");
mez.AddRow("Spider", "258 km/h",  "A bit more expensive.");

If you want to mix different font styles in a table cell, things are a bit more complicated.
Here's a routine that creates a paragraph with bold and regular text.
static MezParagraph TableCellHelper(string bold, string normal)
    // Create a paragraph, add string "bold" as bold text, add string "normal" as regular text.
    return new MezParagraph()
        .AddFormattedText(new MezFormattedText(bold).Bold(true))
        .AddText(" " + normal);

And the routine at work:
mez.AddRow("Beetle", TableCellHelper("140", "km/h"), "The car that runs and runs.");
mez.AddRow("Spider", TableCellHelper("258", "km/h"), "A bit more expensive.");

Can this be made easier? Yes, it can. I have the idea of supporting some mark-up in the strings passed to AddRow().

(To be continued.)

Best regards
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)

Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC

Who is online

Users browsing this forum: No registered users and 1 guest

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group