跳至页脚内容
在 JAVA 中使用 IRONPDF

Java PDF (一站式解决方案)

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

IronPDF For Java - PDF 库

With the IronPDF Java Library Overview, developers may create PDFs, edit new documents, extract content from PDFs, and alter PDF documents with ease within their Java applications using the API. 此库是需要从应用数据创建 PDF 文件的 Java 开发人员的绝佳选择,因为它提供了许多功能,例如对 CJK 字体的支持。 IronPDF for Java 还提供将多个 PDF 文件无缝合并到一个 PDF 文件中的功能。

IronPDF supports creating a PDF from templates, adding new HTML content, customizing headers and footers, generating password-protected PDFs, digitally signing PDF files, adding backgrounds and foregrounds, creating outlines and bookmarks, forming complete PDF files from XML documents, and adding and editing annotations.

使用 HTML 创建 PDF 文档

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

import com.ironsoftware.ironpdf.*;

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

public class GeneratePdf {
    public static void main(String[] args) throws IOException {
        // 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;

public class GeneratePdf {
    public static void main(String[] args) throws IOException {
        // 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 (All-in-One Solution),图 1:输出 输出

HTML 页眉和页脚

使用 IronPDF,可以轻松地为文档添加 HTML 页眉和页脚。 在许多 PDF 文档中,页眉和页脚是必不可少的部分。 使用 IronPDF,开发人员可以自定义其 PDF 文档的页眉和页脚,添加文本、PNG 图像和页码。 需要在其出版物中放置商标或版权信息的企业将发现此功能非常有用。

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;

public class HeaderFooterExample {
    public static void main(String[] args) throws IOException {
        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);

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

public class HeaderFooterExample {
    public static void main(String[] args) throws IOException {
        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);

        // 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 com.ironsoftware.ironpdf.*;
import com.ironsoftware.ironpdf.stamp.HorizontalAlignment;
import com.ironsoftware.ironpdf.stamp.VerticalAlignment;
import java.io.IOException;
import java.nio.file.Paths;

public class WatermarkExample {
    public static void main(String[] args) throws IOException {
        // Apply your commercial license key
        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"));

        // Apply a text watermark to the PDF document
        pdf.applyWatermark("<h2 style='color:red'>SAMPLE</h2>", 
                           30, VerticalAlignment.TOP, HorizontalAlignment.CENTER);

        // Save the updated PDF document
        pdf.saveAs(Paths.get("assets/watermark.pdf"));
    }
}
import com.ironsoftware.ironpdf.*;
import com.ironsoftware.ironpdf.stamp.HorizontalAlignment;
import com.ironsoftware.ironpdf.stamp.VerticalAlignment;
import java.io.IOException;
import java.nio.file.Paths;

public class WatermarkExample {
    public static void main(String[] args) throws IOException {
        // Apply your commercial license key
        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"));

        // Apply a text watermark to the PDF document
        pdf.applyWatermark("<h2 style='color:red'>SAMPLE</h2>", 
                           30, VerticalAlignment.TOP, HorizontalAlignment.CENTER);

        // Save the updated PDF document
        pdf.saveAs(Paths.get("assets/watermark.pdf"));
    }
}
JAVA

背景和前景

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

        // Save the updated PDF document
        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 (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);

        // Save the updated PDF document
        pdf.saveAs(Paths.get("assets/BackgroundForegroundPdf.pdf"));
    }
}
JAVA

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

结论

本文中涵盖了在 PDF 文档中添加注释、书签、HTML 内容、背景和前景颜色、页眉和页脚等功能。 开发人员可以通过遵循本文的分步说明,轻松创建符合其个性化需求的专业外观 PDF 文档。

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

常见问题解答

开发人员如何在 Java 中使用 HTML 创建 PDF 文档?

您可以使用 IronPDF 的 API 将 HTML 内容转换为 PDF 文档。这可以直接在您的 PDF 文件中包含使用 CSS 样式的丰富 HTML 内容,如表格、链接和图像。

IronPDF 提供哪些功能用于自定义 PDF 的页眉和页脚?

IronPDF 允许您自定义具有文本、图像和页码的页眉和页脚。此功能有助于添加个性化品牌或法律信息,如商标和版权。

我可以使用 IronPDF 将多个 PDF 文档合并为一个吗?

是的,IronPDF 通过其全面的 API 提供将多个 PDF 文件无缝合并为一个文档的功能。

可以使用 IronPDF 为 PDF 添加数字签名吗?

是的,IronPDF 支持在 PDF 文档中添加数字签名,提高您的文件的安全性和真实性。

IronPDF 如何处理在 PDF 文档中添加水印?

IronPDF 允许您叠加自定义消息或图像作为印章,并将半透明文本或图像作为水印应用于您的 PDF 文档。

IronPDF 是否支持 PDF 文档的密码保护?

是的,您可以使用 IronPDF 生成受密码保护的 PDF,确保您的文档安全且仅供目标用户访问。

使用 IronPDF 对 Java 开发人员有什么优势?

IronPDF 提供直观的 API 进行无缝的 PDF 集成,支持广泛的 PDF 操作,并提供广泛的自定义选项,使其成为 Java 开发人员管理 PDF 文件的有价值工具。

IronPDF 是否提供开发人员试用版本?

是的,IronPDF 提供免费试用,允许开发人员在购买许可证之前探索所有功能并评估库的能力。

开发人员可以使用 IronPDF 向 PDFs 添加背景和前景吗?

是的,IronPDF 允许添加自定义背景和前景,从而可以在 PDF 文档中进行个性化品牌或图形增强。

IronPDF 为 PDF 文档管理提供哪些自定义选项?

IronPDF 提供一系列自定义选项,包括添加注释、书签、纲要、页眉、页脚、水印、背景和数字签名。

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

Darrius Serrant 拥有迈阿密大学的计算机科学学士学位,目前在 Iron Software 担任全栈 WebOps 市场工程师。从小就被编码吸引,他认为计算机既神秘又易于接触,使其成为创意和问题解决的理想媒介。

在 Iron Software,Darrius 喜欢创造新事物,并简化复杂概念以使其更易理解。作为我们常驻的开发者之一,他还自愿教授学生,与下一代分享他的专业知识。

对于 Darrius 来说,他的工作令人满意,因为它被重视并产生真正的影响。