通過Java從PDF中提取圖像

从 PDF 文档中提取图像。使用 Aspose.PDF for Java 以编程方式修改 PDF 文件

Aspose.PDF for Java Logo

使用 Java 工具从 PDF 文档中提取图像

为了从 PDF 中提取图像,我们将使用 Aspose.PDF for Java API,这是一款适用于 Java 平台的功能丰富、强大且易于使用的转换 API。你可以直接从 Maven 下载其最新版本,然后通过向pom.xml添加以下配置将其安装在基于 Maven 的项目中。

Repository

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java AP</name>
    <url>https://releases.aspose.com/java/repo/</url>
</repository>
    

Dependency

<dependency>
  <groupId>com.aspose</groupId>
  <artifactId>aspose-pdf</artifactId>
  <version>version of aspose-pdf API</version>
</dependency>

通過Java從PDF中提取圖像


你需要 Aspose.PDF for Java 才能在你的环境中试用代码。

  1. 開啟 PDF 文件。
  2. 提取特定影像。
  3. 保存輸出影像。
  4. 儲存更新後的 PDF 檔案。

从 PDF 文件中提取图像-Java

此示例代码说明如何从 PDF 中提取图像-Java

try {
    // Open document
    Document pdfDocument = new Document(DATA_DIR.resolve("ExtractImages.pdf").toString());

    // Extract a particular image
    XImage xImage = pdfDocument.getPages().get_Item(1).getResources().getImages().get_Item(1);

    FileOutputStream outputImage = new FileOutputStream(DATA_DIR.resolve("output.jpg").toString());

    // Save output image
    xImage.save(outputImage, ImageType.getJpeg());
    outputImage.close();

    // Save updated PDF file
    pdfDocument.save(DATA_DIR.resolve("ExtractImages_out.pdf").toString());
} catch (Exception e) {
    System.out.println(e.getMessage());
}