IRONSOFTWAREHOME
Images To 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
Images To PDF

Images To 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天試用金鑰。
無需信用卡或帳戶建立
PDF的Java Maven程式庫
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

手動安裝到您的專案中