Introduction
Sometimes, developers may like to configure page setup settings for their worksheets to control their printing process. These page setup settings offer various options to be configured. Page setup options are fully supported in Aspose.Cells. In this topic, we'd discuss that how can we use Aspose.Cells to configure Page Options as shown below:
Setting Page Options
Aspose.Cells provides a class, Workbook that represents an Excel file. Workbook class contains a 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 property is an object of PageSetup class that enables developers to set different page layout options for a printed worksheet. PageSetup class provides various properties that are used to set page setup options but few of these properties are discussed below to set Page Options.
1. Page Orientation
Developers can set the orientation of the page to Portrait or Landscape using the Orientation property of the PageSetup class. Orientation property accepts one of the pre-defined values in PageOrientationType enumeration, which are listed below:
|
Page Orientation Types
|
Description
|
|
Landscape
|
Represents Landscape orientation
|
|
Portrait
|
Represents Portrait orientation
|
Example:
[C#]
//Instantiating a Workbook object
Workbook workbook = new Workbook();
//Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
//Setting the orientation to Portrait
worksheet.PageSetup.Orientation = PageOrientationType.Portrait;
[VB.NET]
'Instantiating a Workbook object
Dim workbook As Workbook = New Workbook()
'Accessing the first worksheet in the Excel file
Dim worksheet As Worksheet = workbook.Worksheets(0)
'Setting the orientation to Portrait
worksheet.PageSetup.Orientation = PageOrientationType.Portrait
[JAVA]
//Instantiating a Workbook object
Workbook workbook = new Workbook();
//Accessing the first worksheet in the Excel file
Worksheets worksheets = workbook.getWorksheets();
Worksheet sheet = worksheets.addSheet();
//Setting the orientation to Portrait
PageSetup pageSetup = sheet.getPageSetup();
pageSetup.setOrientation(PageOrientationType.PORTRAIT);
2. Scaling Factor
Developer can also reduce or enlarge the size of the worksheets by adjusting the scaling factor of the worksheet with the use of Zoom property of the PageSetup class.
Example:
[C#]
//Instantiating a Workbook object
Workbook workbook = new Workbook();
//Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
//Setting the scaling factor to 100
worksheet.PageSetup.Zoom = 100;
[VB.NET]
'Instantiating a Workbook object
Dim workbook As Workbook = New Workbook()
'Accessing the first worksheet in the Excel file
Dim worksheet As Worksheet = workbook.Worksheets(0)
'Setting the scaling factor to 100
worksheet.PageSetup.Zoom = 100
[JAVA]
//Instantiating a Excel object
Workbook workbook = new Workbook();
//Accessing the first worksheet in the Excel file
Worksheets worksheets = workbook.getWorksheets();
Worksheet sheet = worksheets.addSheet();
//Setting the scaling factor to 100
PageSetup pageSetup = sheet.getPageSetup();
pageSetup.setZoom(100);
3. FitToPages Options
If developers need to fit the contents of the worksheet to desired number of pages then they can do it by using the FitToPagesTall and FitToPagesWide properties of the PageSetup class. These properties are also used for the scaling of worksheets.
Note: You can either choose FitToPages or Zoom property but not both at the same time.
Example:
[C#]
//Instantiating a Workbook object
Workbook workbook = new Workbook();
//Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
//Setting the number of pages to which the length of the worksheet will be spanned
worksheet.PageSetup.FitToPagesTall = 1;
//Setting the number of pages to which the width of the worksheet will be spanned
worksheet.PageSetup.FitToPagesWide = 1;
[VB.NET]
'Instantiating a Workbook object
Dim excel As Workbook = New Workbook()
'Accessing the first worksheet in the Excel file
Dim worksheet As Worksheet = workbook.Worksheets(0)
'Setting the number of pages to which the length of the worksheet will be spanned
worksheet.PageSetup.FitToPagesTall = 1
'Setting the number of pages to which the width of the worksheet will be spanned
worksheet.PageSetup.FitToPagesWide = 1
[JAVA]
//Instantiating a Workbook object
Workbook workbook = new Workbook();
//Accessing the first worksheet in the Excel file
Worksheets worksheets = workbook.getWorksheets();
Worksheet sheet = worksheets.addSheet();
PageSetup pageSetup = sheet.getPageSetup();
//Setting the number of pages to which the length of the worksheet will be spanned
pageSetup.setFitToPagesTall(1);
//Setting the number of pages to which the width of the worksheet will be spanned
pageSetup.setFitToPagesWide(1);
4. Paper Size
Developer can also set the paper size of the worksheets to be printed by setting the PaperSize property of the PageSetup class. PaperSize property accepts one of the pre-defined values in PaperSizeType enumeration, which are listed below:
|
Paper Size Types
|
Description
|
|
Paper10x14
|
10 in. x 14 in.
|
|
Paper11x17
|
11 in. x 17 in.
|
|
PaperA3
|
A3 (297 mm x 420 mm)
|
|
PaperA4
|
A4 (210 mm x 297 mm)
|
|
PaperA4Small
|
A4 Small (210 mm x 297 mm)
|
|
PaperA5
|
A5 (148 mm x 210 mm)
|
|
PaperB4
|
B4 (250 mm x 354 mm)
|
|
PaperB5
|
B5 (182 mm x 257 mm)
|
|
PaperCSheet
|
C size sheet
|
|
PaperDSheet
|
D size sheet
|
|
PaperEnvelope10
|
Envelope #10 (4-1/8 in. x 9-1/2 in.)
|
|
PaperEnvelope11
|
Envelope #11 (4-1/2 in. x 10-3/8 in.)
|
|
PaperEnvelope12
|
Envelope #12 (4-1/2 in. x 11 in.)
|
|
PaperEnvelope14
|
Envelope #14 (5 in. x 11-1/2 in.)
|
|
PaperEnvelope9
|
Envelope #9 (3-7/8 in. x 8-7/8 in.)
|
|
PaperEnvelopeB4
|
Envelope B4 (250 mm x 353 mm)
|
|
PaperEnvelopeB5
|
Envelope B5 (176 mm x 250 mm)
|
|
PaperEnvelopeB6
|
Envelope B6 (176 mm x 125 mm)
|
|
PaperEnvelopeC3
|
Envelope C3 (324 mm x 458 mm)
|
|
PaperEnvelopeC4
|
Envelope C4 (229 mm x 324 mm)
|
|