重新采样图像以将Excel转换为PDF
Contents
[
Hide
]
在处理包含大量图像的大型Microsoft Excel文件时,您可能需要压缩已添加的图像以减小输出PDF文件大小并提高整体转换性能。Aspose.Cells支持重新采样添加的图像以减小输出PDF文件大小并提高性能。
重新采样图像以将Excel转换为PDF
请参阅以下示例代码,描述如何使用Aspose.Cells API执行该任务。该示例将Microsoft Excel文件转换为PDF文件,并压缩文件中的图像。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(ResampleImagesforExceltoPDFConversion.class); | |
// Initialize a new Workbook | |
// Open an Excel file | |
Workbook workbook = new Workbook(dataDir + "Book1.xlsx"); | |
// Instantiate the PdfSaveOptions | |
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions(); | |
// Set Image Resample properties | |
pdfSaveOptions.setImageResample(300, 70); | |
// Save the PDF file | |
workbook.save(dataDir + "output.pdf", pdfSaveOptions); |
使用PdfSaveOptions.setImageResample选项可以最大限度地减小输出PDF的大小,但可能会对图像质量产生一些影响。
如果您的电子表格包含公式,最好在将电子表格呈现为PDF格式之前调用 Workbook.calculateFormula()。这样做将确保重新计算依赖于公式的值,并在PDF中呈现正确的值。