Page Setup and Printing Options
Sometimes, developers need to configure page setup and print settings to control the printing process. Page setup and print settings offer various options and are fully supported in Aspose.Cells.
This article shows how to create a console application and apply page setup and printing options to a worksheet with a few simple lines of code using the Aspose.Cells API.
Working with Page and Print Settings
For this example, we created a workbook in Microsoft Excel and use Aspose.Cells to set page setup and print options.
Setting Page Setup Options
First create a simple worksheet in Microsoft Excel. Then apply page setup options to it. Executing the code changes the Page Setup options as in the screenshot below.
Output file
- Create a worksheet with some data in Microsoft Excel:
-
Open a new workbook in Microsoft Excel.
-
Add some data. Below is a screenshot of the file.
Input file
-
-
Set page setup options: Apply page setup options to the file. Below is a screenshot of the default options, before the new options are applied.
Default page setup options
- Download and install Aspose.Cells:
- Create a project. Either create a project using a Java editor, for example Eclipse, or create a simple program using a text editor.
- Add a class path.
- Extract the Aspose.Cells.jar and dom4j_1.6.1.jar from Aspose.Cells.zip.
- Set the classpath of project in Eclipse:
- Select your project in Eclipse and then click Project followed by Properties.
- Select Java Build Path to the left of the dialog.
- Select the Libraries tab, click Add JARs or Add External JARs to select Aspose.Cells.jar and dom4j_1.6.1.jar and add them to the build paths. Or you may set it at runtime at a DOS prompt in Windows:
javac \-classpath %classpath%;e:\Aspose.Cells.jar; ClassName .javajava \-classpath %classpath%;e:\Aspose.Cells.jar; ClassName
- Write the application that invokes APIs: Below is the code used by the component in this example.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(SettingPageSetupOptions.class); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(dataDir + "CustomerReport.xls"); | |
// Accessing the first worksheet in the Excel file | |
Worksheet sheet = workbook.getWorksheets().get(0); | |
PageSetup pageSetup = sheet.getPageSetup(); | |
// Setting the orientation to Portrait | |
pageSetup.setOrientation(PageOrientationType.PORTRAIT); | |
// Setting the scaling factor to 100 | |
// pageSetup.setZoom(100); | |
// OR Alternately you can use Fit to Page Options as under | |
// 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); | |
// Setting the paper size to A4 | |
pageSetup.setPaperSize(PaperSizeType.PAPER_A_4); | |
// Setting the print quality of the worksheet to 1200 dpi | |
pageSetup.setPrintQuality(1200); | |
// Setting the first page number of the worksheet pages | |
pageSetup.setFirstPageNumber(2); | |
// Save the workbook | |
workbook.save(dataDir + "PageSetup.xls"); |
Setting Print options
Page setup settings also provide several print options (also called sheet options) that allow users to control how worksheet pages are printed. They allow users to:
- Select a specific print area of a worksheet.
- Print titles.
- Print gridlines.
- Print row/column headings.
- Achieve draft quality.
- Print comments.
- Print cell errors.
- Define page ordering.
The example that follows applies print options to the file created in the example above (PageSetup.xls). The screenshot below shows the default print options before new options are applied. Input document
Executing the code changes the print options. Output file
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(SettingPrintoptions.class); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(dataDir + "PageSetup.xls"); | |
// Accessing the first worksheet in the Excel file | |
Worksheet sheet = workbook.getWorksheets().get(0); | |
PageSetup pageSetup = sheet.getPageSetup(); | |
// Specifying the cells range (from A1 cell to E30 cell) of the print area | |
pageSetup.setPrintArea("A1:E30"); | |
// Defining column numbers A & E as title columns | |
pageSetup.setPrintTitleColumns("$A:$E"); | |
// Defining row numbers 1 & 2 as title rows | |
pageSetup.setPrintTitleRows("$1:$2"); | |
// Allowing to print gridlines | |
pageSetup.setPrintGridlines(true); | |
// Allowing to print row/column headings | |
pageSetup.setPrintHeadings(true); | |
// Allowing to print worksheet in black & white mode | |
pageSetup.setBlackAndWhite(true); | |
// Allowing to print comments as displayed on worksheet | |
pageSetup.setPrintComments(PrintCommentsType.PRINT_IN_PLACE); | |
// Allowing to print worksheet with draft quality | |
pageSetup.setPrintDraft(true); | |
// Allowing to print cell errors as N/A | |
pageSetup.setPrintErrors(PrintErrorsType.PRINT_ERRORS_NA); | |
// Setting the printing order of the pages to over then down | |
pageSetup.setOrder(PrintOrderType.OVER_THEN_DOWN); | |
// Save the workbook | |
workbook.save(dataDir + "PageSetup_Print.xls"); |
Summary
This article shows how to set page setup and sheet print options using Aspose.Cells. Hopefully, it will give you some insight, and you can use these options in your own scenarios.
Aspose.Cells benefits from years of research, design and careful tuning. We heartily welcome your queries, comments and suggestions at Aspose.Cells Forum. We warranty a prompt reply.