Set Page Size and Margins

Skip to end of metadata
Go to start of metadata
A Document contains a collection of Sections. A Section contains a collection of Paragraphs. You can define page size and margins for each Section. All pages that are rendered from the same section have the same size and margins. You can set page size and margins using the PageInfo property of the Section class.

The following figure shows the graphical representation of page size and margins of a page in the PDF document:

Page Size
You can use PageSize class to set system defined page size. The default page size is A4 size.
Page Size using Static Read Only Fields

PageSize class has several static read only fields that represent different page sizes as follows:

  • A0
  • A1
  • A2
  • A3
  • A4
  • A5
  • A6
  • B5
  • Letter
  • Legal
  • Ledger
  • P11x17
There are two static read only fields for each page size, one for width and other for height. For example: To represent A3 page size, there are two static fields: A3Width and A3Height.

Code Snippet

C#
sec1.PageInfo.PageWidth = Aspose.Pdf.Generator.PageSize.A3Width; 
sec1.PageInfo.PageHeight = Aspose.Pdf.Generator.PageSize.A3Height; 
 
VB.NET
sec1.PageInfo.PageWidth = Aspose.Pdf.Generator.PageSize.A3Width
sec1.PageInfo.PageHeight = Aspose.Pdf.Generator.PageSize.A3Height 
 
Page Size in units
You can also set page size with width and height numbers. In XML, PageSize is not supported and you can only use numbers.

The units supported for setting the page size are as follows:

  • point
  • cm
  • inch
In API, only point unit is supported.

Code Snippet

C#
sec1.PageInfo.PageWidth = 576; 
sec1.PageInfo.PageHeight = 707.5F; 
 
VB.NET
sec1.PageInfo.PageWidth = 576 
sec1.PageInfo.PageHeight = 707.5 
 
XML
<Section PageWidth="8inch" PageHeight="25cm"> 
 
Page Margins
Page margins can be set through Margin property of PageInfo. The way of setting page margins is just like that of setting page size. The default left and right margin of page are both 90 points. The default top and bottom margin of page are both 72 points.

Please follow these steps to set the Margin of the page:

  • Instantiate the MarginInfo object
  • Set the Top , Bottom , Right and Left properties of MarginInfo instance
  • Assign the instance of MarginInfo class to Margin property of Section.PageInfo

Code Snippet

C#
//Instantiate the MarginInfo instance
Aspose.Pdf.Generator.MarginInfo marginInfo = new Aspose.Pdf.Generator.MarginInfo();

//Set the margin Top. This value is in points but other units like
//inches and centi meters can also be used as 12inch or 12cm
marginInfo.Top = 72;

//Set the margin Bottom. This value is in points but other units like
//inches and centi meters can also be used as 12inch or 12cm
marginInfo.Bottom = 72;

//Set the margin Left. This value is in points but other units like
//inches and centi meters can also be used as 12inch or 12cm
marginInfo.Left = 90;

//Set the margin Right. This value is in points but other units like
//inches and centi meters can also be used as 12inch or 12cm
marginInfo.Right = 90;

//Assign the marginInfo instance to Margin property of sec1.PageInfo
sec1.PageInfo.Margin = marginInfo;
 
VB.NET
'Instantiate the MarginInfo instance
Dim marginInfo As Aspose.Pdf.Generator.MarginInfo = New Aspose.Pdf.Generator.MarginInfo()

'Set the margin Top. This value is in points but other units like
'inches and centi meters can also be used as 12inch or 12cm
marginInfo.Top = 72

'Set the margin Bottom. This value is in points but other units like
'inches and centi meters can also be used as 12inch or 12cm
marginInfo.Bottom = 72

'Set the margin Left. This value is in points but other units like
'inches and centi meters can also be used as 12inch or 12cm
marginInfo.Left = 90

'Set the margin Right. This value is in points but other units like
'inches and centi meters can also be used as 12inch or 12cm
marginInfo.Right = 90

'Assign the marginInfo instance to Margin property of sec1.PageInfo
sec1.PageInfo.Margin = marginInfo
 
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.