DsExcel Java provides the option to paginate a worksheet automatically, according to page boundaries, while exporting to PDF file.
The GetPaginationInfo method of PrintManager class gets an array of the page boundaries for horizontal and vertical paging. The method needs to be called separately for horizontal and vertical pagination. The retrieved pagination information is based on the page setup settings. The print area of the worksheet can also be defined. In case it is not defined, the default area is considered from cell A1 till the last cell where any cell data is present.
In addition to the page setup settings, you can also define ranges which need to be kept together and repeat settings of a range by using the overload of GetPaginationInfo method.
Refer to the following example code to paginate a worksheet while exporting to PDF file based on the page setup settings.
Java |
Copy Code |
---|---|
IWorkbook workbook = new Workbook(); IWorksheet worksheet = workbook.getWorksheets().get(0); // The row and column headings are printed worksheet.getPageSetup().setPrintHeadings(true); // The range "B6:N80" will be printed worksheet.getPageSetup().setPrintArea("B6:N80"); // Set data worksheet.getRange("B6:S8").setValue(10); // Add a table worksheet.getTables().add(worksheet.getRange("B6:N20"), true); PrintManager printManager = new PrintManager(); // The columnIndexs is [9, 13], this means that the horizontal direction is split after the column 10th and 14th List<Integer> columnIndexs = printManager.GetPaginationInfo(worksheet, PaginationOrientation.Horizontal); // The rowIndexs is [50, 79], this means that the vertical direction is split after the row 51th and 80th List<Integer> rowIndexs = printManager.GetPaginationInfo(worksheet, PaginationOrientation.Vertical); worksheet.save("Export.pdf", SaveFileFormat.Pdf); |
Refer to the following example code to paginate a worksheet while exporting to PDF file based on the page setup settings, range to be kept together and repeat settings.
Java |
Copy Code |
---|---|
// create a new workbook Workbook workbook = new Workbook(); IWorksheet worksheet = workbook.getWorksheets().get(0); // The row and column headings are printed worksheet.getPageSetup().setPrintHeadings(true); // The range "B6:N80" will be printed worksheet.getPageSetup().setPrintArea("B6:N80"); // Set data worksheet.getRange("B60:N80").setValue(1); // Add a table worksheet.getTables().add(worksheet.getRange("B6:N20"), true); // The row 6th will be printed at the top of each page ArrayList<RepeatSetting> repeatSettings = new ArrayList<RepeatSetting>(); RepeatSetting repeatSetting = new RepeatSetting(); repeatSetting.setTitleRowStart(5); repeatSetting.setTitleRowEnd(5); repeatSetting.setRange(worksheet.getRange("B6:N80")); repeatSettings.add(repeatSetting); // The rows from 25th to 60th should be paged to one page ArrayList<IRange> keepTogetherRanges = new ArrayList<IRange>(); keepTogetherRanges.add(worksheet.getRange("$25:$60")); PrintManager printManager = new PrintManager(); // The columnIndexs is [9, 13], this means that the horizontal direction is // split after the column 10th and 14th. List<Integer> columnIndexs = printManager.GetPaginationInfo(worksheet, PaginationOrientation.Horizontal, keepTogetherRanges, repeatSettings); // The rowIndexs is [23, 66, 79], this means that the vertical direction is // split after the row 24th, 67th and 80th. List<Integer> rowIndexs = printManager.GetPaginationInfo(worksheet, PaginationOrientation.Vertical, keepTogetherRanges, repeatSettings); List<PageInfo> pages = printManager.paginate(worksheet, keepTogetherRanges, repeatSettings); printManager.savePageInfosToPDF("GetPaginationRangesRepeatSettings.pdf", pages); |