在 JAVA 中使用 IRONPDF 如何在 Java 中写入 PDF 文件 Darrius Serrant 已更新:七月 28, 2025 Download IronPDF Maven 下载 JAR 下载 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article 这篇文章将探讨如何使用IronPDF以编程方式创建PDF文档。 IronPDF for Java PDF Library IronPDF的Java PDF库允许开发人员在其Java应用程序中创建、编辑和操作PDF文档。 需要从其应用程序的数据中创建PDF文件的Java开发人员会发现这个库是一个很好的选择,因为它提供了丰富的功能集。 IronPDF comes with features such as adding new HTML content, embedding HTML headers and footers, stamping and watermarking documents, creating password-protected PDF files, applying digital signatures to PDF files, enhancing documents with backgrounds and foregrounds, creating a full PDF file from XML documents, adding and editing annotations, and using outlines and bookmarks for better navigation. 让我们仔细看看。 添加新的HTML内容 使用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")); JAVA 输出 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); } JAVA 输出 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")); } } JAVA 输出 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")); } } JAVA 添加和编辑注释 注释是为PDF文档添加附加信息的好方法,如笔记、评论或高亮显示。 使用IronPDF,开发人员可以轻松地有效地管理注释,方法是添加和编辑它们。 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")); } } JAVA 输出文件 大纲和书签 开发人员可以使用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")); } } JAVA 摘要 这篇文章探讨了IronPDF的各种功能,如向PDF文档添加注释、书签、HTML内容、背景和前景色、页眉和页脚的能力。 文章提供了使用IronPDF实现这些功能的分步说明,使开发人员能够轻松创建满足其特定需求的专业外观PDF文档。 无论您是在构建Web应用程序还是桌面应用程序,IronPDF都可以帮助您简化生成PDF文档的过程,为您节省时间和精力,同时确保您的文档看起来很棒。 IronPDF的许可信息从$799起。 IronPDF还提供免费试用,允许开发人员在购买决策之前测试库并评估其能力。 在试用期内,用户可以访问库的所有功能,包括支持和更新。 在试用期结束后,用户可以选择购买许可证以继续使用该库。 IronPDF的定价因使用该库的开发人员数量和许可证类型而异。 常见问题解答 如何在 Java 中以编程方式创建 PDF 文档? 你可以使用 IronPDF for Java 以编程方式创建 PDF 文档。该库提供广泛的功能来生成高质量的 PDF,包括支持 HTML 内容、页眉、页脚等。 有哪些方法可以向 PDF 添加 HTML 内容? IronPDF 允许开发人员使用 RenderHtmlAsPdf 方法将 HTML 内容直接添加到 PDF 中。此方法支持各种 HTML 元素和 CSS 样式。 我可以在 PDF 文档中包含数字签名吗? 是的,IronPDF 支持向 PDF 文件添加数字签名,确保文档的真实性和安全性。 如何使用密码保护我的 PDF 文档? IronPDF 提供了创建密码保护 PDF 的功能,使你能够保护文档中的敏感信息。 是否可以向 PDF 添加自定义背景和前景? IronPDF 允许开发人员向 PDF 添加自定义背景和前景,这可以包括品牌元素或装饰性图形。 轮廓和书签如何改善 PDF 中的文档导航? IronPDF 允许添加轮廓和书签,帮助用户快速导航到 PDF 的特定部分,并提供有序的文档结构概览。 有哪些注释 PDF 的选项? 使用 IronPDF,你可以添加各种注释,例如笔记、评论和高亮,增强 PDF 文件的交互性和可用性。 在 Java 应用程序中使用 IronPDF 的许可选项是什么? IronPDF 提供多种许可选项,包括用于评估目的的免费试用。许可证根据开发人员数量和项目的具体需求而有所不同。 Darrius Serrant 立即与工程团队聊天 全栈软件工程师(WebOps) Darrius Serrant 拥有迈阿密大学的计算机科学学士学位,目前在 Iron Software 担任全栈 WebOps 市场工程师。从小就被编码吸引,他认为计算机既神秘又易于接触,使其成为创意和问题解决的理想媒介。在 Iron Software,Darrius 喜欢创造新事物,并简化复杂概念以使其更易理解。作为我们常驻的开发者之一,他还自愿教授学生,与下一代分享他的专业知识。对于 Darrius 来说,他的工作令人满意,因为它被重视并产生真正的影响。 相关文章 已更新六月 22, 2025 如何在 Java 中将 TIFF 转换为 PDF 本完整指南将引导您在 Java 中使用 IronPDF 无缝地将 TIFF 图像转换为 PDF。 阅读更多 已更新七月 28, 2025 如何在 Java 中将 PDF 转换为 PDFA 在本文中,我们将探讨如何在 Java 中使用 IronPDF 将 PDF 文件转换为 PDF/A 格式。 阅读更多 已更新七月 28, 2025 如何在 Java 中创建 PDF 文档 本文将提供一个关于在 Java 中处理 PDF 的全面指南,涵盖关键概念、最佳库和示例。 阅读更多 Java PDF 生成器 (代码示例教程)如何从 Java 应用程序动态...