PDF 適用於 Java(全能解決方案)
市面上有許多 PDF Java 庫,例如 iText Library 和 Apache PDFBox,但IronPDF是一個功能強大的 Java 庫,它允許您執行各種類型的 PDF 操作,包括數位簽章、從表單中提取文字、插入文字等等。 本文將指導您如何使用IronPDF 適用於 Java 透過高效易用的 API 建立 PDF 文件。
IronPDF for Java - PDF 函式庫
借助IronPDF Java 庫概述,開發人員可以使用 API 輕鬆地在 Java 應用程式中建立 PDF、編輯新文件、從 PDF 提取內容以及修改 PDF 文件。 對於需要從應用程式資料建立 PDF 檔案的 Java 開發人員來說,該程式庫是一個絕佳的選擇,因為它提供了許多功能,例如支援 CJK 字體。 IronPDF for Java 也提供了將多個 PDF 檔案無縫合併成一個 PDF 檔案的功能。
IronPDF支援從範本建立 PDF 、新增新的 HTML 內容、自訂頁首和頁尾、產生受密碼保護的 PDF 、對 PDF 檔案進行數位簽章、新增背景和前景、建立大綱和書籤、從 XML 文件形成完整的 PDF 檔案以及新增和編輯註解。
使用 HTML 建立 PDF 文檔
IronPDF使開發人員能夠輕鬆地將新的 HTML 資訊整合到整個 PDF 文件中。 對於希望動態建立包含豐富 HTML 資訊的 PDF 表單文件的開發人員來說,這是一個非常有用的工具,而且易於整合。 該庫支援多種 HTML 元件,例如表格、連結和圖像。 使用 CSS 為 HTML 文字資料或圖像設定樣式,可以輕鬆建立外觀Professional的PDF 檔案。
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;
public class GeneratePdf {
public static void main(String[] args) throws IOException {
// Apply your commercial license key
License.setLicenseKey("YOUR-LICENSE-KEY");
// Set a log file path
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));
// Render the HTML as a PDF. Store in myPdf as type PdfDocument;
PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("<h1>Hello World</h1>");
// Save the PdfDocument to a file
myPdf.saveAs(Paths.get("Demo.pdf"));
}
}
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;
public class GeneratePdf {
public static void main(String[] args) throws IOException {
// Apply your commercial license key
License.setLicenseKey("YOUR-LICENSE-KEY");
// Set a log file path
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));
// Render the HTML as a PDF. Store in myPdf as type PdfDocument;
PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("<h1>Hello World</h1>");
// Save the PdfDocument to a file
myPdf.saveAs(Paths.get("Demo.pdf"));
}
}
以下是根據上述原始程式碼產生的範例文件。
輸出
HTML頁首和頁尾
使用IronPDF可以輕鬆地為文件添加 HTML 頁首和頁尾。 在許多 PDF 文件中,頁首和頁尾是必不可少的部分。 透過IronPDF,開發人員可以使用文字、PNG 圖像和頁碼自訂 PDF 文件的頁首和頁尾。 需要在出版物中添加商標或版權資訊的企業會發現此功能非常有用。
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.headerfooter.HtmlHeaderFooter;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class HeaderFooterExample {
public static void main(String[] args) throws IOException {
PdfDocument pdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com");
// Build a footer using HTML
// Merge Fields are: {page} {total-pages} {url} {date} {time} {html-title} & {pdf-title}
HtmlHeaderFooter footer = new HtmlHeaderFooter();
footer.setMaxHeight(15); // millimeters
footer.setHtmlFragment("<center><i>{page} of {total-pages}</i></center>");
footer.setDrawDividerLine(true);
pdf.addHtmlFooter(footer);
// Build a header using an image asset
// Note the use of BaseUrl to set a relative path to the assets
HtmlHeaderFooter header = new HtmlHeaderFooter();
header.setMaxHeight(20); // millimeters
header.setHtmlFragment("<img src=\"logo.png\" />");
header.setBaseUrl("./assets/");
pdf.addHtmlHeader(header);
try {
pdf.saveAs(Paths.get("assets/html_headers_footers.pdf"));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.headerfooter.HtmlHeaderFooter;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class HeaderFooterExample {
public static void main(String[] args) throws IOException {
PdfDocument pdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com");
// Build a footer using HTML
// Merge Fields are: {page} {total-pages} {url} {date} {time} {html-title} & {pdf-title}
HtmlHeaderFooter footer = new HtmlHeaderFooter();
footer.setMaxHeight(15); // millimeters
footer.setHtmlFragment("<center><i>{page} of {total-pages}</i></center>");
footer.setDrawDividerLine(true);
pdf.addHtmlFooter(footer);
// Build a header using an image asset
// Note the use of BaseUrl to set a relative path to the assets
HtmlHeaderFooter header = new HtmlHeaderFooter();
header.setMaxHeight(20); // millimeters
header.setHtmlFragment("<img src=\"logo.png\" />");
header.setBaseUrl("./assets/");
pdf.addHtmlHeader(header);
try {
pdf.saveAs(Paths.get("assets/html_headers_footers.pdf"));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
印章和浮水印
開發者可以使用IronPDF為 PDF 文件添加浮水印和圖章。 使用圖章為新文件新增自訂訊息或影像; 水印是顯示在文件背景中的半透明圖像或文字。
對於需要添加個人化資訊或保護文件免遭未經授權使用的公司來說,這些選項非常棒。
import com.ironsoftware.ironpdf.*;
import com.ironsoftware.ironpdf.stamp.HorizontalAlignment;
import com.ironsoftware.ironpdf.stamp.VerticalAlignment;
import java.io.IOException;
import java.nio.file.Paths;
public class WatermarkExample {
public static void main(String[] args) throws IOException {
// Apply your commercial license key
License.setLicenseKey("Your-License");
// Create a new PDF or load an existing one from the filesystem
PdfDocument pdf = PdfDocument.fromFile(Paths.get("C:\\byteToPdf.pdf"));
// Apply a text watermark to the PDF document
pdf.applyWatermark("<h2 style='color:red'>SAMPLE</h2>",
30, VerticalAlignment.TOP, HorizontalAlignment.CENTER);
// Save the updated PDF document
pdf.saveAs(Paths.get("assets/watermark.pdf"));
}
}
import com.ironsoftware.ironpdf.*;
import com.ironsoftware.ironpdf.stamp.HorizontalAlignment;
import com.ironsoftware.ironpdf.stamp.VerticalAlignment;
import java.io.IOException;
import java.nio.file.Paths;
public class WatermarkExample {
public static void main(String[] args) throws IOException {
// Apply your commercial license key
License.setLicenseKey("Your-License");
// Create a new PDF or load an existing one from the filesystem
PdfDocument pdf = PdfDocument.fromFile(Paths.get("C:\\byteToPdf.pdf"));
// Apply a text watermark to the PDF document
pdf.applyWatermark("<h2 style='color:red'>SAMPLE</h2>",
30, VerticalAlignment.TOP, HorizontalAlignment.CENTER);
// Save the updated PDF document
pdf.saveAs(Paths.get("assets/watermark.pdf"));
}
}
背景與前景
使用IronPDF,開發人員還可以自訂 PDF 文件的前景色和背景。 可以將自訂文字或圖像新增至文件的前景色或背景色,也可以將自訂顏色或圖像新增至背景色。 如果企業主希望在文件或 PDF 表格中添加個人化品牌或圖形,他們會發現此選項特別有用。
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;
public class BackgroundForegroundExample {
public static void main(String[] args) throws IOException {
// Load background and foreground PDFs from the filesystem (or create them programmatically)
PdfDocument backgroundPdf = PdfDocument.fromFile(Paths.get("assets/MyBackground.pdf"));
PdfDocument foregroundPdf = PdfDocument.fromFile(Paths.get("assets/MyForeground.pdf"));
// Render content (HTML, URL, etc.) as a PDF Document
PdfDocument pdf = PdfDocument.renderUrlAsPdf("https://www.nuget.org/packages/IronPdf");
// Add the background and foreground PDFs to the newly-rendered document
pdf.addBackgroundPdf(backgroundPdf);
pdf.addForegroundPdf(foregroundPdf);
// Save the updated PDF document
pdf.saveAs(Paths.get("assets/BackgroundForegroundPdf.pdf"));
}
}
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;
public class BackgroundForegroundExample {
public static void main(String[] args) throws IOException {
// Load background and foreground PDFs from the filesystem (or create them programmatically)
PdfDocument backgroundPdf = PdfDocument.fromFile(Paths.get("assets/MyBackground.pdf"));
PdfDocument foregroundPdf = PdfDocument.fromFile(Paths.get("assets/MyForeground.pdf"));
// Render content (HTML, URL, etc.) as a PDF Document
PdfDocument pdf = PdfDocument.renderUrlAsPdf("https://www.nuget.org/packages/IronPdf");
// Add the background and foreground PDFs to the newly-rendered document
pdf.addBackgroundPdf(backgroundPdf);
pdf.addForegroundPdf(foregroundPdf);
// Save the updated PDF document
pdf.saveAs(Paths.get("assets/BackgroundForegroundPdf.pdf"));
}
}
要了解有關IronPDF 適用於 Java PDF 庫的更多信息,請參閱Java 的 HTML 轉 PDF 教程。
結論
本文將介紹 PDF 文件的眾多功能,例如新增註解、書籤、HTML 內容、背景色彩和前景色、頁首和頁尾等等。 開發人員可以按照本文中的逐步說明,使用IronPDF整合這些功能,輕鬆製作符合其個人化需求的專業外觀 PDF 文件。
該許可證的價格為 $999。為了幫助開發者在購買前評估該庫的功能, IronPDF提供免費試用。 試用期內,圖書館的所有功能,包括技術支援和升級服務,均可使用。 使用者可以選擇購買許可證,以便在試用期結束後繼續造訪該圖書館。
常見問題解答
開發者如何在Java中使用HTML建立PDF文件?
您可以使用 IronPDF 的 API 將 HTML 內容轉換為 PDF 文件。這樣,您就可以將表格、連結和圖像等豐富的 HTML 內容(使用 CSS 設定樣式)直接新增到 PDF 檔案中。
IronPDF提供了哪些自訂 PDF 頁首和頁尾的功能?
IronPDF可讓您自訂頁首和頁腳,新增文字、圖像和頁碼。此功能可用於添加個人化品牌識別或法律訊息,例如商標和版權資訊。
我可以使用IronPDF將多個 PDF 文件合併成一個嗎?
是的, IronPDF透過其全面的 API 提供了將多個 PDF 文件無縫合併到單一文件中的功能。
是否可以使用IronPDF為 PDF 檔案添加數位簽章?
是的, IronPDF支援為 PDF 文件添加數位簽名,從而增強文件的安全性和真實性。
IronPDF如何處理在 PDF 文件中新增浮水印的問題?
IronPDF可讓您將自訂訊息或圖像疊加為圖章到 PDF 文件上,並將半透明文字或圖像作為浮水印套用到 PDF 文件上。
IronPDF是否支援PDF文件的密碼保護?
是的,您可以使用IronPDF產生受密碼保護的 PDF 文件,確保您的文件安全可靠,只有授權使用者才能存取。
使用IronPDF對 Java 開發人員有哪些優勢?
IronPDF提供直覺的 API,可實現無縫的 PDF 集成,支援各種 PDF 操作,並提供豐富的自訂選項,使其成為 Java 開發人員管理 PDF 文件的重要工具。
IronPDF是否有開發者的試用版?
是的, IronPDF提供免費試用版,讓開發人員在購買許可證之前探索所有功能並評估該庫的功能。
開發者可以使用IronPDF為 PDF 添加背景和前景嗎?
是的, IronPDF支援添加自訂背景和前景,從而可以在 PDF 文件中進行個人化品牌推廣或圖形增強。
IronPDF為 PDF 文件管理提供了哪些自訂選項?
IronPDF提供一系列自訂選項,包括新增註解、書籤、大綱、頁首、頁尾、浮水印、背景和數位簽名。




