Documents for Excel, Java Edition Documentation
File Operations / Export to PDF / Control Pagination / Keep Rows Together Over Page Breaks
In This Topic
    Keep Rows Together Over Page Breaks
    In This Topic

    GcExcel Java enables users to keep some rows together over page breaks while exporting to a PDF file.

    This feature is useful especially when you have data lying in large number of rows in the worksheet that you want to export to a PDF file. For instance - let's say you have a spreadsheet having multiple groups of rows that are often hidden, but ultimately modify the number of pages and page breaks while printing. Now, you want to export your Excel file to PDF in such a way that it keeps some groups of rows together so that they don't split across page breaks or pages when the print operation is executed. In such a scenario, it is extremely helpful to utilize this feature to achieve flawless printing experience and accurate content publishing while exporting to the PDF file.

    In order to keep some groups of rows together over page breaks, you need to first create a cell range including the rows that you want to show together in the PDF file. Next, create an instance of thePrintManager class and use the paginate() method to ensure the desired rows are displayed together. When you are done, simply save your PDF file using the savePageInfosToPDF() method.   

    Using Code

    Refer to the following example code to allow users to keep some rows together over page breaks while exporting to a PDF file.

    Java
    Copy Code
    // Initialize workbook
    Workbook workbook = new Workbook();
            
    // Open Excel file
    workbook.open("KeepTogether.xlsx");
            
    // Fetch default worksheet
    IWorksheet worksheet = workbook.getWorksheets().get(0);
    
    /* The first page of the natural pagination is from 
       row 1st to 36th, the second page is from row 37th to 73rd */
    List<IRange> keepTogetherRanges = new ArrayList<IRange>();
    
    /* The row 37th and 38th need to keep together.
       So the pagination results are: the first page is from row 
       1st to 35th, the second page is from row 36th to 73rd*/
    keepTogetherRanges.add(worksheet.getRange("36:37"));
    
    // Create an instance of the PrintManager class
    PrintManager printManager = new PrintManager();
    
    // Get the pagination information of the worksheet
    List<PageInfo> pages = 
    printManager.paginate(worksheet, keepTogetherRanges, null);
    
    // Save the modified pages into pdf file
    printManager.savePageInfosToPDF("KeepTogether.pdf", pages);