Hi,
We are currently using Aspose.Cells for Java v25.6 to generate PDF files from Excel documents. In our Excel files, we use MS ゴシック for text and Arial for numeric values.
However, we’ve observed several discrepancies between the PDFs generated via Aspose and those produced using the “Save as Adobe PDF” option:
- Font Size Variations: Some text appears smaller or larger than in the original Excel file.
- Number Formatting Issues: Certain numeric values display with inconsistent height, and some appear as superscript.
- Page Scaling Differences: The overall page layout and scaling differ from the Adobe-generated PDF.
Our goal is to ensure that the PDF output matches the Excel file exactly in terms of font size, style, and page scaling.
Could you please advise on how we can resolve these issues using Aspose?
For your reference, I’ve attached:
- The code snippet used in our application
- The original Excel file and The generated PDF file in Zip
The code we are using:
Worksheet worksheet = workbook.getWorksheets().get("Design");
PageSetup pageSetup = worksheet.getPageSetup();
pageSetup.setPrintQuality(1200);
pageSetup.setPaperSize(PaperSizeType.PAPER_A_4);
worksheet.getPageSetup().setTopMarginInch(0.2);
worksheet.getPageSetup().setBottomMarginInch(0.2);
worksheet.getPageSetup().setRightMargin(0.52);
worksheet.getPageSetup().setLeftMargin(0.52);
worksheet.getPageSetup().setHeaderMargin(0);
worksheet.getPageSetup().setFooterMargin(0);
worksheet.getPageSetup().setFitToPagesWide(1);
worksheet.getPageSetup().setFitToPagesTall(0);
worksheet.getPageSetup().setZoom(100);
worksheet.getPageSetup().setPercentScale(true);
PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.setOptimizationType(PdfOptimizationType.STANDARD);
saveOptions.setCheckFontCompatibility(true);
saveOptions.setCompliance(PdfCompliance.PDF_16);
workbook.calculateFormula();
workbook.save(pdfFilePath, saveOptions);
File Attachment
files.zip (264.1 KB)
@rakib786
To address the discrepancies you’re experiencing when converting Excel files to PDF using Aspose.Cells for Java, follow these steps to improve the output quality and ensure it closely matches the original Excel document:
-
Cause - Font Compatibility: Ensure that the fonts used in your Excel file are available on the system where the conversion is taking place. If the fonts are not installed, Aspose may substitute them with default fonts, leading to size and style discrepancies. You can check the font compatibility by setting saveOptions.setCheckFontCompatibility(true);
in your code, which you already have.
-
Solution - Font Size and Style: If you notice variations in font size, explicitly set the font size in your Excel file or adjust it in the code before saving. Additionally, use the PdfSaveOptions
to set the TextCompression
property to PdfTextCompression.Flate
to enhance rendering.
-
Solution - Page Scaling: To ensure the page layout matches the original Excel file, adjust the FitToPagesWide
and FitToPagesTall
settings. For example, to maintain the original scaling, set:
worksheet.getPageSetup().setFitToPagesWide(1);
worksheet.getPageSetup().setFitToPagesTall(1);
-
Solution - Print Quality: You have set the print quality to 1200 DPI, which is good for high-quality output. If the output is still not satisfactory, experiment with different DPI settings to see if it affects the rendering.
-
Solution - Margins and Zoom: Ensure that the margins and zoom settings are appropriate for your document. Adjust these settings in the PageSetup
as needed.
Here’s a refined version of your code snippet with some adjustments:
Worksheet worksheet = workbook.getWorksheets().get("Design");
PageSetup pageSetup = worksheet.getPageSetup();
pageSetup.setPrintQuality(1200);
pageSetup.setPaperSize(PaperSizeType.PAPER_A_4);
pageSetup.setTopMarginInch(0.2);
pageSetup.setBottomMarginInch(0.2);
pageSetup.setRightMargin(0.52);
pageSetup.setLeftMargin(0.52);
pageSetup.setHeaderMargin(0);
pageSetup.setFooterMargin(0);
pageSetup.setFitToPagesWide(1);
pageSetup.setFitToPagesTall(1); // Adjusted for better scaling
pageSetup.setZoom(100);
pageSetup.setPercentScale(true);
PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.setOptimizationType(PdfOptimizationType.STANDARD);
saveOptions.setCheckFontCompatibility(true);
saveOptions.setCompliance(PdfCompliance.PDF_16);
workbook.calculateFormula();
workbook.save(pdfFilePath, saveOptions);
Next Steps: By following these steps and making the suggested adjustments, you should see improvements in the PDF output quality. If issues persist, check the original Excel file for any specific formatting that might not be supported during conversion.
Please let me know if you need further assistance!
@rakib786
By testing with sample files and code on the latest version v25.7, we can reproduce the issue. Certain numeric values display with inconsistent height and some values appear as superscript. Please refer to the attachment. 2.png (188.2 KB)
Workbook workbook = new Workbook(filePath + "Excel.xlsx");
Worksheet worksheet = workbook.getWorksheets().get("Design");
PageSetup pageSetup = worksheet.getPageSetup();
pageSetup.setPrintQuality(1200);
pageSetup.setPaperSize(PaperSizeType.PAPER_A_4);
worksheet.getPageSetup().setTopMarginInch(0.2);
worksheet.getPageSetup().setBottomMarginInch(0.2);
worksheet.getPageSetup().setRightMargin(0.52);
worksheet.getPageSetup().setLeftMargin(0.52);
worksheet.getPageSetup().setHeaderMargin(0);
worksheet.getPageSetup().setFooterMargin(0);
worksheet.getPageSetup().setZoom(100);
worksheet.getPageSetup().setPercentScale(true);
PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.setOptimizationType(PdfOptimizationType.STANDARD);
saveOptions.setCheckFontCompatibility(true);
saveOptions.setCompliance(PdfCompliance.PDF_16);
workbook.calculateFormula();
workbook.save(filePath + "out_java.pdf", saveOptions);
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): CELLSJAVA-46445
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.
@rakib786
By testing with sample files and code on the latest version v25.7, we can reproduce the issue. Some text appears smaller or larger than in the original Excel file. Please refer to the attachment. 1.png (228.7 KB)
If you have any additional information regarding this screenshot, please provide a screenshot and highlight the incorrect location. This will be very helpful for us to locate and fix the issue.
Workbook workbook = new Workbook(filePath + "Excel.xlsx");
Worksheet worksheet = workbook.getWorksheets().get("Design");
PageSetup pageSetup = worksheet.getPageSetup();
pageSetup.setPrintQuality(1200);
pageSetup.setPaperSize(PaperSizeType.PAPER_A_4);
worksheet.getPageSetup().setTopMarginInch(0.2);
worksheet.getPageSetup().setBottomMarginInch(0.2);
worksheet.getPageSetup().setRightMargin(0.52);
worksheet.getPageSetup().setLeftMargin(0.52);
worksheet.getPageSetup().setHeaderMargin(0);
worksheet.getPageSetup().setFooterMargin(0);
worksheet.getPageSetup().setZoom(100);
worksheet.getPageSetup().setPercentScale(true);
PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.setOptimizationType(PdfOptimizationType.STANDARD);
saveOptions.setCheckFontCompatibility(true);
saveOptions.setCompliance(PdfCompliance.PDF_16);
workbook.calculateFormula();
workbook.save(filePath + "out_java.pdf", saveOptions);
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): CELLSJAVA-46446
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.
@rakib786
By testing with sample files and code on the latest version v25.7, we can reproduce the issue. The overall page layout and scaling differ from the Adobe-generated PDF.
Workbook workbook = new Workbook(filePath + "Excel.xlsx");
Worksheet worksheet = workbook.getWorksheets().get("Design");
PageSetup pageSetup = worksheet.getPageSetup();
pageSetup.setPrintQuality(1200);
pageSetup.setPaperSize(PaperSizeType.PAPER_A_4);
worksheet.getPageSetup().setTopMarginInch(0.2);
worksheet.getPageSetup().setBottomMarginInch(0.2);
worksheet.getPageSetup().setRightMargin(0.52);
worksheet.getPageSetup().setLeftMargin(0.52);
worksheet.getPageSetup().setHeaderMargin(0);
worksheet.getPageSetup().setFooterMargin(0);
worksheet.getPageSetup().setZoom(100);
worksheet.getPageSetup().setPercentScale(true);
PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.setOptimizationType(PdfOptimizationType.STANDARD);
saveOptions.setCheckFontCompatibility(true);
saveOptions.setCompliance(PdfCompliance.PDF_16);
workbook.calculateFormula();
workbook.save(filePath + "out_java.pdf", saveOptions);
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): CELLSJAVA-46447
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.
1 Like
@rakib786
Additionally, please check the method setPercentScale(boolean value) in the sample code. If this property is False, the FitToPagesWide and FitToPagesTall properties control how the worksheet is scaled.
If this property is True, the FitToPagesWide and FitToPagesTall properties will be ignored and will not work.
I have tried in this way, but it is not working.
Hi,
There is similar issue in font spacing.
Some lines are overlapping with other line. Some texts are touching with other line’s text.
I have attached the excel & pdf file in zip
files.zip (1.4 MB)
could you please suggest me how I can fix the spacing issue?
Thank you in advance!
@rakib786 ,
Since your code modified the pagesetup, so we need to save file to Excel file before saving to pdf. Then open the saved Excel xlsx file, save it to pdf by Excel for comparing. Also, I added the code worksheet.autoFitRows(true);
, it only autofits the height of the row that is autoheight(Excel does the same thing when you open source file in Excel).
The code will be:
Workbook workbook = new Workbook("Excel.xlsx");
workbook.calculateFormula();
Worksheet worksheet = workbook.getWorksheets().get("Design");
//Only autofit the height of the Row that is auto height.
worksheet.autoFitRows(true);
PageSetup pageSetup = worksheet.getPageSetup();
pageSetup.setPrintQuality(1200);
pageSetup.setPaperSize(PaperSizeType.PAPER_A_4);
worksheet.getPageSetup().setTopMarginInch(0.2);
worksheet.getPageSetup().setBottomMarginInch(0.2);
worksheet.getPageSetup().setRightMargin(0.52);
worksheet.getPageSetup().setLeftMargin(0.52);
worksheet.getPageSetup().setHeaderMargin(0);
worksheet.getPageSetup().setFooterMargin(0);
//worksheet.getPageSetup().setFitToPagesWide(1);
//worksheet.getPageSetup().setFitToPagesTall(0);
worksheet.getPageSetup().setZoom(100);
worksheet.getPageSetup().setPercentScale(true);
//Save it Excel file before converting to pdf.
workbook.save("output.xlsx");
PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.setOptimizationType(PdfOptimizationType.STANDARD);
saveOptions.setCheckFontCompatibility(true);
saveOptions.setCompliance(PdfCompliance.PDF_16);
workbook.save("output_by_Aspose.pdf", saveOptions);
Please note, Excel has different output/page layout with different System display scale setting(e.g. 100%, 150%, 200%). Apsose.Cells only tries to generate the same output as the one generated by Excel with system display scale setting 100%. Please check the attache files, the Page Scaling is same for Aspose and Excel generated pdf files.
output.zip (354.1 KB)
@rakib786
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): CELLSJAVA-46450
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.
@rakib786
Your issue(CELLSJAVA-46450) has been resolved and the fixed functionality will be released with version 25.8.
Here is the resulting file generated using the latest function:
CELLSJAVA-46450_java.pdf (19.3 KB)
The issues you have found earlier (filed as CELLSJAVA-46450) have been fixed in Aspose.Cells for Java 25.8.
@rakib786
Your issue( CELLSJAVA-46445) has been resolved and the fixed functionality will be released with version 25.9.
Here is the resulting file generated using the latest function:
CELLSJAVA-46445_java.pdf (92.1 KB)
1 Like
The issues you have found earlier (filed as CELLSJAVA-46445) have been fixed in Aspose.Cells for Java 25.9.