//create to a pdf file stream FileOutputStream outputStream = null; try { outputStream = new FileOutputStream("KeepTogether.pdf"); } catch (FileNotFoundException e) { e.printStackTrace(); } //create a new workbook Workbook workbook = new Workbook(); workbook.open(this.getResourceStream("xlsx/KeepTogether.xlsx")); IWorksheet worksheet = workbook.getWorksheets().get(0); //The first page of the natural pagination is from row 1th to 37th, the second page is from row 38th to 73th. List keepTogetherRanges = new ArrayList(); //The row 37th and 38th need to keep together. So the pagination results are: the first page if from row 1th to 36th, the second page is from row 37th to 73th. keepTogetherRanges.add(worksheet.getRange("37:38")); //Create a PrintManager. PrintManager printManager = new PrintManager(); //Get the pagination information of the worksheet. List pages = printManager.paginate(worksheet, keepTogetherRanges, null); //Save the pages into pdf file. printManager.savePageInfosToPDF(outputStream, pages); //close the file stream try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); }
//create to a pdf file stream var outputStream: FileOutputStream? = null try { outputStream = FileOutputStream("KeepTogether.pdf") } catch (e: FileNotFoundException) { e.printStackTrace() } //create a new workbook var workbook = Workbook() workbook.open(this.getResourceStream("xlsx/KeepTogether.xlsx")!!) val worksheet = workbook.worksheets.get(0) //The first page of the natural pagination is from row 1th to 37th, the second page is from row 38th to 73th. val keepTogetherRanges = ArrayList() //The row 37th and 38th need to keep together. So the pagination results are: the first page if from row 1th to 36th, the second page is from row 37th to 73th. keepTogetherRanges.add(worksheet.getRange("37:38")) //Create a PrintManager. val printManager = PrintManager() //Get the pagination information of the worksheet. val pages = printManager.paginate(worksheet, keepTogetherRanges, null) //Save the pages into pdf file. printManager.savePageInfosToPDF(outputStream, pages) //close the file stream try { if(outputStream != null){ outputStream.close() } } catch (e: IOException) { e.printStackTrace() }