MigraDoc: PageSetup, Headers, Footers

Modified on 2015/08/27 12:04 by Thomas Hövel — Categorized as: Articles, MigraDoc Articles, MigraDoc FAQ

The PageSetup of MigraDoc can be used in two modes, depending on how you print the document.

Will you print on both sides?

Imagine a document with a few pages, printed on both sides and bound decoder on the long edge. At first you see page 1. Flip the page over and you will see page 2 on the left and page 3 on the right.

To get this, set "MirrorMargins" in MigraDoc to true. If you do this, "LeftMargin" will become the inner margin and "RightMargin" will be the outer margin. "LeftMargin" will be the left margin on pages 1, 3, ... but will be the right margin on pages 2, 4, ...

In this case you can also set "OddAndEvenPagesHeaderFooter" to true. This will allow you to have page numbers always at the outer edge: on the right side of odd pages and on the left side of even pages.

You may not want page numbers on the title page at all. In this case, you can set "DifferentFirstPageHeaderFooter" to true and leave header and footer empty.

Using sections

PageSetup, Header, and Footer are set for a section. You can have as many sections in your document as you need.

If your document contains several chapters and you want to have the name of the chapter in the page header, just create a new section for every chapter and set a new header.

The new section will inherit settings from the previous section if you do not overwrite them. You do not have to set a PageSetup for the new section unless you want to change something.

If you have to create a table with many columns, you may wish to have the section with the table in Landscape format while the rest is in Portrait format. Start a new section and set "Orientation" as needed. Set "Orientation" again in the following section to get back to Portrait format.

If you have set "MirrorMargins" to true you may wish to set "SectionStart" to either "BreakEvenPage" or "BreakOddPage". In that case MigraDoc may automatically create an empty page to have your section start on the correct page. "BreakNextPage" will be fine if your code does not rely on having the section start on a right or left page.
If, for example, you want to start a new section with a picture on the left and initial text on the right page, then use "BreakEvenPage" for that section.

Headers and Footers

There are three headers and three footers for each section.


Never modify the DefaultPageSetup

The DefaultPageSetup is not mutable and your code can change it. Feel free to change it, but I strongly recommended not to change it ever.

Just suppose your application supports more than one document. Let's say "Doc1" does not change the page margins, but "Doc2" changes the page margins and the orientation. If the user creates "Doc1" first, it will have the default margins. If the user creates "Doc2" and then "Doc1", they will get "Doc1" with the margins and orientation set for "Doc2".

You can easily miss this potential problem during debugging. Everything works fine unless you create "Doc1" when you previously created "Doc2".

It is easy to avoid this: when creating a new document, start with "DefaultPageSetup.Clone()" and make changes to your clone, your private copy of the default page setup.