Java pdf使用aspose转图片后再转pdf再压缩代码
时间: 2023-02-14 18:08:42 浏览: 202
下面是使用 Aspose 库将 PDF 转换为图片并压缩的示例代码:
```java
import com.aspose.pdf.*;
// Load the PDF document
Document pdfDocument = new Document("input.pdf");
// Convert each page of the PDF to image
for (int i = 1; i <= pdfDocument.getPages().size(); i++) {
// Create an image object
com.aspose.pdf.Image image = new com.aspose.pdf.Image();
// Convert the page to image
image.bind(pdfDocument.getPages().get_Item(i));
// Save the image
image.save("image" + i + ".jpg");
}
// Re-create the pdf
Document doc = new Document();
for (int i = 1; i <= pdfDocument.getPages().size(); i++) {
Image image = Image.getInstance("image" + i + ".jpg");
doc.newPage();
doc.add(image);
}
// Save the PDF
doc.save("output.pdf");
// Compress the PDF
PdfFileEditor pdfEditor = new PdfFileEditor();
pdfEditor.compress("output.pdf", "compressed.pdf", CompressionEnum.Flate);
```
请注意,在使用上面的代码之前,您需要在项目中导入 Aspose PDF for Java 库。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)