PDF 創建者 適用於 Java(分步教程)
本文將示範如何使用以 Java 程式語言編寫的 PDF 建立器建立 PDF 檔案。
使用 Java 建立 PDF 檔案在 IT 產業有許多應用。 有時,開發人員可能需要將 PDF 建立工具整合到他們的專案中。 本文討論的 PDF 建立工具是IronPDF 適用於 Java 。 它可以幫助開發者從頭開始建立新的 PDF 文檔,並將不同的文件格式轉換為 PDF 文件。
1. IronPDF for Java
IronPDF 適用於 Java 是一個用於建立、準備和管理 PDF 檔案的 Java 程式庫。 它允許開發人員讀取、產生和修改 PDF 文件,而無需安裝任何 Adobe 軟體。
IronPDF在從 PDF 文件建立 PDF 表單方面也表現出色。 IronPDF for Java 可以建立自訂頁首和頁尾、建立簽章、新增附件,以及新增密碼加密和安全機制。 IronPDF也支援多執行緒和非同步功能,以提高效能。
如何在Java中使用PDF創建器
- 安裝 Java 庫以建立 PDF 檔案。
- 使用renderHtmlAsPdf方法從 HTML 字串建立 PDF
- 將 HTML 檔案路徑傳遞給renderHtmlFileAsPdf方法以建立 PDF 檔案。
- 使用 Java 中的renderUrlAsPdf方法從 URL 連結建立 PDF 文件
- 利用fromImage方法從影像建立 PDF
2. 先決條件
在開始建立 PDF 檔案之前,必須先滿足一些先決條件。
- 系統中應安裝 Java,並在環境變數中設定其路徑。 如果您尚未安裝 Java,請參考此安裝指南進行安裝。
- 應該安裝一個好的 Java IDE,例如 Eclipse 或 IntelliJ。 若要下載 Eclipse,請造訪此下載頁面;若要安裝 IntelliJ,請點選此安裝頁面。
- 在開始 PDF 轉換之前,應將Maven整合到 IDE 中。 有關安裝Maven並將其整合到環境中的教程,請造訪JetBrains 提供的以下整合連結。
3. IronPDF Java 版安裝
一旦所有先決條件都滿足,即使對於 Java 新手開發人員來說,安裝IronPDF 適用於 Java 也非常簡單容易。
本文將使用 JetBrains IntelliJ IDEA 安裝程式庫並執行程式碼範例。
首先,打開 JetBrains IntelliJ IDEA 並建立一個新的Maven專案。
新建項目
將出現一個新視窗。 輸入項目名稱,然後點選完成。
設定新項目
點擊"完成"後,將開啟一個新項目,其中包含一個 pom.xml 文件,然後使用此文件新增一些Maven項目依賴項。
pom.xml 文件
在 pom.xml 檔案中新增以下相依性。
<dependencies>
<dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>ironpdf</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>ironpdf</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
在 pom.xml 檔案中放置依賴項後,檔案右上角會出現一個小圖示。
帶有安裝依賴項圖示的 pom.xml 文件
點選此圖示安裝IronPDF 適用於 Java 的Maven相依性。 這只需要幾分鐘,具體時間取決於您的網路連線速度。
4. 建立 PDF 文件
本節將討論如何使用IronPDF 適用於 Java 建立新的 PDF 文件。 IronPDF只需幾行程式碼即可輕鬆建立新的 PDF 文件。
使用IronPDF 適用於 Java 建立 PDF 檔案的方法有很多種。
- 此方法可以將[HTML](/java/object-reference/api/com/ironsoftware/ironpdf/PdfDocument.html#renderHtmlAsPdf(java.lang.String)字串轉換為 PDF 文件
- 此方法( [
renderHtmlFileAsPdf](/java/object-reference/api/com/ironsoftware/ironpdf/PdfDocument.html#renderHtmlFileAsPdf(java.lang.String) )可將 HTML 文件轉換為 PDF 文檔 renderUrlAsPdf方法可以直接從 URL 建立 PDF 文件,並將結果儲存到指定的資料夾中。
4.1. 使用 HTML 字串建立 PDF 文檔
下面的程式碼範例展示如何使用 renderHtmlAsPdf 方法將 HTML 標記字串轉換為 PDF。 請注意,使用 renderHtmlAsPdf 方法需要具備 HTML 的基本知識。
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) throws IOException {
License.setLicenseKey("YOUR-LICENSE-KEY"); // Set your IronPDF license key
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log")); // Set the path for log files
// Convert a simple HTML string into a PDF document
PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("<h1> ~Hello World~ </h1> Made with IronPDF!");
// Save the PDF document to the specified path
myPdf.saveAs(Paths.get("html_saved.pdf"));
}
}
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) throws IOException {
License.setLicenseKey("YOUR-LICENSE-KEY"); // Set your IronPDF license key
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log")); // Set the path for log files
// Convert a simple HTML string into a PDF document
PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("<h1> ~Hello World~ </h1> Made with IronPDF!");
// Save the PDF document to the specified path
myPdf.saveAs(Paths.get("html_saved.pdf"));
}
}
上述程式碼的輸出結果如下所示。
輸出文件
4.2. 從 HTML 文件建立 PDF 文件
IronPDF庫的 renderHtmlFileAsPdf 方法可讓您從 HTML 原始檔產生新的 PDF 檔案。此方法會將 HTML 文件中的所有內容(包括圖形、CSS、表單等)轉換為 PDF。
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) throws IOException {
License.setLicenseKey("YOUR-LICENSE-KEY"); // Set your IronPDF license key
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log")); // Set the path for log files
// Convert an HTML file into a PDF document
PdfDocument myPdf = PdfDocument.renderHtmlFileAsPdf("index.html");
// Save the PDF document to the specified path
myPdf.saveAs(Paths.get("html_saved.pdf"));
}
}
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) throws IOException {
License.setLicenseKey("YOUR-LICENSE-KEY"); // Set your IronPDF license key
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log")); // Set the path for log files
// Convert an HTML file into a PDF document
PdfDocument myPdf = PdfDocument.renderHtmlFileAsPdf("index.html");
// Save the PDF document to the specified path
myPdf.saveAs(Paths.get("html_saved.pdf"));
}
}
上述程式碼範例的輸出結果如下所示。
HTML 檔案的輸出文件
4.3. 從URL建立PDF文件
使用IronPDF,您可以根據網頁 URL 快速建立 PDF,精確度和準確度都非常高。 它還提供了從需要登入憑證才能存取的網站建立 PDF 文件的功能。
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) throws IOException {
License.setLicenseKey("YOUR-LICENSE-KEY"); // Set your IronPDF license key
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log")); // Set the path for log files
// Convert a web page URL into a PDF document
PdfDocument myPdf = PdfDocument.renderUrlAsPdf("https://www.alibaba.com/");
// Save the PDF document to the specified path
myPdf.saveAs(Paths.get("html_saved.pdf"));
}
}
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) throws IOException {
License.setLicenseKey("YOUR-LICENSE-KEY"); // Set your IronPDF license key
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log")); // Set the path for log files
// Convert a web page URL into a PDF document
PdfDocument myPdf = PdfDocument.renderUrlAsPdf("https://www.alibaba.com/");
// Save the PDF document to the specified path
myPdf.saveAs(Paths.get("html_saved.pdf"));
}
}
上述程式碼的輸出結果如下所示。
來自阿里巴巴網站的輸出 PDF 文件
4.4. 從影像建立 PDF 文件
IronPDF for Java 也可以從一個或多個影像建立 PDF 檔案。
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) throws IOException {
Path imageDirectory = Paths.get("assets/images"); // Define the directory to search for images
List<Path> imageFiles = new ArrayList<>();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(imageDirectory, "*.{png,jpg}")) {
for (Path entry: stream) {
imageFiles.add(entry); // Add each image file to the list
}
// Convert images into a composite PDF document
PdfDocument.fromImage(imageFiles).saveAs(Paths.get("assets/composite.pdf"));
} catch (IOException exception) {
// Handle the exception if an error occurs during image-to-PDF conversion
throw new RuntimeException(String.format("Error converting images to PDF from directory: %s: %s",
imageDirectory,
exception.getMessage()),
exception);
}
}
}
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) throws IOException {
Path imageDirectory = Paths.get("assets/images"); // Define the directory to search for images
List<Path> imageFiles = new ArrayList<>();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(imageDirectory, "*.{png,jpg}")) {
for (Path entry: stream) {
imageFiles.add(entry); // Add each image file to the list
}
// Convert images into a composite PDF document
PdfDocument.fromImage(imageFiles).saveAs(Paths.get("assets/composite.pdf"));
} catch (IOException exception) {
// Handle the exception if an error occurs during image-to-PDF conversion
throw new RuntimeException(String.format("Error converting images to PDF from directory: %s: %s",
imageDirectory,
exception.getMessage()),
exception);
}
}
}
上述程式碼的輸出結果如下所示。
從影像輸出的 PDF 文件
5. 結論
本文示範如何從頭開始建立 PDF,以及如何從現有的 HTML 檔案或 URL 建立 PDF。 IronPDF可讓您將多種格式轉換為高品質的 PDF,並簡化 PDF 檔案的操作和格式化流程。
IronPDF非常適合需要處理、修改或操作 PDF 文件的軟體開發人員和企業。 要了解有關IronPDF 適用於 Java 的更多信息,並獲取有關如何使用 Java 操作 PDF 的類似教程,請參閱以下官方文件。 有關使用 Java 建立 PDF 檔案的教學課程,請造訪此Java 程式碼範例。
IronPDF for Java 可免費用於開發用途,但商業用途需要許可證。 有關許可的更多信息,請訪問以下許可頁面。
常見問題解答
如何使用Java建立PDF檔案?
您可以使用IronPDF 適用於 Java 庫建立 PDF 文件,該庫允許您從頭開始生成 PDF,或將 HTML、URL 或圖像等各種格式轉換為 PDF 文件。
如何在 IntelliJ IDEA 中安裝 PDF 函式庫?
若要在 IntelliJ IDEA 中安裝IronPDF ,請建立一個 Maven 項目,並將 IronPDF 的 Maven 依賴項新增至pom.xml檔案中。然後,使用 IDE 管理安裝過程。
我可以用Java為PDF添加自訂頁首和頁尾嗎?
是的, IronPDF for Java 允許您使用其 API 方法新增頁首和頁尾來自訂 PDF 文件。
如何在Java中將HTML檔案轉換為PDF文件?
您可以使用IronPDF 適用於 Java 庫中的renderHtmlFileAsPdf方法將 HTML 檔案轉換為 PDF 文件。此方法會保留圖形、CSS 和表單。
是否可以使用 Java 對 PDF 檔案進行加密?
是的, IronPDF for Java 支援 PDF 檔案的密碼加密,讓您能夠有效地保護文件安全。
如何在Java中使用影像建立PDF文件?
IronPDF for Java 提供了一個名為fromImage方法,可讓您從一個或多個影像建立 PDF 文件。
IronPDF for Java 是否支援多執行緒?
是的, IronPDF for Java 包含了多執行緒和非同步功能,可以提高處理 PDF 文件時的效能。
如果我在Java中建立PDF失敗,我該怎麼辦?
請確保已正確安裝IronPDF 適用於 Java,且 Java 環境配置正確。檢查程式碼中是否有使用IronPDF方法的錯誤,並參考官方文件進行故障排除。
我可以用Java將網頁URL轉換為PDF嗎?
是的, IronPDF for Java 允許您使用renderUrlAsPdf方法將網頁 URL 轉換為 PDF 文件。
哪裡可以找到學習如何使用IronPDF 適用於 Java 的資源?
您可以在IronPDF官方文件頁面上找到有關使用IronPDF 適用於 Java 的全面教學和文件。




