In DsExcel Java, you can use the setLeftHeader, setRightHeader, setLeftFooter, setRightFooter, setCenterHeader and setCenterFooter methods of the IPageSetup interface in order to configure header and footer for a page.
Java |
Copy Code |
---|---|
// Configure PageHeader and PageFooter // Set header for the page worksheet.getPageSetup().setLeftHeader("&\"Arial,Italic\"LeftHeader"); worksheet.getPageSetup().setCenterHeader("&P"); // Set header and footer graphic for the page worksheet.getPageSetup().setCenterFooter("&G"); InputStream stream = ClassLoader.getSystemResourceAsStream("logo.png"); worksheet.getPageSetup().getCenterFooterPicture().setGraphicStream(stream, ImageType.PNG); |
For special settings, you can also perform the following tasks in order to customize the configuration of the header and footer of your page:
If you want a different header and footer in your first page, you first need to set the setDifferentFirstPageHeaderFooter method of the IPageSetup interface to true. When this is done, you can use the method of the IPageSetup interface in order to configure the first page header and footer.
Java |
Copy Code |
---|---|
//Set first page header and footer worksheet.getPageSetup().setDifferentFirstPageHeaderFooter(true); worksheet.getPageSetup().getFirstPage().getCenterHeader().setText("&T"); worksheet.getPageSetup().getFirstPage().getRightFooter().setText("&D"); //Set first page header and footer graphic worksheet.getPageSetup().getFirstPage().getLeftFooter().setText("&G"); InputStream stream = ClassLoader.getSystemResourceAsStream("logo.png"); worksheet.getPageSetup().getFirstPage().getLeftFooter().getPicture().setGraphicStream(stream, ImageType.PNG); worksheet.getPageSetup().getFirstPage().getLeftFooter().getPicture().setWidth(100); worksheet.getPageSetup().getFirstPage().getLeftFooter().getPicture().setHeight(13); |
If you want a different header and footer for all the even pages, you first need to set the setOddAndEvenPagesHeaderFooter method to true. When this is done, you can use other methods of the IPageSetup interface in order to configure the even page header and footer.
Java |
Copy Code |
---|---|
// Set even page header and footer worksheet.getPageSetup().setOddAndEvenPagesHeaderFooter(true); worksheet.getPageSetup().getEvenPage().getCenterHeader().setText("&T"); worksheet.getPageSetup().getEvenPage().getRightFooter().setText("&D"); // Set even page header and footer graphic worksheet.getPageSetup().getEvenPage().getLeftFooter().setText("&G"); InputStream stream = ClassLoader.getSystemResourceAsStream("logo.png"); worksheet.getPageSetup().getEvenPage().getLeftFooter().getPicture().setGraphicStream(stream, ImageType.PNG); |
DsExcel supports Addition (+) and Subtraction (-) operators for page and total page number calculations in custom page headers and footers. These operators function in all paginated outputs that respect custom headers and footers in getPageSetup method of IWorksheet interface, such as PDFs and print to physical printers.
DsExcel supports the following formulas for page and total page number calculations:
Formula | Description |
---|---|
&P+number | Prints the page number plus the specified number. |
&P-number | Prints the page number minus the specified number. |
&N+number | Prints the total number of pages plus the specified number. |
&N-number | Prints the total number of pages minus the specified number. |
Java |
Copy Code |
---|---|
// Create a new workbook. var workbook = new Workbook(); // Open Excel document. workbook.open("PageSetup Demo.xlsx"); // Access first worksheet. IWorksheet worksheet = workbook.getWorksheets().get(0); // Set page headerfooter. /* Use &P+1 to display the page number plus one. Use &N+1 to display the total page number plus one. */ worksheet.getPageSetup().setRightHeader("Page header example &P+1 of &N+1"); worksheet.getPageSetup().setRightFooter("Page footer example &P+2 of &N+2"); worksheet.getPageSetup().customPaperSize(8.5,6); // Save workbook to a PDF document. workbook.save("ConfigHeaderFooterPageNumberCalc.pdf"); |