[]
        
(Showing Draft Content)

跟踪导出进度

GcExcel在PdfSaveOptions类中提供getPagePrinting和getPagePrinted事件来跟踪工作簿到PDF的导出进度。getPagePrinting事件在打印页面之前发生,并提供setSkipThisPage方法在导出时跳过页面。类似地,getPagePrinted事件在打印页面之后发生,并提供setHasMorePages方法退出PDF导出。


显示导出进度

请参考以下示例代码,以显示工作簿导出PDF的进度。

// 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();
}

导出时跳过页面

请参考以下示例代码,以在将工作簿导出为PDF时跳过第二页。

// 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();
}

退出导出

请参阅以下示例代码以在第二页之后退出PDF导出。

// 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();
}