IRONSOFTWAREHOME
图片转PDF

import com.ironsoftware.ironpdf.*;  
import java.io.IOException;  
import java.nio.file.*;  
import java.util.ArrayList;  
import java.util.List;  

// Reference to the directory containing the images that we desire to convert
Path imageDirectory = Paths.get("assets/images");

// Create an empty List to contain Paths to images from the directory.
List<Path> imageFiles = new ArrayList<>();

// Use a DirectoryStream to populate the List with paths for each image in the directory
// that we want to convert (this example uses a glob pattern to target only png and jpg images)
try (DirectoryStream<Path> stream = Files.newDirectoryStream(imageDirectory, "*.{png,jpg}")) {
    for (Path entry : stream) {
        imageFiles.add(entry);
    }

    // Render all targeted images as PDF content and save them together in one PDF document.
    PdfDocument.fromImage(imageFiles).saveAs(Paths.get("assets/composite.pdf"));
} catch (IOException exception) {
    throw new RuntimeException(String.format("Error converting images to PDF from directory: %s: %s",
            imageDirectory,
            exception.getMessage()),
            exception);
}
mvn install
图片转PDF

图片转PDF

开发人员可以使用 IronPDF 将多个图像合并为单个 PDF 文档。

Path对象列表。 这给开发人员在特殊性上提供了很大的自由度。 开发人员可以构建此列表,以包含跨多个目录的图像组,包含符合特定命名或类型标准的图像等。如果图像来自同一目录(如以下示例所示),开发人员可以将java.nio.file类一起使用,快速构建该目录中包含的所有图像列表。

PdfDocument.fromImage将在PDF文档中每个单独页面上渲染列表中参考的每张图像。

如果需要将图像放在不同的 PDF 文档中(而不是合并为一个),那么开发人员可以执行以下任一操作:

  1. 使用只包含一个PdfDocument.fromImage。
  2. 如上例所示,将所有图像整合为一个PDF文档,然后使用PdfDocument.copyPage方法复制每个页面到新的PDF文档中。
学习用 Java 将 PDF 转换为图像
在 GitHub 上查看

准备开始了吗?

版本:2026.6刚刚发布

Key in blue circle

立即获取免费的 30 天试用版密钥。

Your trial license will be sent to your email address

无任何限制。100% 解锁。无需信用卡。

OR
bullet_checked无需信用卡或创建账户无任何限制。100% 解锁。无需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried Iron Suite
预约您的免费现场演示
Booking Badge

深受全球数百万工程师信赖

Iron Software 的客户徽标
获取您的无义务咨询
填写下面的表格或通过sales@ironsoftware.com
您的资料将始终保密。
深受全球数百万工程师信赖
Iron Software 的客户徽标
立即获取您的免费30 天试用密钥。
无需信用卡或创建账户
Java Maven PDF 库
using Maven 安装

版本: 2026.6

<dependency>
   <groupId>com.ironsoftware</groupId>
   <artifactId>ironpdf</artifactId>
   <version>2026.6.1</version>
</dependency>
https://central.sonatype.com/artifact/com.ironsoftware/ironpdf/2026.6.1
or
Java PDF JAR
下载 JAR

版本: 2026.6

手动安装到您的项目中