Document Solutions for Excel, Java Edition | Document Solutions
File Operations / Export to PDF / Control Pagination / Save Multiple Workbooks to Single PDF
In This Topic
    Save Multiple Workbooks to Single PDF
    In This Topic

    DsExcel Java allows users to save multiple workbooks into a single Portable Document File (PDF) by either using the saveWorkbooksToPDF() method or using the savePageInfosToPDF() method of the PrintManager class. Each workbook is saved to a new page in the PDF file. The information in the PDF such as the page number, number of pages, odd and even pages, first page etc. is saved on the basis of the final pagination results.

    Advantage of Saving Multiple Workbooks to Single PDF File

    This feature is useful especially when you need consolidated information at one place for enhanced analysis and visualization. For instance - let's say you have sales information about different versions of a product in different workbooks. Instead of sharing multiple spreadsheets or PDF files; you can share a combined PDF (by saving all the workbooks to a single PDF file) showcasing the annual sales figures of the product. This will not only help users to analyse all the crucial information at one place but it will also facilitate them in sharing, manipulating and printing all the sales data in an efficient way.

    Refer to the following example code in order to export multiple workbooks to a single PDF file using the saveWorkbooksToPDF() method.

    Java
    Copy Code
    // Initialize first workbook
    Workbook workbook1 = new Workbook();
            
    // Opening Excel file
    workbook1.open("Book1.xlsx");
    
    // Initialize second workbook
    Workbook workbook2 = new Workbook();
            
    // Opening Excel file
    workbook2.open("Book2.xlsx");
    
    // Create an instance of the PrintManager class
    PrintManager printManager = new PrintManager();
    List<IWorkbook> workbooks = new ArrayList<IWorkbook>();
    workbooks.add(workbook1);
    workbooks.add(workbook2);
    
    printManager.saveWorkbooksToPDF("SaveToOnePDF.pdf", workbooks);

    Refer to the following example code in order to export multiple workbooks to a single PDF file using the savePageInfosToPDF() method.

    Java
    Copy Code
    // Initialize first workbook
    Workbook workbook1 = new Workbook();
            
    // Opening Excel file
    workbook1.open("Book1.xlsx");
            
    // Initialize second workbook
    Workbook workbook2 = new Workbook();
            
    // Opening Excel file
    workbook2.open("Book2.xlsx");
    
    // Create an instance of the PrintManager class
    PrintManager printManager = new PrintManager();
    
    workbook1.getWorksheets().get(0).getPageSetup().setCenterFooter("&P of &N");
    workbook1.getWorksheets().get(0).getPageSetup().setCenterHeader("&G");
    try {
        workbook1.getWorksheets().get(0).getPageSetup().getCenterHeaderPicture()
        .setFilename("C:\\Documents\\GcExcelJAVA-May'19Samples\\logo.png");
    } catch (FileNotFoundException e) {
    
        e.printStackTrace();
    }
    workbook1.getWorksheets().get(0).getPageSetup().getCenterHeaderPicture().setWidth(150);
    workbook1.getWorksheets().get(0).getPageSetup().getCenterHeaderPicture().setHeight(50);
    workbook1.getWorksheets().get(0).getPageSetup().setTopMargin(100);
    
    workbook1.getWorksheets().get(1).getPageSetup().setCenterFooter("&P of &N");
    workbook1.getWorksheets().get(1).getPageSetup().setCenterHeader("&G");
    try {
        workbook1.getWorksheets().get(1).getPageSetup().getCenterHeaderPicture()
        .setFilename("C:\\Documents\\GcExcelJAVA-May'19Samples\\logo.png");
    } catch (FileNotFoundException e) {
    
        e.printStackTrace();
    }
    workbook1.getWorksheets().get(1).getPageSetup().getCenterHeaderPicture().setWidth(150);
    workbook1.getWorksheets().get(1).getPageSetup().getCenterHeaderPicture().setHeight(50);
    workbook1.getWorksheets().get(1).getPageSetup().setTopMargin(100);
    
    workbook2.getWorksheets().get(0).getPageSetup().setCenterFooter("&P of &N");
    workbook2.getWorksheets().get(0).getPageSetup().setCenterHeader("Google");
    workbook2.getWorksheets().get(0).getPageSetup().setTopMargin(100);
    
    List<PageInfo> pages1 = printManager.paginate(workbook1);
    List<PageInfo> pages2 = printManager.paginate(workbook2);
    
    ArrayList<PageInfo> pages = new ArrayList<PageInfo>();
    pages.addAll(pages1);
    pages.addAll(pages2);
    
    printManager.updatePageNumberAndPageSettings(pages);
    
    // Save the workbook1 and workbook2 into the PDF file
    printManager.savePageInfosToPDF("SaveToOnePDF.pdf", pages);