Free Support Forum - aspose.com

Workbook.Save to PDF from Empty Spreadsheet

Hello,

When I open a spreadsheet with empty tabs (no data) and use Workbook.Save(PDF) I get a single page, blank PDF.

Is this by design?

If I try to export or print this spreadsheet in Excel it prompts with an error message “Nothing
to print”.

Thanks,

Sheri

@sheri_steeves

Thanks for using Aspose APIs.

We were able to observe this issue and logged it in our database for evaluation and implementation or fix. Once, we will have some news for you, we will share it with you asap.

This issue has been logged as

  • CELLSNET-46032 - Do not generate single page blank PDF when Excel file is empty

@sheri_steeves

Thanks for using Aspose APIs.

We will add API OutputBlankPageWhenNothingToPrint to PdfSaveOptions, just like the existing API ImageOrPrintOptions.OutputBlankPageWhenNothingToPrint. But the default value of PdfSaveOptions.OutputBlankPageWhenNothingToPrint will be true to keep same behavior as old version. If the user sets PdfSaveOptions.OutputBlankPageWhenNothingToPrint to false, an exception will be thrown when there is nothing to print.

@sheri_steeves,

Please try our latest Aspose.Cells for .NET v18.3.9.

We added a new API: PdfSaveOptions.OutputBlankPageWhenNothingToPrint in the fix v18.3.9:
Sample code:

Workbook wb = new Workbook(srcFile);
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.OutputBlankPageWhenNothingToPrint = false;
wb.Save(outFile.pdf, pdfSaveOptions); 

Let us know your feedback.

Perfect, I’ll give it a try shortly.

Worked a treat! Thanks!

@sheri_steeves,

Good to know that your issue is sorted out by the new fix/version. Feel free to contact us any time if you need further help or have some other issue or queries, we will be happy to assist you soon.

The issues you have found earlier (filed as CELLSNET-46032) have been fixed in Aspose.Cells for .NET 18.4. Please also check the document/article for your reference: https://docs.aspose.com/display/cellsnet/Installation

hello, i have un excel file with empty or black pages. i set OutputBlankPageWhenNothingToPrint = false
but the white page still rendred in pdf file???
I test with 20.12 and 21.4 version

@Magali_ISAIA,
I have tried this scenario with the latest version 21.4.x and observed that if the Excel file is blank and this property is set to false, then instead of saving the PDF it raises following exception. Hence it can be handled to avoid creating such PDF file.

There is nothing to output/print.

Moreover if you want to get print preview information before rendering the Excel file to PDF or image format, you may try using WorkbookPrintingPreview for complete workbook or SheetPrintingPreview for a specific sheet. It will return zero number of pages for such blank workbook that can be used to control creation of such PDF file. Here is an example code that demonstrates all these features:

Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.OutputBlankPageWhenNothingToPrint = false;
try
{
    workbook.Save("output.pdf", pdfSaveOptions);
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
WorkbookPrintingPreview preview = new WorkbookPrintingPreview(workbook, imgOptions);
Console.WriteLine("Workbook page count: " + preview.EvaluatedPageCount);

SheetPrintingPreview preview2 = new SheetPrintingPreview(workbook.Worksheets[0], imgOptions);
Console.WriteLine("Worksheet page count: " + preview2.EvaluatedPageCount);

Program Output

There is nothing to output/print.
Workbook page count: 0
Worksheet page count: 0

@Magali_ISAIA

It indicates that the source file indeed has a blank page(e.g. BlankPage.zip (6.8 KB)) instead of it has no page to output(e.g. NothingToPrint.zip (5.3 KB))

Code:

Workbook wb = new Workbook(srcFile);

PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.OutputBlankPageWhenNothingToPrint = false;

wb.Save(outFile.pdf, pdfSaveOptions);

If you still have issues, please share us the source file.