Document Solutions for Excel, Java Edition | Document Solutions
File Operations / Export to PDF / Track Export Progress
In This Topic
    Track Export Progress
    In This Topic

    DsExcel provides getPagePrinting and getPagePrinted events in PdfSaveOptions class to track the export progress of a workbook to PDF. The getPagePrinting event occurs before printing a page and provides setSkipThisPage method to skip pages while exporting. Similarly, the getPagePrinted event occurs after printing a page and provides setHasMorePages method to exit PDF exporting.

    Display Export Progress

    Refer to the following example code to display the export progress of a workbook to PDF.

    Java
    Copy Code
    // Create to a pdf file stream
    FileOutputStream outputStream = null;
    try {
        outputStream = new FileOutputStream("PagePrintEventsTrackProgress.pdf");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    // Create a new workbook
    Workbook workbook = new Workbook();
    IWorksheet activeSheet = workbook.getActiveSheet();
    activeSheet.getRange("A1").setValue(1);
    activeSheet.getRange("A2:A100").setFormulaR1C1("=R[-1]C+1");
    PdfSaveOptions options = new PdfSaveOptions();
    options.getPagePrintingEvent().addListener(
    (sender, e) -> System.out.println(String.format("Printing page %1$s of %2$s", e.getPageNumber(), e.getPageCount())));
    activeSheet.getPageSetup().setCenterHeader("Page &P of &N");
    workbook.save(outputStream, options);
    
    // Close the file stream
    try {
        outputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    Skip a Page while Exporting

    Refer to the following example code to skip second page while exporting a workbook to PDF.

    Java
    Copy Code
    // Create to a pdf file stream
    FileOutputStream outputStream = null;
    try {
        outputStream = new FileOutputStream("PagePrintEventsSkipPage.pdf");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    // Create a new workbook
    Workbook workbook = new Workbook();
    IWorksheet activeSheet = workbook.getActiveSheet();
    activeSheet.getRange("A1").setValue(1);
    activeSheet.getRange("A2:A100").setFormulaR1C1("=R[-1]C+1");
    PdfSaveOptions options = new PdfSaveOptions();
    options.getPagePrintingEvent().addListener((sender, e) -> {
    if (e.getPageNumber() == 2) {
    e.setSkipThisPage(true);
    }
    });
    activeSheet.getPageSetup().setCenterHeader("Page &P of &N");
    workbook.save(outputStream, options);
    
    // Close the file stream
    try {
        outputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    Exit Exporting

    Refer to the following example code to exit PDF exporting after second page.

    Java
    Copy Code
    // Create to a pdf file stream
    FileOutputStream outputStream = null;
    try {
        outputStream = new FileOutputStream("PagePrintEventsExitPrinting.pdf");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    // Create a new workbook
    Workbook workbook = new Workbook();
    IWorksheet activeSheet = workbook.getActiveSheet();
    activeSheet.getRange("A1").setValue(1);
    activeSheet.getRange("A2:A100").setFormulaR1C1("=R[-1]C+1");
    PdfSaveOptions options = new PdfSaveOptions();
    options.getPagePrintedEvent().addListener((sender, e) -> {
    if (e.getPageNumber() == 2) {
    e.setHasMorePages(false);
    }
    });
    activeSheet.getPageSetup().setCenterHeader("Page &P of &N");
    workbook.save(outputStream, options);
    
    // Close the file stream
    try {
        outputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }