出力PDFの空白ページを回避する

可能な使用シナリオ

Excelファイルが空で、そのファイルをAspose.CellsでPDFに保存すると、出力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-.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)