IronPDF for Java - Create, Edit, and Read PDFs in Java Applications

This article was translated from English: Does it need improvement?
Translated
View the article in English

关于 IronPDF for Java

IronPDF for Java 是由 Iron Software 开发和维护的库,帮助软件工程师在 Java 8+、Kotlin 和 Scala 项目中创建、编辑和提取PDF内容。

IronPDF for Java 建立在 IronPDF for .NET 的成功和受欢迎的基础上。

IronPDF for Java 使用 gRPC 与 IronPdfEngine 通信。

IronPDF 的优势

  • 从以下生成PDF:HTML、URL、JavaScript、CSS 和多种图像格式
  • 添加页眉/页脚、签名、附件,以及密码和安全性
  • 性能优化:完全多线程和异步支持
  • 以及更多! 访问我们的网站,查看所有的代码示例和 我们50+的功能完整列表

使用 IronPDF for Java

将IronPDF定义为Java依赖项

pom.xml依赖项

要将 IronPDF 定义为依赖关系,请在 pom.xml 中添加以下内容:

<dependencies>

    <!-- Adds IronPDF Java. Use the latest version in the version tag. -->
    <dependency>
        <groupId>com.ironsoftware</groupId>
        <artifactId>ironpdf</artifactId>
        <version>20xx.xx.xxxx</version>
    </dependency>

    <!-- Adds the slf4j logger which IronPDF Java uses. -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>2.0.3</version>
    </dependency>

</dependencies>
<dependencies>

    <!-- Adds IronPDF Java. Use the latest version in the version tag. -->
    <dependency>
        <groupId>com.ironsoftware</groupId>
        <artifactId>ironpdf</artifactId>
        <version>20xx.xx.xxxx</version>
    </dependency>

    <!-- Adds the slf4j logger which IronPDF Java uses. -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>2.0.3</version>
    </dependency>

</dependencies>
XML

下载jar文件

可选择手动下载IronPDF JAR文件以独立使用。

首次构建和运行

在第一次运行项目时,IronPdfEngine 二进制文件会自动下载。当您第一次调用任何 IronPdf 功能时,IronPdfEngine 进程将启动,并将在关闭应用程序或进入空闲阶段时停止。

作为 Maven 依赖项安装 IronPDF 引擎

通过将 IronPdfEngine 添加为 Maven 依赖项,二进制文件将在加载依赖项期间下载:

  • 此方法将避免冗长的启动过程,因为 IronPdfEngine 二进制文件已下载。
  • 此外,对于不允许从外部来源下载的部署设置,这将是有益的。

如果您正在开发跨平台应用程序,只需将以下一个或多个代码片段添加到您的 pom.xml 文件中:

适用于 Windows x64.

<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf-engine-windows-x64</artifactId>
    <version>20xx.xx.xxxx</version>
</dependency>
<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf-engine-windows-x64</artifactId>
    <version>20xx.xx.xxxx</version>
</dependency>
XML

适用于 Windows x86.

<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf-engine-windows-x86</artifactId>
    <version>20xx.xx.xxxx</version>
</dependency>
<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf-engine-windows-x86</artifactId>
    <version>20xx.xx.xxxx</version>
</dependency>
XML

适用于 Linux x64.

<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf-engine-linux-x64</artifactId>
    <version>20xx.xx.xxxx</version>
</dependency>
<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf-engine-linux-x64</artifactId>
    <version>20xx.xx.xxxx</version>
</dependency>
XML

适用于 macOS x64(Intel)

<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf-engine-macos-x64</artifactId>
    <version>20xx.xx.xxxx</version>
</dependency>
<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf-engine-macos-x64</artifactId>
    <version>20xx.xx.xxxx</version>
</dependency>
XML

适用于 macOS Arm(苹果芯片)

<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf-engine-macos-arm64</artifactId>
    <version>20xx.xx.xxxx</version>
</dependency>
<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf-engine-macos-arm64</artifactId>
    <version>20xx.xx.xxxx</version>
</dependency>
XML

开始编写 Java 代码

定义依赖关系后,您可以通过在 Java 代码顶部添加 import com.ironsoftware.ironpdf.* 来开始。 这里是一个简单的HTML到PDF示例以供启动:

// Import statement for IronPDF Java
import com.ironsoftware.ironpdf.*;

// 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 statement for IronPDF Java
import com.ironsoftware.ironpdf.*;

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

这是另一个简单的演示 URL 到 PDF 的示例:

// Import statement for IronPDF Java
import com.ironsoftware.ironpdf.*;

// Apply your license key
License.setLicenseKey("YOUR-LICENSE-KEY");

// Set a log path
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));

// Render the URL as a PDF. Stored in myPdf as type PdfDocument
PdfDocument myPdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com/java");

// Save the PdfDocument to a file
myPdf.saveAs(Paths.get("url_saved.pdf"));
// Import statement for IronPDF Java
import com.ironsoftware.ironpdf.*;

// Apply your license key
License.setLicenseKey("YOUR-LICENSE-KEY");

// Set a log path
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));

// Render the URL as a PDF. Stored in myPdf as type PdfDocument
PdfDocument myPdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com/java");

// Save the PdfDocument to a file
myPdf.saveAs(Paths.get("url_saved.pdf"));
JAVA

完整的 Main.java 示例

package org.example;

// Import statement for IronPDF Java
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;

public class Main {

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

    }

}
package org.example;

// Import statement for IronPDF Java
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;

public class Main {

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

更多设置信息

注意:请注意,所有设置、日志记录和许可证操作必须在调用任何 IronPDF 方法之前执行。

应用许可证密钥

要应用您的许可证密钥,请将以下内容添加到方法顶部:

com.ironsoftware.ironpdf.License.setLicenseKey("YOUR-LICENSE-KEY");
com.ironsoftware.ironpdf.License.setLicenseKey("YOUR-LICENSE-KEY");
JAVA

日志记录

IronPDF Java 使用slf4j 日志记录器进行日志记录。 要启用日志记录,请使用:

com.ironsoftware.ironpdf.Settings.setDebug(true);
com.ironsoftware.ironpdf.Settings.setDebug(true);
JAVA

要指定IronPdfEngine日志路径,请添加:

com.ironsoftware.ironpdf.Settings.setLogPath(Paths.get("C:/tmp/myIronPdfEngineLog.log"));
com.ironsoftware.ironpdf.Settings.setLogPath(Paths.get("C:/tmp/myIronPdfEngineLog.log"));
JAVA

许可证和支持可用

购买 IronPDF 许可证以用于实际项目。 30天试用许可证也可供试用用户使用。

有关我们完整的代码示例、教程、许可证信息和文档,请访问:IronPDF for Java 资源

如需更多支持和查询,请联系我们的支持团队

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

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

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

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

准备开始了吗?
版本: 2025.11 刚刚发布