Aspose.Pdf

Set Page Size and Margins

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:

 

 

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 = PageSize.A3Width;

sec1.PageInfo.PageHeight = PageSize.A3Height;

 

[VB.NET]

 

sec1.PageInfo.PageWidth = PageSize.A3Width

sec1.PageInfo.PageHeight = PageSize.A3Height

 

[JAVA]

 

sec1.getPageSetup().setPageWidth(PageSize.A3Width);

sec1.getPageSetup().setPageHeight(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:

 

 

Note: 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 Section.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:

 

 

Code Snippet

 

[C#]

 

//Instantiate the MarginInfo instance

MarginInfo marginInfo = new 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 MarginInfo = New 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

 

[JAVA]

 

//Instantiate the MarginInfo instance

MarginInfo marginInfo = new 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.setTop(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.setBottom(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.setLeft(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.setRight(90);

 

//Assign the marginInfo instance to Margin property of sec1.PageInfo

sec1.getPageSetup().setMargin(marginInfo);