问题描述:
使用Aspose.Cells转Excel为PDF,出现本应该在一页的结果分页出错。
WPS正常打印效果:
第一页:
image.png (106.8 KB)
第二页:
image.png (102.4 KB)
Aspose.Cells错误效果:
第一页:
image.png (66.1 KB)
问题:第二页的前两行被搞到了第一页。
代码:
块引用
Workbook workbook = new Workbook(excelFilePath);
PdfSaveOptions options = new PdfSaveOptions
{
OnePagePerSheet = false,
AllColumnsInOnePagePerSheet = false,
PrintingPageType = PrintingPageType.Default,
Compliance = PdfCompliance.PdfA1b
};
workbook.Save(pdfOutputPath, options);
测试过程:
我们有很多excel不同的模板,有些aspose代码固定死页边距倒是就好了,但是不能自适应啊。
设备环境:
操作系统:win11
显示器分辨率:125%
版本号:
image.png (7.8 KB)
@jeffer88
Cause
The issue you’re experiencing with Aspose.Cells converting Excel to PDF, where content that should fit on one page is incorrectly split across multiple pages, is often due to the page setup settings or the way Aspose.Cells handles content rendering compared to other applications like WPS Office.
Solution
To resolve the pagination issue, adjust the page setup properties in your code. Specifically, set the OnePagePerSheet
and AllColumnsInOnePagePerSheet
options according to your requirements. Here’s a refined approach to your existing code:
Code
Workbook workbook = new Workbook(excelFilePath);
PdfSaveOptions options = new PdfSaveOptions
{
OnePagePerSheet = false, // Set to true if you want all content on one page
AllColumnsInOnePagePerSheet = false, // Set to true if you want all columns on one page
PrintingPageType = PrintingPageType.Default,
Compliance = PdfCompliance.PdfA1b,
// You can also set other properties like page margins if needed
};
// Save the workbook as a PDF
workbook.Save(pdfOutputPath, options);
Next Steps
- Adjust Page Margins: If the content still overflows, adjust the page margins in the Excel file itself or programmatically before saving.
- Check Content Size: Ensure that the content in your Excel sheets is not too large for the page size you are using. You may need to reduce font sizes or adjust column widths.
- Test with Different Settings: Experiment with the
OnePagePerSheet
and AllColumnsInOnePagePerSheet
settings to find the best combination for your specific templates.
- Review Excel Templates: Ensure that all your templates are consistently set up regarding page layout and content size.
If you implement these changes and continue to face issues, please share more details about your Excel templates or any specific settings you are using. I’m here to help!
@jeffer88
通过使用样例文件进行测试,我们发现当系统显示的缩放与布局设置为125%时,在WPS里对文件进行打印预览,可以得到想要的分页效果。请查看附件。wps_125%display.png (64.4 KB)
当系统显示的缩放与布局设置为100%时,在WPS里对文件进行打印预览,会出现第二页的前两行被放置到了第一页末尾。这种情况和Aspose.Cells导出结果一致。请查看附件。wps_100%display.png (59.2 KB)
Aspose.Cells的PDF导出结果一直和缩放与布局设置为100%时的打印阅览结果保持一致。为了得到和打印预览相同的结果。请在系统显示设置为100%的比例下编辑和预览文件。
是的,正如您反馈的,那么请问是否有方式可以修改Aspose的DPI为125%,以便避免修改所有的Excel模板,客户的Excel模板大约有100多个,全部修改工作量巨大。
@jeffer88
感谢你的反馈。很抱歉,目前无法通过修改Cells的DPI使得pdf导出结果和显示设置为125%时软件预览一致。作为临时解决方案,请在文件需要分页的地方手动设置分页符。
我们已经在内部问题跟踪系统中打开了以下新问题单,并将根据Free Support Policies中提到的条款提供修复。
问题单号:CELLSNET-58742
另外,这种需求短期很难支持,我们建议优先修改文件并在需要的地方设置分页符来解决问题。关于新需求,一旦有任何更新,我们将及时通知你。
好吧,那确实很遗憾,因为Excel都是我们业务系统生成的,插入分页符有时候并不是很方便。
那我们先把一些业务切换到Office的COM导出PDF,期待咱们aspose后续的更新。
感谢及时的信息回复~~
@jeffer88
感谢你的反馈。不客气。如果你有任何疑问,请随时联系我们 。