如何在 Java 中寫入 PDF 文件
本文將探討如何使用IronPDF以程式設計方式建立 PDF 文件。
IronPDF for Java PDF 函式庫
IronPDF的 Java PDF 程式庫允許開發人員在其 Java 應用程式中建立、編輯和操作 PDF 文件。 對於需要從應用程式資料建立 PDF 檔案的 Java 開發人員來說,該程式庫是一個絕佳的選擇,因為它提供了多種多樣的功能。
IronPDF具有多種功能,例如添加新的 HTML 內容、嵌入 HTML 標題和頁腳、為文件添加浮水印和戳記、創建受密碼保護的 PDF 文件、對 PDF 文件應用數位簽名、使用背景和前景增強文檔、從 XML 文件創建完整的 PDF 文件、添加和編輯註釋,以及使用大綱和書籤進行更好的導航。 讓我們仔細看看。
新增新的 HTML 內容
透過IronPDF,開發人員可以輕鬆地在 PDF 文件中新增新的 HTML 內容。 對於希望動態產生包含豐富 HTML 內容的 PDF 表單文件的開發人員來說,這是一個很棒的功能。 該程式庫支援多種 HTML 元素,包括圖像、連結和表格等。 也可以使用 CSS 對 HTML 內容進行樣式設置,從而輕鬆建立外觀專業的 PDF 檔案。
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;
// Apply your license key
License.setLicenseKey("YOUR-LICENSE-KEY");
// Set a log path
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));
// Render the HTML as a PDF. Stored in myPdf as type PdfDocument.
PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("<h1>Hello World</h1> Made with IronPDF!");
// Save the PdfDocument to a file
myPdf.saveAs(Paths.get("html_saved.pdf"));
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;
// Apply your license key
License.setLicenseKey("YOUR-LICENSE-KEY");
// Set a log path
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));
// Render the HTML as a PDF. Stored in myPdf as type PdfDocument.
PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("<h1>Hello World</h1> Made with IronPDF!");
// Save the PdfDocument to a file
myPdf.saveAs(Paths.get("html_saved.pdf"));
輸出 PDF
新增 HTML 頁首和頁尾
頁首和頁尾是許多 PDF 文件的重要組成部分, IronPDF可以輕鬆地將 HTML 頁首和頁尾整合到您的文件中。 透過IronPDF,開發人員可以為 PDF 文件添加自訂頁首和頁腳,包括文字、圖像和頁碼。 對於需要在文件中添加品牌或版權資訊的企業來說,此功能尤其有用。
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.headerfooter.HtmlHeaderFooter;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.List;
import java.util.ArrayList;
// Render a PDF from a URL
PdfDocument pdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com");
// Build a footer using HTML
HtmlHeaderFooter footer = new HtmlHeaderFooter();
footer.setMaxHeight(15); // millimeters
footer.setHtmlFragment("<center><i>{page} of {total-pages}</i></center>");
footer.setDrawDividerLine(true);
pdf.addHtmlFooter(footer);
List<PdfDocument> pdfs = new ArrayList<>();
// Build a header using an image asset
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.List;
import java.util.ArrayList;
// Render a PDF from a URL
PdfDocument pdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com");
// Build a footer using HTML
HtmlHeaderFooter footer = new HtmlHeaderFooter();
footer.setMaxHeight(15); // millimeters
footer.setHtmlFragment("<center><i>{page} of {total-pages}</i></center>");
footer.setDrawDividerLine(true);
pdf.addHtmlFooter(footer);
List<PdfDocument> pdfs = new ArrayList<>();
// Build a header using an image asset
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);
}
輸出 PDF
印章和浮水印
使用IronPDF,開發人員可以為 PDF 文件添加圖章和浮水印。 水印是出現在文件背景的透明圖像或文本,而圖章則是向新文件添加自訂訊息或圖像。
這些功能非常適合需要保護文件免遭未經授權使用或向文件添加自訂訊息的企業。
package IronPDF.ironpdf_java;
import java.io.IOException;
import java.nio.file.Paths;
import com.ironsoftware.ironpdf.*;
import com.ironsoftware.ironpdf.stamp.HorizontalAlignment;
import com.ironsoftware.ironpdf.stamp.VerticalAlignment;
public class Test {
public static void main(String[] args) throws IOException {
License.setLicenseKey("Your-License");
// Load an existing PDF from the filesystem
PdfDocument pdf = PdfDocument.fromFile(Paths.get("C:\\byteToPdf.pdf"));
// Apply a watermark to the PDF
pdf.applyWatermark("<h2 style='color:red'>SAMPLE</h2>", 30, VerticalAlignment.TOP, HorizontalAlignment.CENTER);
// Save the watermarked PDF
pdf.saveAs(Paths.get("assets/watermark.pdf"));
}
}
package IronPDF.ironpdf_java;
import java.io.IOException;
import java.nio.file.Paths;
import com.ironsoftware.ironpdf.*;
import com.ironsoftware.ironpdf.stamp.HorizontalAlignment;
import com.ironsoftware.ironpdf.stamp.VerticalAlignment;
public class Test {
public static void main(String[] args) throws IOException {
License.setLicenseKey("Your-License");
// Load an existing PDF from the filesystem
PdfDocument pdf = PdfDocument.fromFile(Paths.get("C:\\byteToPdf.pdf"));
// Apply a watermark to the PDF
pdf.applyWatermark("<h2 style='color:red'>SAMPLE</h2>", 30, VerticalAlignment.TOP, HorizontalAlignment.CENTER);
// Save the watermarked PDF
pdf.saveAs(Paths.get("assets/watermark.pdf"));
}
}
輸出 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
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 document with background and foreground
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
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 document with background and foreground
pdf.saveAs(Paths.get("assets/BackgroundForegroundPdf.pdf"));
}
}
新增和編輯註釋
註釋是為 PDF 文件添加附加資訊(例如筆記、評論或高亮顯示)的好方法。 透過IronPDF,開發人員可以透過在 PDF 文件中新增和編輯註釋,輕鬆有效地管理註釋。
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.annotation.AnnotationIcon;
import com.ironsoftware.ironpdf.annotation.AnnotationManager;
import com.ironsoftware.ironpdf.annotation.AnnotationOptions;
import java.io.IOException;
import java.nio.file.Paths;
public class AnnotationExample {
public static void main(String[] args) throws IOException {
// Load an existing PDF from the file system
PdfDocument pdf = PdfDocument.fromFile(Paths.get("assets/example.pdf"));
// Create an annotation to be placed at a specific location on a page.
AnnotationOptions annotation = new AnnotationOptions(
"This is a major title", // Title of the annotation
"This is the long 'sticky note' comment content...", // Content of the annotation
150, // x-axis coordinate location
250 // y-axis coordinate location
);
annotation.setIcon(AnnotationIcon.HELP);
annotation.setOpacity(0.9);
annotation.setPrintable(false);
annotation.setHidden(false);
annotation.setOpen(true);
annotation.setReadonly(true);
annotation.setRotateable(true);
// Add the annotation to a specific page of the PDF
AnnotationManager annotationManager = pdf.getAnnotation();
annotationManager.addTextAnnotation(annotation, 0); // Add to the first page
// Save the PDF with the modifications
pdf.saveAs(Paths.get("assets/annotated.pdf"));
}
}
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.annotation.AnnotationIcon;
import com.ironsoftware.ironpdf.annotation.AnnotationManager;
import com.ironsoftware.ironpdf.annotation.AnnotationOptions;
import java.io.IOException;
import java.nio.file.Paths;
public class AnnotationExample {
public static void main(String[] args) throws IOException {
// Load an existing PDF from the file system
PdfDocument pdf = PdfDocument.fromFile(Paths.get("assets/example.pdf"));
// Create an annotation to be placed at a specific location on a page.
AnnotationOptions annotation = new AnnotationOptions(
"This is a major title", // Title of the annotation
"This is the long 'sticky note' comment content...", // Content of the annotation
150, // x-axis coordinate location
250 // y-axis coordinate location
);
annotation.setIcon(AnnotationIcon.HELP);
annotation.setOpacity(0.9);
annotation.setPrintable(false);
annotation.setHidden(false);
annotation.setOpen(true);
annotation.setReadonly(true);
annotation.setRotateable(true);
// Add the annotation to a specific page of the PDF
AnnotationManager annotationManager = pdf.getAnnotation();
annotationManager.addTextAnnotation(annotation, 0); // Add to the first page
// Save the PDF with the modifications
pdf.saveAs(Paths.get("assets/annotated.pdf"));
}
}
輸出檔案
大綱和書籤
開發者可以使用IronPDF 的書籤功能來增強 PDF 文件。 大綱提供文件內容的高級概覽,而書籤則提供對特定部分的快速存取。 對於篇幅較長或內容複雜的文檔,此功能可讓使用者快速導覽至所需部分。
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.bookmark.Bookmark;
import com.ironsoftware.ironpdf.bookmark.BookmarkManager;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.List;
public class BookmarkExample {
public static void main(String[] args) throws IOException {
// Load an existing PDF from the file system
PdfDocument pdf = PdfDocument.fromFile(Paths.get("assets/book.pdf"));
// Add top-level bookmarks to pages of the PDF using their page indices
BookmarkManager bookmarks = pdf.getBookmark();
bookmarks.addBookMarkAtEnd("Author's Note", 2);
bookmarks.addBookMarkAtEnd("Table of Contents", 3);
bookmarks.addBookMarkAtEnd("Summary", 10);
bookmarks.addBookMarkAtEnd("References", 12);
List<Bookmark> bookmarkList = bookmarks.getBookmarks();
Bookmark bookmark = bookmarkList.get(2);
// Add a child bookmark
bookmark.addChildBookmark("Conclusion", 11);
// Save the PDF to the filesystem
pdf.saveAs(Paths.get("assets/bookmarked.pdf"));
}
}
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.bookmark.Bookmark;
import com.ironsoftware.ironpdf.bookmark.BookmarkManager;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.List;
public class BookmarkExample {
public static void main(String[] args) throws IOException {
// Load an existing PDF from the file system
PdfDocument pdf = PdfDocument.fromFile(Paths.get("assets/book.pdf"));
// Add top-level bookmarks to pages of the PDF using their page indices
BookmarkManager bookmarks = pdf.getBookmark();
bookmarks.addBookMarkAtEnd("Author's Note", 2);
bookmarks.addBookMarkAtEnd("Table of Contents", 3);
bookmarks.addBookMarkAtEnd("Summary", 10);
bookmarks.addBookMarkAtEnd("References", 12);
List<Bookmark> bookmarkList = bookmarks.getBookmarks();
Bookmark bookmark = bookmarkList.get(2);
// Add a child bookmark
bookmark.addChildBookmark("Conclusion", 11);
// Save the PDF to the filesystem
pdf.saveAs(Paths.get("assets/bookmarked.pdf"));
}
}
概括
本文探討了IronPDF的各種功能,例如在 PDF 文件中添加註釋、書籤、HTML 內容、背景色和前景色以及頁眉和頁腳的功能。 本文提供了使用IronPDF實現這些功能的詳細步驟說明,使開發人員能夠輕鬆建立滿足其特定需求的專業外觀 PDF 文件。
無論您是建立 Web 應用程式還是桌面應用程序, IronPDF都能幫助您簡化產生 PDF 文件的過程,節省您的時間和精力,同時確保您的文件看起來很棒。
IronPDF許可資訊從 $999 開始。 IronPDF還提供免費試用版,讓開發人員在做出購買決定之前測試該程式庫並評估其功能。 在試用期內,使用者可以存取圖書館的所有功能,包括支援和更新。 試用期結束後,使用者可以選擇購買許可證繼續使用該庫。 IronPDF的定價取決於使用該庫的開發人員數量和許可證類型。
常見問題解答
如何使用Java程式建立PDF文件?
您可以使用IronPDF 適用於 Java 以程式設計方式建立 PDF 文件。該庫提供豐富的功能來產生高品質的 PDF,包括支援 HTML 內容、頁首、頁尾等等。
有哪些方法可以將HTML內容加入PDF中?
IronPDF允許開發者使用RenderHtmlAsPdf方法將 HTML 內容直接加入 PDF 中。此方法支援各種 HTML 元素和 CSS 樣式。
我可以在PDF文件中新增數位簽名嗎?
是的, IronPDF支援為 PDF 文件添加數位簽名,以確保文件的真實性和安全性。
如何用密碼保護我的PDF文件?
IronPDF提供創建密碼保護 PDF 的功能,讓您可以保護文件中的敏感資訊。
是否可以為 PDF 文件添加自訂背景和前景?
IronPDF允許開發人員為 PDF 添加自訂背景和前景,其中可以包含品牌元素或裝飾性圖形。
如何利用大綱和書籤改善 PDF 文件的導航體驗?
IronPDF允許添加大綱和書籤,幫助使用者快速導航到 PDF 的特定部分,並提供文件結構的有序概覽。
有哪些可用於註釋 PDF 文件的選項?
使用IronPDF,您可以添加各種註釋,例如筆記、評論和高亮顯示,從而增強 PDF 文件的互動性和可用性。
在Java應用程式中使用IronPDF有哪些授權選項?
IronPDF提供多種許可選項,包括用於評估的免費試用版。許可價格根據開發人員數量和專案的具體需求而有所不同。




