Setting Margins

Skip to end of metadata
Go to start of metadata
The Page setup options are fully supported by Aspose.Cells component. The developers may need to configure page setup settings for their worksheets to control their printing process. In this topic, we'd discuss that how can we use Aspose.Cells APIs to configure Page Margins as shown in the dialog below:



Figure: Page Margins

Setting Margins

Aspose.Cells provides a class, Workbook that represents an Excel file. Workbook class contains  Worksheets collection that allows to access each worksheet in the Excel file. A worksheet is represented by the Worksheet class.

Worksheet class provides PageSetup property that is used to set the page setup options of the worksheet. In fact, this PageSetup attribute is an object of PageSetup class that enables developers to set different page layout options for a printed worksheet. PageSetup class provides various properties/methods that are used to set page setup options accordingly.

1. Page Margins

Developers can set the margins (left, right, top, bottom) of the page for their need using the PageSetup class members. A few of the properties/methods are listed below which are used to specify page margins:

  • LeftMargin (.NET)
  • RightMargin (.NET)
  • TopMargin (.NET)
  • BottomMargin (.NET)

Example:

[C#]
//Create a workbook object
Workbook workbook = new Workbook();

//Get the worksheets in the workbook
WorksheetCollection worksheets = workbook.Worksheets;

//Get the first (default) worksheet
Worksheet worksheet = worksheets[0];

//Get the pagesetup object
PageSetup pageSetup = worksheet.PageSetup;

//Set bottom,left,right and top page margins
pageSetup.BottomMargin = 2;
pageSetup.LeftMargin = 1;
pageSetup.RightMargin = 1;
pageSetup.TopMargin = 3;
 


[VB]
'Create a workbook object
Dim workbook As New Workbook()

'Get the worksheets in the workbook
Dim worksheets As WorksheetCollection = workbook.Worksheets

'Get the first (default) worksheet
Dim worksheet As Worksheet = worksheets(0)

'Get the pagesetup object
Dim pageSetup As PageSetup = worksheet.PageSetup

'Set bottom,left,right and top page margins
pageSetup.BottomMargin = 2
pageSetup.LeftMargin = 1
pageSetup.RightMargin = 1
pageSetup.TopMargin = 3
 
2. Center on Page

Developers may utilize Center on page Horizontally and Vertically for their need. There are some useful members of PageSetup class, such as, CenterHorizontally and CenterVertically.

Example:

[C#]
//Specify Center on page Horizontally and Vertically
pageSetup.CenterHorizontally = true;
pageSetup.CenterVertically = true;
 


[VB]
'Specify Center on page Horizontally and Vertically
pageSetup.CenterHorizontally = True
pageSetup.CenterVertically = True
 
3. Header / Footer Margins

Developers set header / footer margins for their need. There are some useful members of PageSetup class, such as, HeaderMargin and FooterMargin.

Example:

[C#]
//Specify Header / Footer margins
pageSetup.HeaderMargin = 2;
pageSetup.FooterMargin = 2;
 


[VB]
'Specify Header / Footer margins
pageSetup.HeaderMargin = 2
pageSetup.FooterMargin = 2
 
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.