Using MigraDoc was never easier – “MigraDoc Made EZR”

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 official MigraDoc “Hello, World!” sample (60 lines) for comparison.

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()
            new MezFormattedText(bold)
        .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.)

2 thoughts on “Using MigraDoc was never easier – “MigraDoc Made EZR””

  1. Michael, thanks for asking. I didn’t publish it yet. You’re the first asking for it.

    Your feedback encourages me to publish it soon. I consider publishing the source (didn’t decide where to publish it yet) and one or more NuGet packages. Maybe July or August 2016.

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.