Java PDF 生成器 (代码示例教程)
本文将探讨如何使用IronPDF生成新文件,提取内容和保存PDF。
如何用 Java 生成 PDF 文件
- 安装 IronPDF Java Library for PDF Generation
- 使用
renderHtmlAsPdf方法从 HTML 字符串渲染 PDF - 使用
renderHtmlFileAsPdf方法从 HTML 文件渲染 PDF - 使用
renderUrlAsPdf方法从 URL 渲染 PDF - 在Java中对新生成的PDF应用密码保护
IronPDF for Java
IronPDF for Java专为从HTML代码生成PDF文档或PDF表单而构建,无论是从文件、HTML字符串、HTML页面还是URL。 它生成的PDF文件具有高精确度,并且格式也得到了保留。 它的设计方式使开发人员使用起来非常简单。
IronPDF构建于.NET Framework之上,使之成为在各种上下文中生成PDF的多功能工具。
IronPDF提供以下功能用于生成和处理大型文档:
在Java应用程序中创建PDF文件的步骤
前提条件
要使用 IronPDF 创建 PDF 生成工具,需要在计算机上安装以下软件:
- Java开发套件 - JDK是构建和运行Java程序所必需的。 如果尚未安装,请从Oracle网站下载最新版本。
- 集成开发环境 - IDE 是帮助编写、编辑和调试程序的软件。 下载任何用于Java的IDE,例如Eclipse、NetBeans、IntelliJ。
- Maven - Maven 是一款自动化和开源Java工具,帮助从中央Maven库下载库。 从Apache Maven网站下载。
IronPDF - 最后,在Java中创建PDF文件需要IronPDF。 需要将该项添加为Java Maven项目中的依赖项。 在
pom.xml文件中添加IronPDF artifact和slf4j依赖项,如下所示:<dependency> <groupId>com.ironsoftware</groupId> <artifactId>ironpdf</artifactId> <version>YOUR_VERSION_HERE</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>YOUR_VERSION_HERE</version> </dependency><dependency> <groupId>com.ironsoftware</groupId> <artifactId>ironpdf</artifactId> <version>YOUR_VERSION_HERE</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>YOUR_VERSION_HERE</version> </dependency>XML
添加必要的导入
首先,在Java主类源代码文件的顶部添加以下行,以从IronPDF库导入所有所需的重要类方法。
import com.ironsoftware.ironpdf.*;import com.ironsoftware.ironpdf.*;接下来,配置IronPDF并使用有效的许可证密钥以使用其方法。 在主方法中调用setLicenseKey方法。
License.setLicenseKey("Your license key");License.setLicenseKey("Your license key");注意: 您可以从IronPDF获取免费试用许可证密钥以创建和阅读PDF文件。
从HTML字符串生成PDF文档
从HTML字符串创建PDF文件非常简单,通常只需要一两行代码即可完成。 在此,将HTML代码以字符串形式写入变量中,然后传递给[renderHtmlAsPdf](/java/object-reference/api/com/Iron Software/ironpdf/PdfDocument.html#renderHtmlAsPdf(java.lang.String)方法,该方法位于[PdfDocument](/java/object-reference/api/com/Iron Software/ironpdf/PdfDocument.html)类中。 以下代码生成一个新的PDF文档实例:
// Create a string that contains HTML content
String htmlString = "<h1>Hello World!</h1><p>This is an example of an HTML string in Java.</p>";
// Generate a PDF document from the HTML string
PdfDocument pdf = PdfDocument.renderHtmlAsPdf(htmlString);// Create a string that contains HTML content
String htmlString = "<h1>Hello World!</h1><p>This is an example of an HTML string in Java.</p>";
// Generate a PDF document from the HTML string
PdfDocument pdf = PdfDocument.renderHtmlAsPdf(htmlString);现在,使用[saveAs](/java/object-reference/api/com/Iron Software/ironpdf/PdfDocument.html#saveAs(java.lang.String)方法将生成的PDF保存到本地系统的一路径中:
// Save the generated PDF to the specified path
pdf.saveAs("htmlstring.pdf");// Save the generated PDF to the specified path
pdf.saveAs("htmlstring.pdf");上述代码行创建了一个名为"htmlstring.pdf"的PDF,其中包含HTML字符串的内容。
译文如下:
HTML字符串到PDF输出
从HTML文件创建PDF文档
以下代码从HTML文件创建一个PDF文件:
// Convert an HTML file to a PDF document
PdfDocument myPdf = PdfDocument.renderHtmlFileAsPdf("example.html");
// Save the PDF document to the specified path
myPdf.saveAs("html_file.pdf");// Convert an HTML file to a PDF document
PdfDocument myPdf = PdfDocument.renderHtmlFileAsPdf("example.html");
// Save the PDF document to the specified path
myPdf.saveAs("html_file.pdf");HTML文件代码:
<html>
<head>
<title>Example HTML File</title>
</head>
<body>
<h1>HTML File Example</h1>
<p style="font-style:italic;">This is an example HTML file</p>
</body>
</html><html>
<head>
<title>Example HTML File</title>
</head>
<body>
<h1>HTML File Example</h1>
<p style="font-style:italic;">This is an example HTML file</p>
</body>
</html>在上述代码中,[renderHtmlFileAsPdf](/java/object-reference/api/com/Iron Software/ironpdf/PdfDocument.html#renderHtmlFileAsPdf(java.lang.String)方法从HTML文件生成PDF文件。 该方法接受包含HTML文件路径的字符串参数。
IronPDF可渲染HTML文件元素,以及所附的CSS和JavaScript(如有)。 您可以在下面的输出中看到,IronPDF也保持了CSS样式,输出与在网页浏览器中一样。
HTML文件至PDF输出
从URL生成PDF文件
[renderUrlAsPdf](/java/object-reference/api/com/Iron Software/ironpdf/PdfDocument.html#renderUrlAsPdf(java.lang.String)方法用于从网页创建PDF文件。 它接受网页的 URL 作为参数。
// Generate a PDF document using a URL
PdfDocument urlToPdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com");
// Save the generated PDF to the specified path
urlToPdf.saveAs("urlToPdf.pdf");// Generate a PDF document using a URL
PdfDocument urlToPdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com");
// Save the generated PDF to the specified path
urlToPdf.saveAs("urlToPdf.pdf");
URL到PDF输出
可以设置附加渲染选项以配置PDF生成。 您可以在将URL转换为PDF示例代码中获取更多信息。
生成密码保护的PDF文件
IronPDF可以使用SecurityOptions类创建密码保护的PDF文件。 如果集成了IronPDF的PDF功能,所有文件权限都可以设置。 代码如下
// Create security options and set a user password
SecurityOptions securityOptions = new SecurityOptions();
securityOptions.setUserPassword("shareable");// Create security options and set a user password
SecurityOptions securityOptions = new SecurityOptions();
securityOptions.setUserPassword("shareable");setUserPassword用于设置安全密码。 下面的代码示例对在URL到PDF示例中创建的PDF文档应用密码保护:
// Get the security manager of the PDF document and set the security options
SecurityManager securityManager = urlToPdf.getSecurity();
securityManager.setSecurityOptions(securityOptions);
// Save the protected PDF document to the specified path
urlToPdf.saveAs("protected.pdf");// Get the security manager of the PDF document and set the security options
SecurityManager securityManager = urlToPdf.getSecurity();
securityManager.setSecurityOptions(securityOptions);
// Save the protected PDF document to the specified path
urlToPdf.saveAs("protected.pdf");PDF文件现在已密码保护。 现在打开PDF文件,会出现密码选项:
已密码保护文件
正确输入密码后,PDF文档将打开。
PDF文档
更多关于PDF文件的安全设置和元数据可在相关安全和元数据代码示例中探索。
摘要
本文展示了使用多个方法创建PDF的IronPDF库的能力。 IronPDF是纯Java库,功能强大,能够在Java中轻松处理PDF文件。
IronPDF的引擎使得从HTML文件、图像文件、XML文档、Jasper报表或任何其他输入中轻松创建PDF。 它符合标准Java打印API,这促进了文档打印,同时您也可以对PDF文件进行数字签名。 IronPDF帮助快速轻松完成所有与PDF相关的任务。
IronPdf 并非开源 Java 库。 它提供了一个商业许可证,其起价为$799。 您还可以获得IronPDF的免费试用版,在您的Java应用程序中进行生产测试。
常见问题解答
如何在 Java 中从 HTML 字符串生成 PDF?
要使用IronPDF从HTML字符串生成PDF,需在PdfDocument类中使用renderHtmlAsPdf方法。PDF创建后,使用saveAs方法保存文档。
我可以从HTML文件创建PDF吗?
是的,使用IronPDF,您可以通过PdfDocument类中的renderHtmlFileAsPdf方法,通过提供文件路径从HTML文件生成PDF。
我如何从URL生成PDF?
IronPDF通过使用renderUrlAsPdf方法便利地从网页URL创建PDF。只需将网页URL作为参数传递给此方法即可。
是否可以对PDF文件进行密码保护?
是的,IronPDF允许您对PDF文件进行密码保护。使用SecurityOptions类中的setUserPassword方法设置用户密码。
在Java应用程序中使用IronPDF的前提条件是什么?
要在Java中使用IronPDF创建PDF文件,确保安装了Java开发工具包 (JDK)、集成开发环境 (IDE)、Maven,以及将IronPDF本身配置为Maven依赖项。
IronPDF是否支持PDF中的数字签名?
是的,IronPDF支持在PDF文件中添加数字签名,增强文档安全性并确保真实性。
IronPDF是开源的Java库吗?
不,IronPDF是商业Java库。不过,有免费的试用版可以让您在购买前测试其完整功能。
我如何使用许可证密钥配置IronPDF?
要使用许可证密钥配置IronPDF,请在您的Java应用程序中调用setLicenseKey方法。您可以通过免费试用或购买获得许可证密钥。
在Java中生成PDF时如何保留HTML格式?
IronPDF在将HTML转换为PDF时保留HTML格式。它支持CSS和JavaScript样式,确保渲染的PDF与原始HTML设计相匹配。
使用IronPDF保存生成的PDF有哪些方法?
当您使用IronPDF生成PDF后,可以使用saveAs方法来保存,指定PDF所需的文件路径和名称。










