避免输出PDF中的空白页在没有内容打印时

可能的使用场景

当Excel文件为空且用户将其保存为PDF使用Aspose.Cells时,在输出PDF中会呈现空白页。有时,这种默认行为是不希望的。Aspose.Cells提供了PdfSaveOptions.OutputBlankPageWhenNothingToPrint属性来处理此问题。如果将其设置为false,则在输出PDF中没有打印内容时将会发生CellsException

当没有需要打印的内容时,避免在输出PDF中出现空白页

以下示例代码创建一个空工作簿,然后在将PdfSaveOptions.OutputBlankPageWhenNothingToPrint属性设置为false后将其保存为输出PDF。由于输出PDF中没有打印内容,将会发生如下CellsException

示例代码

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
//Create empty workbook.
Workbook wb = new Workbook();
//Create Pdf save options.
PdfSaveOptions opts = new PdfSaveOptions();
//Default value of OutputBlankPageWhenNothingToPrint is true.
//Setting false means - Do not output blank page when there is nothing to print.
opts.setOutputBlankPageWhenNothingToPrint(false);
//Save workbook to Pdf format, it will throw exception because workbook has nothing to print.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try
{
wb.save(baos, opts);
}
catch(Exception ex)
{
System.out.println("Exception Message: " + ex.getMessage());
}

异常

 Exception in thread "main" com.aspose.cells.CellsException: There is nothing to output/print.

	at com.aspose.cells.zcab.a(Unknown Source)

	at com.aspose.cells.zcab.a(Unknown Source)

	at com.aspose.cells.zcab.a(Unknown Source)

	at com.aspose.cells.Workbook.a(Unknown Source)

	at com.aspose.cells.Workbook.save(Unknown Source)