I use the following code to export a excel to pdf:
Workbook workbook = new Workbook(sourcePath);
WorksheetCollection collection = workbook.getWorksheets();
for (int i = 0; i
Worksheet sheet = collection.get(i);
sheet.getCells().deleteBlankColumns();
sheet.getCells().deleteBlankRows();
}
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.setAllColumnsInOnePagePerSheet(true);
workbook.save(targetPath, pdfSaveOptions);
But the output pdf still contains one blank page.
The file is attached.
And is it possible to contain sheet name to the output pdf file?
Hi Huangfu,
Thanks for your posting and using Aspose.Cells.
Please download and try the latest version: Aspose.Cells for Java (Latest Version) and see if it resolves your issue.
If your issue still persists, then please provide us your source Excel file which you are converting to PDF. We will look into your issue and help you asap.
If you want to show the sheet name inside the PDF page, then you will have to set the Header of the sheet with its name. Please see the following article that explains how to set Header & Footer of worksheet.
8.2.1.4 will still generate the empty page.
The source file is attached
And the header solution solved my second problem perfectly.
Still a problem with header.
The same source file, use the following command to set the header:sheet.getPageSetup().setHeader(1, “&A”);
The last page’s header will be lower than the real content. See the attachment
Hi Huangfu,
String fname = “预算.xlsx”;
Workbook wb = new Workbook(myDir + fname);
int worksheetCount = wb.getWorksheets().getCount();
for (int i = 0 ; i < worksheetCount; i++)
{
Worksheet sheet = wb.getWorksheets().get(i);
sheet.getCells().deleteBlankColumns();
sheet.getCells().deleteBlankRows();
}
PdfSaveOptions options = new PdfSaveOptions();
options.setOnePagePerSheet(true);
wb.save(myDir + fname + “.pdf”, options);
Actually, in your output pdf, there are still lots of blank at the bottom of the first page.
deleteBlankRows seems failed to recognize those blanks.
And for our business logic, the sheet may be very long. So we have to use AllColumnsInOnePagePerSheet, so that empty pages appear.
And for the headers, you can carefully examine your output pdf, the original content is much higher in the last page than in the others. If you set the header, the header in the last page will below the original content.
Hi Huangfu,
for (int i = 0 ; i < worksheetCount; i++)
{
Worksheet sheet = wb.getWorksheets().get(i);
sheet.getCells().deleteBlankColumns();
sheet.getCells().deleteBlankRows();
sheet.getPageSetup().setPrintArea("");
}
After setting the print area, it’s ok now.
Thanks a lot.
Hi Huangfu,