Page control
Version 6.1.0
In this article
This article describes, how to control the page flow within a MigraDoc document.
Introduction
When rendering a PDF, the pages of a document are created. The PdfDocumentRenderer therefore calculates if the content will fit on the current page. If not, a new page is inserted.
When rendering a RTF, no pages are created at all. The displaying application will calculate where page breaks will occur.
For both PDF and RTF rendering, some MigraDoc objects will insert page breaks and some formatting properties will affect the page flow calculation. Those are explained here.
Sections
A section always starts on a new page.
Additionally, you can define, if a section shall explicitly start on an odd...
section.PageSetup.SectionStart = BreakType.BreakOddPage;
... or on an even page:
section.PageSetup.SectionStart = BreakType.BreakEvenPage;
See full Sections sample in PageControl in PDFsharp.Sample repository. Show resulting PDF
If needed, an empty page will be inserted to fulfill this requirement.
Page breaks
You can manually insert a page break using the following line:
section.AddPageBreak();
See full PageBreaks sample in PageControl in PDFsharp.Sample repository. Show resulting PDF
Page flow properties
For a paragraph, there are properties you can set on a ParagraphFormat object to control where page breaks will occur. Mostly you would use them in a style instead of setting them directly for the paragraph (see Formatting for more information).
The PageBreakBefore property can be activated to add a page break before the paragraph.
The KeepWithNext property can be activated to glue two paragraphs together. This property is useful for headings, because a heading at the bottom of a page just before a page break makes no sense.
The KeepTogether property can be activated to prevent page breaks within a paragraph. Use this with care and only for rather short paragraphs.
The WidowControl property can be activated to prevent page breaks after the first line and before the last line of a paragraph.
In books it is usually avoided to have a single line of a paragraph on one page with its rest on the other page.
WidowControl is set to true for default style StyleNames.Normal
, so without any changes it will be activated by default.
See PageFlowProperties sample in PageControl in PDFsharp.Sample repository. Show resulting PDF