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 seguente codice di esempio crea una cartella di lavoro vuota e quindi la salva come output 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-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());
}

Eccezione

 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)