使用IRONPDF FOR JAVA

Java版PDF(全功能解决方案)

市场上有多种 PDF Java 库,如 iText Library 和 Apache PDFBox,但 IronPDF for Java 是功能强大的 Java 库之一,它允许您执行各种类型的 PDF 操作,包括数字签名、从表单中提取文本、插入文本等。 本文将指导您如何使用 IronPDF for Java 通过高效易用的 API 创建 PDF 文档。

IronPDF For Java - PDF库

通过IronPDF Java库概述,开发人员可以在Java应用程序中使用API创建PDF,编辑新文档,从PDF提取内容,以及轻松更改PDF文档。 对于需要从应用程序数据创建 PDF 文件的 Java 开发人员来说,该库是一个绝佳的选择,因为它提供了大量功能,例如支持中日韩字体。 IronPDF for Java 还提供将多个 PDF 文件无缝合并为一个 PDF 文件的功能。

IronPDF 支持从模板创建 PDF,添加新的 HTML 内容,自定义页眉和页脚生成密码保护的 PDF数字签名 PDF 文件,添加背景和前景,创建大纲和书签,从 XML 文档形成完整的 PDF 文件,以及添加和编辑注释。

使用HTML创建PDF文档

IronPDF使开发人员可以轻松将新的HTML信息整合到整个PDF文档中。 希望动态创建包含丰富HTML信息的PDF表单文档的开发人员会发现这是一个非常有用且易于集成的工具。 该库支持多种HTML组件,例如表格、链接和图片。 通过使用CSS样式化HTML文本数据或图像,可以轻松创建具有专业外观的PDF。

import com.ironsoftware.ironpdf.*;

import java.io.IOException;
import java.nio.file.Paths;

// 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;

// 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"));
JAVA

以下是从上述源代码生成的示例文档。

PDF For Java(全合一解决方案),图1:输出

输出

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;

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;
import java.util.ArrayList;
import java.util.List;

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);
}
JAVA

印章和水印

开发人员可以使用IronPDF在他们的PDF文档中添加水印和印章。 使用印章将自定义消息或图像添加到新文档中; 水印是显示在文档背景中的半透明图像或文本。

对于需要添加个性化信息或保护其文档免受未经授权使用的公司来说,这些选项非常出色。

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");
        // 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"));
    }
}
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");
        // 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"));
    }
}
JAVA

背景和前景

使用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"));
JAVA

要了解有关IronPDF for Java PDF库的更多信息,请参考Java的HTML到PDF教程

结论

在 PDF 文档中添加注释、书签、HTML 内容、背景和前景颜色、页眉和页脚的功能只是本文所涉及功能的一小部分。 开发人员只需按照文章中的步骤说明,使用 IronPDF 集成这些功能,就能轻松制作出符合个人需求的专业 PDF 文档。

该许可证的价格为$749。 为了帮助开发者在决定购买前评估该库的功能,IronPDF提供了免费试用。 在试用期内,可以使用库的所有功能,包括支持和升级。 用户可以选择购买许可证,以便在试用期结束后继续访问该库。

Darrius Serrant
全栈软件工程师(WebOps)

达瑞乌斯·塞兰特拥有迈阿密大学计算机科学学士学位,目前在Iron Software担任全栈WebOps营销工程师。从小对编码的热爱使他认为计算机既神秘又易接近,成为创意和解决问题的完美媒介。

在Iron Software,达瑞乌斯乐于创造新事物并简化复杂概念,使其更易于理解。作为我们在职开发者之一,他还自愿教授学生,将他的专业知识传授给下一代。

对达瑞乌斯而言,他的工作之所以令人满足,是因为它具有价值并产生了真正的影响。

< 前一页
如何在Java中创建PDF阅读器
下一步 >
如何在Java中查看PDF文件

通过Maven安装

版本:2025.5.6

<dependency>
  <groupId>com.ironsoftware</groupId>
  <artifactId>ironpdf</artifactId>
  <version>2025.5.6</version>
</dependency>