PDF 建立者 適用於 Java(分步教程)
本文將演示如何使用用Java程式語言編寫的PDF建立工具來建立PDF文件。
使用Java建立PDF文件在IT行業有許多應用。 有時,開發人員可能需要將PDF建立器整合到其專案中。 本文中討論的PDF建立器是IronPDF for Java。 它幫助開發人員從頭建立新的PDF文件,並將不同的文件格式轉換為PDF文件。
1. IronPDF for Java
IronPDF for Java 是一個用於建立、準備和管理PDF文件的Java程式庫。 它允許開發人員讀取、生成和修改PDF文件,而無需安裝任何Adobe軟體。
IronPDF也非常擅長從PDF文件建立PDF表單。IronPDF for Java可以建立自訂頁眉和頁尾,建立簽名,新增附件,並新增密碼加密和安全機制。 IronPDF還支持多執行緒和異步功能以改善性能。
如何在Java中使用PDF建立工具
- 安裝用於建立PDF的Java程式庫
- 使用renderHtmlAsPdf方法從HTML字串建立PDF
- 將HTML文件路徑傳遞給renderHtmlFileAsPdf方法以建立PDF
- 使用renderUrlAsPdf方法在Java中從URL連結建立PDF
- 利用fromImage方法從圖像建立PDF
2. 先決條件
在開始之前,必須滿足一些前提條件才能執行PDF建立。
- 系統上應安裝Java,並且其路徑應設置在環境變數中。 如果您尚未安裝Java,請參考此安裝指導。
- 應安裝一個良好的Java IDE,比如Eclipse或IntelliJ。 要下載Eclipse,請存取此下載頁面,或者點擊此安裝頁面安裝IntelliJ。
- 在開始PDF轉換之前,應將Maven與IDE整合。 要獲得有關安裝Maven並將其整合到環境中的教程,請存取以下JetBrains整合連結。
3. IronPDF for Java 安裝
一旦滿足了所有的前提條件,即使是新手Java開發人員,安裝IronPDF for 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 for Java的Maven依賴項。 這將只需幾分鐘,具體取決於您的網路連接。
4. 建立PDF文件
本節將討論如何使用IronPDF for Java建立新的PDF文件。 IronPDF使得只需少量程式碼就可以非常輕鬆地建立新的PDF文件。
使用IronPDF for Java建立PDF文件有多種不同的方法
- [
renderHtmlAsPdf](/java/object-reference/api/com/ironsoftware/ironpdf/PdfDocument.html#renderHtmlAsPdf(java.lang.String)方法可以將HTML字串轉換為PDF文件 - [
renderHtmlFileAsPdf](/java/object-reference/api/com/ironsoftware/ironpdf/PdfDocument.html#renderHtmlFileAsPdf(java.lang.String)方法可以將HTML文件轉換為PDF文件 - [
renderUrlAsPdf](/java/object-reference/api/com/ironsoftware/ironpdf/PdfDocument.html#renderUrlAsPdf(java.lang.String)方法可以直接從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. 從URLs建立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. 結論
本文演示了如何從頭開始或從已有的HTML文件或URLs建立PDF。 IronPDF允許您從多種格式轉換以生成高質量的PDF,並使操作和格式化這些PDF文件變得非常簡單。
IronPDF非常適合需要處理、修改或操作PDF文件的開發人員和企業。 要了解更多有關IronPDF for Java的資訊以及如何使用Java操作PDF的類似教程,請參考以下官方文件。 要獲得有關使用Java建立PDF文件的教程,請存取此Java程式碼範例。
IronPDF for Java供開發用途免費,但商業用途需要許可證。 要了解更多有關許可的附加資訊,請存取以下許可頁面。
常見問題
我如何使用Java建立PDF文件?
您可以使用IronPDF for 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 for 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 for Java已正確安裝,並確認您的Java環境配置正確。檢查您的程式碼中IronPDF方法的任何錯誤,並參考官方文件進行故障排除。
我可以在Java中將網頁URL轉換為PDF嗎?
是的,IronPDF for Java允許您使用renderUrlAsPdf方法將網頁URL轉換為PDF文件。
我可以在哪裡找到學習使用IronPDF for Java的資源?
您可以在官方IronPDF文件頁面上找到關於使用IronPDF for Java的完整教程和文件。





