Page setup
Version 6.1.0
In this article
This article describes how the page size and margins are changed.
Introduction
Within the PageSetup instance of a section you can control its page size and margins as well as some other page related options. But usually, you don’t have to initialize a page setup for each section. The page setup comes with inheritance. So, unset properties of a sections’s page setup will inherit the value from the previous sections’s page setup. The first section will finally inherit from the document’s default page setup.
The document’s default page setup always comes with a portrait A4 page (21 x 29.7 cm). The top, left and right margins are set to 2.5 cm and the bottom margin to 2 cm. The distance to the header and the footer from the page border is 1.25 cm. It configures the section to start with the next page and not to use special first or even page headers and footers.
If you don’t want a section to inherit the page setup from the previous one, you can assign a copy of the default page setup:
// Reset to DefaultPageSetup.
section.PageSetup = document.DefaultPageSetup.Clone();
See full Example in PDFsharp.Sample repository. Show resulting PDF
The default page setup itself is unchangeable. But instead of the default page setup, you could also assign a clone of any PageSetup instance with your preferred settings.
See note below on ResetPageSize method, useful when cloning the DefaultPageSetup.
Page size
Setting the page size equals most commonly choosing the desired page format and orientation. The PageFormat enum provides a set of usual page formats for that purpose and the Orientation enum comes with Portrait and Landscape values.
section.PageSetup.PageFormat = PageFormat.A5;
section.PageSetup.Orientation = Orientation.Landscape;
See full Example in PDFsharp.Sample repository. Show resulting PDF
Of course, you can also specify an individual size:
section.PageSetup.PageWidth = Unit.FromCentimeter(15);
section.PageSetup.PageHeight = Unit.FromCentimeter(20);
See full Example in PDFsharp.Sample repository. Show resulting PDF
PageWidth and PageHeight are properties of the type Unit (see Unit for more information).
When setting the PageWidth or PageHeight property, the Orientation property is automatically updated to the correct value. If the width and the height of a page is equal, it is considered as portrait.
Note
Setting PageFormat or Orientation may not have the desired effect if values have been set for PageWidth and/or PageHeight. This will happen when you create a Clone of the DefaultPageSetup. The PageSetup class provides the ResetPageSize method for that purpose. After cloning the DefaultPageSetup, invoke ResetPageSize for the clone before setting PageFormat and Orientation.
Page margins
The page margins are set like this:
section.PageSetup.TopMargin = Unit.FromCentimeter(2);
section.PageSetup.RightMargin = Unit.FromCentimeter(4);
section.PageSetup.BottomMargin = Unit.FromCentimeter(6);
section.PageSetup.LeftMargin = Unit.FromCentimeter(8);
See full Example in PDFsharp.Sample repository. Show resulting PDF
The margin properties are of the type Unit (see Unit for more information).
If you want your document to be laid out as a book or magazine, you may want to set up a symmetric page layout. You can activate such a layout by setting the MirrorMargins property to true:
section.PageSetup.MirrorMargins = true;
See full Example in PDFsharp.Sample repository. Show resulting PDF
With MirrorMargins set to true, all odd pages will have the margins like they were set. All even pages will instead have the left and right margin switched.
When using mirrored margins, you can set the horizontal position of images, text frames or charts to inside and outside (see Sizing & positioning for more information).
Further options
There are some more functionalities of the page setup that are not described here. These are:
- SectionStart, which lets you start the section explicitly on an odd or even page (see Document / Page control)
- StartingNumber, which influences the page number shown by fields (see Contents / Fields)
- Header and footer related properties (see Contents / Headers & footers)