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

可能的使用场景

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

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

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

示例代码

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
//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.OutputBlankPageWhenNothingToPrint = false;
//Save workbook to Pdf format, it will throw exception because workbook has nothing to print.
MemoryStream ms = new MemoryStream();
try
{
//Save to Pdf format. It will throw exception.
wb.Save(ms, opts);
}
catch (Exception ex)
{
Console.Write("Exception Message: " + ex.Message + "\r\n");
}

异常

 Aspose.Cells.CellsException was unhandled

  HResult=-2146232832

  Message=There is nothing to output/print.

  Source=Aspose.Cells

  StackTrace:

       at Aspose.Cells.Workbook.Save(String fileName, SaveOptions saveOptions)