Spire.XLS is a professional Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets. Get free and professional technical support for Spire.XLS for .NET, Java, Android, C++, Python.

Thu Feb 11, 2021 4:16 am

I have the following code to convert an Excel file to PDF file. Page size is correct but the content is not covering the full page.
Workbook workbook = new Workbook();
FileInputStream fileInputStream = new FileInputStream(attachmentFile);
workbook.loadFromStream(fileInputStream);
//workbook.getConverterSetting().setSheetFitToPage(true);
WorksheetsCollection worksheets = workbook.getWorksheets();
Worksheet worksheet;
PageSetup pageSetup;
for (int i = 0; i < worksheets.size(); i++) {
worksheet = worksheets.get(i);
pageSetup = worksheet.getPageSetup();
pageSetup.setHeaderMarginInch(.5);
pageSetup.setFooterMarginInch(.5);
pageSetup.setTopMargin(.5);
pageSetup.setBottomMargin(.5);
pageSetup.setLeftMargin(.5);
pageSetup.setRightMargin(.5);
//pageSetup.isFitToPage(true);
pageSetup.setOrientation(PageOrientationType.Portrait);
pageSetup.setPaperSize(PaperSizeType.PaperLetter);
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
workbook.saveToStream(outputStream, FileFormat.PDF);
byte pdfFileBytes[] = outputStream.toByteArray();

Where am I going wrong?
Attachments
Archive 2.zip
I have attached the original Excel file and the exported PDF file.
(1.04 MiB) Downloaded 193 times

nathanthiya
 
Posts: 1
Joined: Thu Feb 11, 2021 3:32 am

Thu Feb 11, 2021 11:57 am

Hello,

Thanks for your inquiry.
After setting the "pageSetup" with the code you provided, I temporarily saved an Excel document, and then opened it in Microsoft Excel, I found that the pagination of the document changed, as shown in the attached picture "comparison.png". After further analysis, I found that this is caused by the left and right margins you set too large, please set a smaller value like the following.
Code: Select all
            pageSetup.setLeftMargin(.2);
            pageSetup.setRightMargin(.2);

But unfortunately, I found that after adjusting the left and right margins to an appropriate value, the PDF content generated by our product is still inconsistent with the Excel document. I have logged this issue in our bug tracking system with the ticket SPIREXLS-3119. We will let you know if there is any update. Sorry for the inconvenience caused.

Sincerely,
Rachel
E-iceblue support team
Attachments
comparison.png
comparison.png (128.12 KiB) Viewed 897 times
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Return to Spire.XLS