IronPDF for Java - 在 Java 应用程序中创建、编辑和阅读 PDF.
关于 IronPDF for Java
IronPDF for Java 是由 Iron Software 开发和维护的库,帮助软件工程师在 Java 8+、Kotlin 和 Scala 项目中创建、编辑和提取PDF内容。
IronPDF for Java 是在IronPDF 适用于 .NET的成功和流行基础上建立起来的。
IronPDF for Java 使用 gRPC 与 IronPdfEngine 通信。
IronPDF 的优势
- 从以下生成PDF:HTML、URL、JavaScript、CSS 和多种图像格式
- 添加页眉/页脚、签名、附件,以及密码和安全性
- 性能优化:完全多线程和异步支持
- 以及更多! 访问我们的网站,查看所有的代码示例和 我们50+的功能完整列表
使用 IronPDF for Java
将IronPDF定义为Java依赖项
pom.xml依赖项
要将IronPDF定义为依赖项,请将以下内容添加到您的 pom.xml:
<dependencies>
<dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>ironpdf</artifactId>
<version>20xx.xx.xxxx</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.3</version>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>ironpdf</artifactId>
<version>20xx.xx.xxxx</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.3</version>
</dependency>
</dependencies>
下载jar文件
可选择手动下载IronPDF JAR文件以独立使用。
首次构建和运行
首次运行项目时,IronPdfEngine 二进制文件将自动下载。IronPdfEngine 进程将在您首次调用任何IronPDF函数时启动,并在您的应用程序关闭或进入空闲状态时停止。
作为 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>
适用于 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>
适用于 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>
适用于 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>
适用于 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>
开始编写 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"));
这是另一个简单的演示 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"));
完整的 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"));
}
}
更多设置信息
注意:请注意,所有设置、日志记录和许可证操作必须在调用任何 IronPDF 方法之前执行。
应用许可证密钥
要应用您的许可证密钥,请将以下内容添加到方法顶部:
com.ironsoftware.ironpdf.License.setLicenseKey("YOUR-LICENSE-KEY");
com.ironsoftware.ironpdf.License.setLicenseKey("YOUR-LICENSE-KEY");
日志记录
IronPDF for Java 使用slf4j 日志记录器进行日志记录。 要启用日志记录,请使用:
com.ironsoftware.ironpdf.Settings.setDebug(true);
com.ironsoftware.ironpdf.Settings.setDebug(true);
要指定 IronPdfEngine 日志路径,请添加:
com.ironsoftware.ironpdf.Settings.setLogPath(Paths.get("C:/tmp/myIronPdfEngineLog.log"));
com.ironsoftware.ironpdf.Settings.setLogPath(Paths.get("C:/tmp/myIronPdfEngineLog.log"));
可用的许可和支持
购买 IronPDF 许可证以用于实际项目。 30天试用许可证也可供试用用户使用。
有关我们完整的代码示例、教程、许可证信息和文档,请访问:IronPDF 适用于 Java 资源。
如需更多支持和查询,请联系我们的支持团队。

