| 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:
//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;
'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
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:
//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;
'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
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.
| You can either choose FitToPages or Zoom property but not both at the same time. |
Example:
//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;
'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 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
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) |
| PaperEnvelopeC5 | Envelope C5 (162 mm x 229 mm) |
| PaperEnvelopeC6 | Envelope C6 (114 mm x 162 mm) |
| PaperEnvelopeC65 | Envelope C65 (114 mm x 229 mm) |
| PaperEnvelopeDL | Envelope DL (110 mm x 220 mm) |
| PaperEnvelopeItaly | Envelope Italy (110 mm x 230 mm) |
| PaperEnvelopeMonarch | Envelope Monarch (3-7/8 in. x 7-1/2 in.) |
| PaperEnvelopePersonal | Envelope (3-5/8 in. x 6-1/2 in.) |
| PaperESheet | E size sheet |
| PaperExecutive | Executive (7-1/2 in. x 10-1/2 in.) |
| PaperFanfoldLegalGerman | German Legal Fanfold (8-1/2 in. x 13 in.) |
| PaperFanfoldStdGerman | German Standard Fanfold (8-1/2 in. x 12 in.) |
| PaperFanfoldUS | U.S. Standard Fanfold (14-7/8 in. x 11 in.) |
| PaperFolio | Folio (8-1/2 in. x 13 in.) |
| PaperLedger | Ledger (17 in. x 11 in.) |
| PaperLegal | Legal (8-1/2 in. x 14 in.) |
| PaperLetter | Letter (8-1/2 in. x 11 in.) |
| PaperLetterSmall | Letter Small (8-1/2 in. x 11 in.) |
| PaperNote | Note (8-1/2 in. x 11 in.) |
| PaperQuarto | Quarto (215 mm x 275 mm) |
| PaperStatement | Statement (5-1/2 in. x 8-1/2 in.) |
| PaperTabloid | Tabloid (11 in. x 17 in.) |
Example:
//Instantiating a Workbook object Workbook workbook = new Workbook(); //Accessing the first worksheet in the Excel file Worksheet worksheet = workbook.Worksheets[0]; //Setting the paper size to A4 worksheet.PageSetup.PaperSize = PaperSizeType.PaperA4;
'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 paper size to A4 worksheet.PageSetup.PaperSize = PaperSizeType.PaperA4
5. Print Quality
Developer can set the print quality of the worksheets to be printed by setting the PrintQuality property of the PageSetup class. The measuring unit for print quality is Dot Per Inches (DPI) .
Example:
//Instantiating a Workbook object Workbook workbook = new Workbook(); //Accessing the first worksheet in the Excel file Worksheet worksheet = workbook.Worksheets[0]; //Setting the print quality of the worksheet to 180 dpi worksheet.PageSetup.PrintQuality = 180;
'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 print quality of the worksheet to 180 dpi worksheet.PageSetup.PrintQuality = 180
6. First Page Number
We can start the numbering of worksheet pages using the FirstPageNumber property of the PageSetup class. FirstPageNumber property sets the page number of the first worksheet page and then the next pages are numbered in ascending order.
Example:
//Instantiating a Workbook object Workbook workbook = new Workbook(); //Accessing the first worksheet in the Excel file Worksheet worksheet = workbook.Worksheets[0]; //Setting the first page number of the worksheet pages worksheet.PageSetup.FirstPageNumber = 2;
'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 first page number of the worksheet pages worksheet.PageSetup.FirstPageNumber = 2

