Document Solutions for Excel, Java Edition | Document Solutions
File Operations / Export to PDF / Control Pagination / Delete Blank Pages From Middle
In This Topic
    Delete Blank Pages From Middle
    In This Topic

    While exporting a workbook to a PDF file, sometimes you may encounter a couple of extra pages that are completely blank. In a workbook with large number of worksheets, it is extremely difficult to find out which pages are empty and even more time-consuming to delete them from the middle without impacting the pagination. 

    In order to avoid printing and publishing of blank pages, DsExcel Java enables users to scan through the pages of the PDF, find out which pages are blank and then exclude the blank pages from the middle while also updating the pagination information accurately.

    For removing blank pages from your PDF file, you need to first create an instance of the PrintManager class and use the paginate() method to get the default pagination of the workbook. Now, you can use the hasPrintContent() method to check whether the pages have content or not. Finally, call the updatePageNumberAndPageSettings() method in order to update the indexes of the page number and the page settings for each page. When you are done, simply save your PDF file using the savePageInfosToPDF() method.   

    Using Code

    Refer to the following example code to delete blank pages from the middle while exporting to PDF.

    Java
    Copy Code
    // Initialize workbook
    Workbook workbook = new Workbook();
    
    // Open Excel file
    workbook.open("DeletingBlankPages.xlsx");
    
    // Create an instance of the PrintManager class
    PrintManager printManager = new PrintManager();
            
    // Get the default pagination information of the workbook
    List<PageInfo> pages = printManager.paginate(workbook);
    
    // Remove empty pages
    List<PageInfo> newPages =new ArrayList<PageInfo>();
    for(PageInfo page : pages)
    {
        // True if there is content in the range to print
        if(printManager.hasPrintContent(page.getPageContent()
            .getRange()))
        {
            newPages.add(page);
        }
    }
    
    // Update the page number and the page settings of each page
    printManager.updatePageNumberAndPageSettings(newPages);
    
    // Save to PDF file
    printManager.savePageInfosToPDF("DeleteBlankPagesFromMiddle.pdf", newPages);