Aspose-cells Excel convert to PDF to follow Print Settings

Hi, I have an excel file that I want to convert to pdf. Is there a way for me to convert to pdf where-in it will follow how it is displayed in the Print Preview (Settings) / Print to PDF?

Currently, some pages that are meant for page 2 are appearing in the page 1 of the converted pdf result.
Excel Print to PDF:
image.jpg (103.1 KB)

PDF Page 1
image.png (83.6 KB)

Aspose version:
cells: 23.10
pdf: 23.10

See below test files and output:
Test_files.zip (366.7 KB)

Below is the code snippet I am trying, but it doesn’t seem to be meant for it.

Workbook workbook = new Workbook(sourceFilePath);
PdfSaveOptions pdfOptions = new PdfSaveOptions();
pdfOptions.setExportDocumentStructure(true);
//pdfOptions.setAllColumnsInOnePagePerSheet(true);
workbook.save(finalSourcePath, pdfOptions);
			
try (com.aspose.pdf.Document document = new com.aspose.pdf.Document(finalSourcePath)) {
       document.save(finalSourcePath, com.aspose.pdf.SaveFormat.Pdf);
}

Is there a way for this to be achieved?

Thanks!

@sysnetnovahubcom
Please try to use

worksheet.autoFitRows(true); 

Hope helps a bit!

1 Like

@sysnetnovahubcom,

Here is the complete sample code segment that works fine as I tested, the output PDF file is fine tuned.
e.g.
Sample code:

Workbook workbook = new Workbook("f:\\files\\testExcel.xlsx");

Worksheet worksheet = workbook.getWorksheets().get(0);
worksheet.autoFitRows(true);

PdfSaveOptions pdfOptions = new PdfSaveOptions();
pdfOptions.setExportDocumentStructure(true);
//pdfOptions.setAllColumnsInOnePagePerSheet(true);
workbook.save("f:\\files\\out1.pdf", pdfOptions);

For your information, there are some rows in your Excel file whose height is auto (which means MS Excel will auto-fit those rows data when opening the Excel file into it). In Aspose.Cells, we do provide Worksheet.autoFitRows(true) API, setting “true” would auto-fit those rows only whose height are auto. Please try to add a couple of lines (as above) to your sample code and it will work fine.

1 Like

The autofitRows worked! Thank you so much!

@sysnetnovahubcom
You are welcome!