
如何在 Java 中写入 PDF 文件
本文将探讨使用 IronPDF 程序化创建 PDF 文档。
IronPDF 适用于 Java PDF Library
IronPDF的Java PDF库允许开发人员在其Java应用程序中创建、编辑和操作PDF文档。 需要从其应用程序的数据中创建PDF文件的Java开发人员会发现这个库是一个很好的选择,因为它提供了丰富的功能集。
IronPDF提供多种功能,包括添加新HTML内容,嵌入HTML页眉和页脚,加盖和水印,创建密码保护的PDF,应用数字签名,用背景和前景增强文档,从XML创建完整PDF,添加和编辑注释,以及用大纲和书签导航。 让我们仔细看看。
添加新的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"));
输出 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);
}
输出 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"));
}
}
输出 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"));
}
}
添加和编辑注释
注释是为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"));
}
}
输出文件
大纲和书签
开发人员可以使用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"));
}
}
摘要
这篇文章探讨了IronPDF的各种功能,如向PDF文档添加注释、书签、HTML内容、背景和前景色、页眉和页脚的能力。 文章提供了使用IronPDF实现这些功能的分步说明,使开发人员能够轻松创建满足其特定需求的专业外观PDF文档。
无论您是在构建Web应用程序还是桌面应用程序,IronPDF都可以帮助您简化生成PDF文档的过程,为您节省时间和精力,同时确保您的文档看起来很棒。
有关IronPDF许可信息从$999开始。 IronPDF还提供免费试用,允许开发人员在购买决策之前测试库并评估其能力。 在试用期内,用户可以访问库的所有功能,包括支持和更新。 在试用期结束后,用户可以选择购买许可证以继续使用该库。 IronPDF的定价因使用该库的开发人员数量和许可证类型而异。

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。
相关文章


