如何在 Java 中创建 PDF 文件
使用 IronPDF 库在 Java 中创建 PDF 文件非常简单,该库使用 renderHtmlAsPdf() 等方法将 HTML 字符串转换为 PDF,使用 renderHtmlFileAsPdf() 等方法将 HTML 文件转换为 PDF,使用 renderUrlAsPdf() 等方法将网页转换为 PDF。
as-heading:2(快速入门:用 Java 创建您的第一个 PDF)
1.在 pom.xml 中添加 IronPDF 依赖关系:
<dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>ironpdf</artifactId>
<version>1.0.0</version>
</dependency><dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>ironpdf</artifactId>
<version>1.0.0</version>
</dependency>2.导入 IronPdf 类:
import com.ironsoftware.ironpdf.*;import com.ironsoftware.ironpdf.*;3.从 HTML 创建 PDF: ```java :title=Quickstart PdfDocument pdf = PdfDocument.renderHtmlAsPdf(""); pdf.saveAs(Paths.get("output.pdf"));
<div class="hsg-featured-snippet">
<h2>如何用 Java 创建 PDF 文件</h2>
<ol>
<li><a class="js-modal-open" data-modal-id="download-modal" href="#download-modal">安装 IronPDF for Java 库</a>。</li>
<li>使用 <code>renderHtmlAsPdf</code> 方法从 HTML 字符串创建 PDF 文件</li>
<li>使用 <code>renderHtmlFileAsPdf</code> 方法从 HTML 文件创建 PDF 文件</li>
<li>使用 <code>renderUrlAsPdf</code> 方法从 URL 创建 PDF</li>
<li>将密码保护的 PDF 文件导出到所需目录</li>
</ol>
</div>
<em>在 Java 中以编程方式创建 PDF,可按需自动生成发票、报告和其他业务文档。
<em>本指南涵盖在 Java 应用程序中使用 IronPDF 以编程方式创建 PDF 文件。
<h2>什么是 IronPDF Java PDF 库?
IronPDF 是一个 Java 库,用于从 HTML 创建 PDF 文档。 它提供创建和自定义 PDF 的功能,包括
1.添加文本、图片和其他内容类型
2.选择字体、颜色并控制布局和格式
IronPDF 基于 .NET Framework 构建,可在 .NET 和 Java 应用程序中使用。 该库支持[自定义水印](https://ironpdf.com/java/how-to/custom-watermark/)、[PDF压缩](https://ironpdf.com/java/how-to/compress-pdf-java-tutorial/)和[表单创建](https://ironpdf.com/java/how-to/create-forms/)等高级功能。
IronPDF 还能处理 PDF 相关任务,包括文件格式转换、文本和数据提取以及密码加密。 您可以根据需要[合并多个 PDF 文件](https://ironpdf.com/java/how-to/java-merge-pdf-tutorial/)或[分割 PDF 文件](https://ironpdf.com/java/examples/split-pdfs/)。
<h2>如何在 Java 应用程序中创建 PDF 文档?
<! -- 待办事项:在此处添加图片 -->
<! --  --> -->。
<!-- 说明:显示逐步过程的截图 -->
<h3>我需要哪些先决条件?
要在 Maven 项目中使用 IronPDF,请确保安装了这些先决条件:
1.**Java Development Kit (JDK):** 编译和运行 Java 应用程序所需的工具。 Download from <a href="https://www.oracle.com/java/technologies/javase-downloads.html" target="_blank" rel="nofollow noopener noreferrer">Oracle website</a>.
2.**Maven:** 下载项目库所需。 从 [Apache Maven 网站](https://maven.apache.org/download.cgi)下载。
3.**IronPdf库:**作为依赖项添加到Maven项目的pom.xml中:
```xml
<dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>ironpdf</artifactId>
<version>1.0.0</version>
</dependency>对于 Gradle 项目,请使用以下方法添加 IronPdf:
implementation 'com.ironsoftware:ironpdf:1.0.0'编写代码前应采取哪些步骤? 首先,在 Java 源文件中添加该导入语句: ```java import com.ironsoftware.ironpdf.*; ``` 对于特定功能,请导入其他类: ```java import com.ironsoftware.ironpdf.render.ChromePdfRenderOptions; import com.ironsoftware.ironpdf.security.SecurityOptions; import com.ironsoftware.ironpdf.security.SecurityManager; ``` 接下来,在 `main` 方法中使用有效的许可证密钥配置 IronPDF: ```java License.setLicenseKey("Your license key"); ``` **注意**: *许可证密钥可去除水印。 [购买许可证密钥](/java/licensing/)或[获取免费试用版](trial-license)。 如果没有许可证,生成的 PDF 文件将带有水印。 有关详细信息,请参见 [ 使用许可证密钥](https://ironpdf.com/java/get-started/license-keys/)。* IronPDF for Java如何用 Java 从 HTML 字符串创建 PDF 文件? 使用 `renderHtmlAsPdf()` 将 HTML 字符串转换为 PDF。 此方法支持 HTML5、CSS3 和 JavaScript 渲染。 向 `renderHtmlAsPdf` 传递 HTML 字符串。 IronPDF 将其转换为 `PdfDocument` 实例: ```java // HTML content to be converted to PDF String htmlString = "
This is an example HTML string.
"; // Convert HTML string to PDF PdfDocument pdf = PdfDocument.renderHtmlAsPdf(htmlString); // Save the PDF document to a file pdf.saveAs(Paths.get("html.pdf")); ``` 这将创建包含 HTML 内容的 "html.pdf"。 包括带有 CSS 样式的复杂 HTML: ```java // HTML with CSS styling String styledHtml = """This PDF was created from HTML with custom CSS styling.
如何用 Java 从 HTML 页面创建 PDF 文件? 从本地 HTML 文件创建 PDF: ```java // Convert HTML file to PDF PdfDocument myPdf = PdfDocument.renderHtmlFileAsPdf("example.html"); // Save the PdfDocument to a file myPdf.saveAs(Paths.get("html_file_saved.pdf")); ``` `renderHtmlFileAsPdf` 方法接受字符串或路径对象形式的文件路径。 IronPDF 使用 CSS 和 JavaScript 呈现 HTML 元素,与浏览器完全一致。 这包括外部 CSS 文件、图像和 JavaScript 库。 详情请参见 [HTML 文件转 PDF](https://ironpdf.com/java/examples/file-to-pdf/) 。 使用 `saveAs` 将 PDF 保存到特定位置。如何用 Java 从 URL 创建 PDF 文件? 使用 `renderUrlAsPdf()` 从网页创建 PDF: ```java // Convert a URL to PDF PdfDocument urlToPdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com"); ``` 对于经过验证的网站,请提供证书: ```java // Create render options with login credentials ChromePdfRenderOptions renderOptions = new ChromePdfRenderOptions(); renderOptions.setAuthUsername("username"); renderOptions.setAuthPassword("password"); // Convert secured URL to PDF PdfDocument securedPdf = PdfDocument.renderUrlAsPdf("https://secure-site.com", renderOptions); securedPdf.saveAs(Paths.get("secured.pdf")); ``` 有关详细信息,请参阅 [URL 转 PDF 代码示例](/java/examples/converting-a-url-to-a-pdf/)。 有关复杂的身份验证,请参阅 [Website & System Logins](https://ironpdf.com/java/examples/ironpdf-website-and-system-logins/)。如何格式化 PDF 文件? 使用 `ChromePdfRenderOptions` 指定 PDF 格式。 配置页面方向、大小和页边距。 将选项作为第二个参数传递给呈现方法: ```java // Create render options with custom settings ChromePdfRenderOptions renderOptions = new ChromePdfRenderOptions(); // Set page orientation to landscape renderOptions.setPaperOrientation(PaperOrientation.LANDSCAPE); // Set custom paper size (Letter size) renderOptions.setPaperSize(PaperSize.LETTER); // Set custom margins (in millimeters) renderOptions.setMarginTop(10); renderOptions.setMarginRight(10); renderOptions.setMarginBottom(10); renderOptions.setMarginLeft(10); // Enable background images and colors renderOptions.setPrintHtmlBackgrounds(true); // Apply options to PDF generation PdfDocument formattedPdf = PdfDocument.renderHtmlAsPdf("", renderOptions); formattedPdf.saveAs(Paths.get("formatted.pdf")); ``` 请参阅 [PDF 生成设置](/java/examples/pdf-generation-settings/) 了解更多选项。 探索[自定义纸张尺寸](https://ironpdf.com/java/examples/custom-pdf-paper-size/)和[自定义页边距](https://ironpdf.com/java/examples/ironpdf-set-custom-margins/)。如何对 PDF 文件进行密码保护? 使用 `SecurityOptions` 对 PDF 进行密码保护: ```java // Create security options and set user password SecurityOptions securityOptions = new SecurityOptions(); securityOptions.setUserPassword("shareable"); ``` 设置高级安全选项: ```java // Advanced security settings SecurityOptions advancedSecurity = new SecurityOptions(); advancedSecurity.setUserPassword("user123"); advancedSecurity.setOwnerPassword("owner456"); // Restrict permissions advancedSecurity.setAllowPrint(false); advancedSecurity.setAllowCopy(false); advancedSecurity.setAllowEditContent(false); advancedSecurity.setAllowEditAnnotations(false); ``` 通过 PDF 的 `SecurityManager` 应用安全性: ```java // Apply security options to the PDF SecurityManager securityManager = urlToPdf.getSecurity(); securityManager.setSecurityOptions(securityOptions); // Save the password-protected PDF document urlToPdf.saveAs("protected.pdf"); ``` 打开 PDF 会触发密码提示:输入正确的密码后,PDF 可正常打开。有关其他设置,请参见 [Security and Metadata Example](/java/examples/security-and-metadata/) 。什么是完整源代码? 本教程的完整源代码: ```java // Import statement for IronPDF Java import com.ironsoftware.ironpdf.*; import java.io.IOException; import java.nio.file.Paths; public class App { public static void main(String[] args) throws IOException { // Apply your license key License.setLicenseKey("Your License Key"); // Convert HTML string to a PDF and save it String htmlString = "
如何格式化 PDF 文件? 使用 `ChromePdfRenderOptions` 指定 PDF 格式。 配置页面方向、大小和页边距。 将选项作为第二个参数传递给呈现方法: ```java // Create render options with custom settings ChromePdfRenderOptions renderOptions = new ChromePdfRenderOptions(); // Set page orientation to landscape renderOptions.setPaperOrientation(PaperOrientation.LANDSCAPE); // Set custom paper size (Letter size) renderOptions.setPaperSize(PaperSize.LETTER); // Set custom margins (in millimeters) renderOptions.setMarginTop(10); renderOptions.setMarginRight(10); renderOptions.setMarginBottom(10); renderOptions.setMarginLeft(10); // Enable background images and colors renderOptions.setPrintHtmlBackgrounds(true); // Apply options to PDF generation PdfDocument formattedPdf = PdfDocument.renderHtmlAsPdf("", renderOptions); formattedPdf.saveAs(Paths.get("formatted.pdf")); ``` 请参阅 [PDF 生成设置](/java/examples/pdf-generation-settings/) 了解更多选项。 探索[自定义纸张尺寸](https://ironpdf.com/java/examples/custom-pdf-paper-size/)和[自定义页边距](https://ironpdf.com/java/examples/ironpdf-set-custom-margins/)。如何对 PDF 文件进行密码保护? 使用 `SecurityOptions` 对 PDF 进行密码保护: ```java // Create security options and set user password SecurityOptions securityOptions = new SecurityOptions(); securityOptions.setUserPassword("shareable"); ``` 设置高级安全选项: ```java // Advanced security settings SecurityOptions advancedSecurity = new SecurityOptions(); advancedSecurity.setUserPassword("user123"); advancedSecurity.setOwnerPassword("owner456"); // Restrict permissions advancedSecurity.setAllowPrint(false); advancedSecurity.setAllowCopy(false); advancedSecurity.setAllowEditContent(false); advancedSecurity.setAllowEditAnnotations(false); ``` 通过 PDF 的 `SecurityManager` 应用安全性: ```java // Apply security options to the PDF SecurityManager securityManager = urlToPdf.getSecurity(); securityManager.setSecurityOptions(securityOptions); // Save the password-protected PDF document urlToPdf.saveAs("protected.pdf"); ``` 打开 PDF 会触发密码提示:输入正确的密码后,PDF 可正常打开。有关其他设置,请参见 [Security and Metadata Example](/java/examples/security-and-metadata/) 。什么是完整源代码? 本教程的完整源代码: ```java // Import statement for IronPDF Java import com.ironsoftware.ironpdf.*; import java.io.IOException; import java.nio.file.Paths; public class App { public static void main(String[] args) throws IOException { // Apply your license key License.setLicenseKey("Your License Key"); // Convert HTML string to a PDF and save it String htmlString = "
什么是完整源代码? 本教程的完整源代码: ```java // Import statement for IronPDF Java import com.ironsoftware.ironpdf.*; import java.io.IOException; import java.nio.file.Paths; public class App { public static void main(String[] args) throws IOException { // Apply your license key License.setLicenseKey("Your License Key"); // Convert HTML string to a PDF and save it String htmlString = "
This is an example HTML string.
"; PdfDocument pdf = PdfDocument.renderHtmlAsPdf(htmlString); pdf.saveAs(Paths.get("html.pdf")); // Convert HTML file to a PDF and save it PdfDocument myPdf = PdfDocument.renderHtmlFileAsPdf("example.html"); myPdf.saveAs(Paths.get("html_file_saved.pdf")); // Convert URL to a PDF and save it PdfDocument urlToPdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com"); urlToPdf.saveAs(Paths.get("urlToPdf.pdf")); // Password-protect the PDF file SecurityOptions securityOptions = new SecurityOptions(); securityOptions.setUserPassword("shareable"); SecurityManager securityManager = urlToPdf.getSecurity(); securityManager.setSecurityOptions(securityOptions); urlToPdf.saveAs(Paths.get("protected.pdf")); } } ``` IronPDF 可在不丢失格式的情况下渲染图像和文本。 按钮保持可点击,文本框保持可编辑。 该库支持[添加签名](https://ironpdf.com/java/examples/pdf-signatures/)、[渲染图表](https://ironpdf.com/java/examples/js-charts-to-pdf/)和[打印 PDF](https://ironpdf.com/java/how-to/java-print-pdf-tutorial/) 。主要收获是什么? 本指南演示了使用 IronPDF 在 Java 中创建 PDF。 IronPDF 提供了一个简单的 API,用于从 HTML 文件、XML 文档或其他来源生成 PDF。 快速生成报告、发票或任何文档类型。 部署在 [AWS](https://ironpdf.com/java/get-started/aws/)、[Azure](https://ironpdf.com/java/get-started/azure/) 或 [Google Cloud](https://ironpdf.com/java/get-started/google-cloud/) 上。 IronPDF for Java 需要[商业许可证](/java/licensing/),起价为 $799。 [获取免费试用版](trial-license),用于生产测试。 *[下载 IronPDF for Java 库](/java/how-to/java-create-pdf-tutorial/).*。
常见问题解答
用 Java 创建 PDF 文件的最简单方法是什么?
在 Java 中创建 PDF 的最简单方法是使用 IronPDF for Java 的 renderHtmlAsPdf() 方法。只需向该方法传递 HTML 字符串并保存结果即可:PdfDocument pdf = PdfDocument.renderHtmlAsPdf("Hello World!"); pdf.saveAs(Paths.get("output.pdf"));
如何将 IronPDF 添加到我的 Maven 项目中?
在 pom.xml 文件中加入此依赖项,将 IronPDF 添加到您的 Maven 项目中:
我能否将现有的 HTML 文件转换为 PDF 格式?
是的,IronPDF 提供了 renderHtmlFileAsPdf() 方法,专门用于将 HTML 文件转换为 PDF。该方法从文件系统中读取 HTML 文件,然后将其转换为 PDF 文档。
如何用 Java 从网页生成 PDF?
IronPDF 提供 renderUrlAsPdf() 方法,可将网页直接转换为 PDF。只需提供您要转换的网页的 URL,IronPDF 就会将其渲染为 PDF 文档。
我可以通过编程创建哪些类型的业务文档?
IronPdf 可自动生成各种商业文档,包括发票、报告、表格和其他按需文档。该库支持自定义水印、PDF 压缩和表单创建等高级功能。
是否可以创建受密码保护的 PDF?
是的,IronPDF 支持对 PDF 文件进行密码加密。您可以将受密码保护的 PDF 文件导出到所需目录,确保敏感商业信息的文档安全。
在Java中使用IronPDF的系统要求是什么?
要在 Java 中使用 IronPDF,您需要使用 Java Development Kit (JDK) 来编译和运行应用程序,使用 Maven 或 Gradle 进行依赖关系管理,并将 IronPDF 库作为依赖关系添加到您的项目中。
我能否处理现有的 PDF 文件,而不仅仅是创建新的 PDF 文件?
是的,IronPDF 可处理创建以外的各种 PDF 操作任务。您可以使用该库的综合功能合并多个 PDF、分割 PDF、提取文本和数据以及执行文件格式转换。









