Evitare pagine vuote nell'output PDF quando non c'è niente da stampare

Possibili scenari di utilizzo

Quando il file Excel è vuoto e l’utente lo salva in PDF utilizzando Aspose.Cells, esegue il rendering di una pagina vuota nell’output PDF. A volte, questo comportamento predefinito è indesiderato. Aspose.Cells fornisce ilPdfSaveOptions.OutputBlankPageWhenNothingToPrint proprietà per affrontare questo problema. Se lo imposterai comefalso, poiCellsExceptionsi verificherà ogni volta che non c’è niente da stampare nell’output PDF.

Evitare pagine vuote nell’output PDF quando non c’è niente da stampare

Il codice di esempio seguente crea una cartella di lavoro vuota e quindi la salva come PDF dopo aver impostato ilPdfSaveOptions.OutputBlankPageWhenNothingToPrint proprietà comefalso. Poiché non c’è nulla da stampare nell’output PDF, ilCellsExceptionavviene come mostrato di seguito.

Codice d’esempio

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

Eccezione

 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)