在生產環境中測試,無水印。
在任何需要的地方都能運行。
獲得 30 天的全功能產品。
在幾分鐘內上手運行。
試用產品期間完全訪問我們的支援工程團隊
本文將探討如何使用 IronPDF 以程式化方式建立 PDF 文件。
IronPDF 的 Java PDF 庫允許開發人員在其 Java 應用程式中創建、編輯和操作 PDF 文件。 需要從應用程式資料創建 PDF 檔案的 Java 開發人員會發現這個庫是一個絕佳的選擇,因為它提供了多樣化的功能。
IronPDF 提供的功能包括添加新的 HTML 內容、嵌入 HTML 頁眉和頁腳、在文件上蓋章和加水印、創建受密碼保護的 PDF 文件、將數位簽名應用於 PDF 文件、使用背景和前景增強文件、從 XML 文檔創建完整的 PDF 文件、添加和編輯註釋,以及使用大綱和書籤來提高導航。 讓我們仔細看看。
使用 IronPDF,開發人員可以輕鬆地將新的 HTML 內容添加到他們的 PDF 文件中。 這是對於希望動態生成包含豐富 HTML 內容的 PDF 表單文件的開發人員來說一個很好的功能。 該庫支援許多 HTML 元素,包括圖片、連結和表格等。 HTML 內容也可以使用 CSS 進行樣式設定,使創建專業外觀的 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
頁眉和頁腳是許多 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;
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);
List<PdfDocument> pdfs = new ArrayList<>();
// 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;
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);
List<PdfDocument> pdfs = new ArrayList<>();
// 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);
}
輸出 PDF
使用 IronPDF,開發人員可以向其 PDF 文件中添加印章和水印。 水印是出現在文件背景中的透明圖像或文字,而印章則是在新文件中添加自訂訊息或圖像。
這些功能對於需要保護文件免於未經授權使用或需要在文件中添加自訂訊息的企業來說非常有用。
package IronPDF.ironpdf_java;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
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");
// Create a new PDF or load an existing one from the filesystem
PdfDocument pdf = PdfDocument.fromFile(Paths.get("C:\\byteToPdf.pdf"));
pdf.applyWatermark("<h2 style='color:red'>SAMPLE</h2>", 30, VerticalAlignment.TOP, HorizontalAlignment.CENTER);
pdf.saveAs(Paths.get("assets/watermark.pdf"));
}
}
package IronPDF.ironpdf_java;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
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");
// Create a new PDF or load an existing one from the filesystem
PdfDocument pdf = PdfDocument.fromFile(Paths.get("C:\\byteToPdf.pdf"));
pdf.applyWatermark("<h2 style='color:red'>SAMPLE</h2>", 30, VerticalAlignment.TOP, HorizontalAlignment.CENTER);
pdf.saveAs(Paths.get("assets/watermark.pdf"));
}
}
輸出 PDF
IronPDF 也允許開發人員在其 PDF 文件中實現自訂背景和前景。 前景用來在文件的頂部添加自訂文字或圖像,而背景則將自訂圖像或顏色添加到背景中。 希望其文件或 PDF 表單具有自訂品牌或圖形的企業主會發現此功能特別有用。
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;
// 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);
pdf.saveAs(Paths.get("assets/BackgroundForegroundPdf.pdf"));
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;
// 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);
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;
// Create a new PDF or load an existing one from the filesystem
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);
// 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;
// Create a new PDF or load an existing one from the filesystem
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);
// 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;
// Load an existing PDF from the file system (or create a new one from HTML)
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);
// Retrieve a reference to the Summary bookmark so that we can add a sublist of bookmarks to it.
List<Bookmark> bookmarkList = bookmarks.getBookmarks();
Bookmark bookmark = bookmarkList.get(2);
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;
// Load an existing PDF from the file system (or create a new one from HTML)
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);
// Retrieve a reference to the Summary bookmark so that we can add a sublist of bookmarks to it.
List<Bookmark> bookmarkList = bookmarks.getBookmarks();
Bookmark bookmark = bookmarkList.get(2);
bookmark.AddChildBookmark("Conclusion", 11);
// Save the PDF to the filesystem
pdf.saveAs(Paths.get("assets/bookmarked.pdf"));
本文探討了IronPDF的各種功能,例如向PDF文件添加註釋、書籤、HTML內容、背景和前景顏色以及頁眉和頁腳的能力。 本文提供了使用 IronPDF 實現這些功能的逐步指南,使開發人員能夠輕鬆創建符合其特定需求的專業外觀 PDF 文件。
無論您是在建立網頁應用程式還是桌面應用程式,IronPDF 都能協助您簡化生成 PDF 文件的過程,節省時間和精力,並確保您的文件看起來很出色。
IronPDF 授權資訊 起始於 $749。 IronPDF 也提供免費試用,讓開發人員在購買決策前測試該庫並評估其功能。 在試用期內,用户可以使用該庫的所有功能,包括支援和更新。 試用期結束後,使用者可以選擇購買許可證以繼續使用該程式庫。 IronPDF 的定價取決於使用該庫的開發人員數量和許可證類型。