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

如何在 Java 中合并两个 PDF 文件

本文将演示如何使用IronPDF Library for Java合并多个PDF文档。 我们将经历设置环境、导入库、读取输入文件并将它们合并为一个文档的过程。

class="hsg-featured-snippet">

如何使用Java合并两个PDF文件

  1. 安装Java库以执行PDF合并
  2. 导入或呈现要合并的PDF
  3. 使用PdfDocument类合并PDF
  4. 将合并后的PDF导出为单个文件
  5. 检查生成的PDF

IronPDF for Java

IronPDF for Java 是一个功能强大的库,允许开发人员从零开始创建新的 PDF 文档,并将各种文件格式转换为 PDF 文档。 它还能将多个 PDF 文件合并为一个文档。

IronPDF for Java 易于使用,其简单直观的 API 可让开发人员轻松创建 PDF 文件。 It also supports methods for Rendering Charts in PDFs, working with PDF Forms, and even handling Digital Signatures Programmatically.

前提条件

在实现之前,必须满足一些先决条件以进行PDF创建过程。

1.Java 应安装在系统中,其路径应设置在环境变量中。 如果您尚未安装Java,请参阅Java网站的安装指南以获取说明。 2.应安装 Eclipse 或 IntelliJ 等 Java IDE。 You can download Eclipse from this official Eclipse download page and IntelliJ from JetBrains' download section. 3.应下载 IronPDF for Java 库并将其作为依赖项添加到您的项目中。 您可以在IronPDF官方网站上了解如何操作。 4.在开始进行 PDF 转换之前,应安装 Maven 并与您的集成开发环境集成。 关于安装Maven及将其集成到您的环境的教程,请访问JetBrains的Maven分步教程

IronPDF for Java安装

如果满足所有要求,IronPDF for Java 的安装相当简单明了,即使是 Java 新手也能轻松上手。

本文将使用JetBrains的IntelliJ IDEA安装和运行示例。

首先,打开 JetBrains IntelliJ IDEA 并创建一个新的 Maven 项目。

如何使用Java合并两个PDF文件,图1:IntelliJ中的新Maven项目 New Maven Project in IntelliJ

将出现一个新窗口。 输入项目名称并点击完成。

如何使用Java合并两个PDF文件,图2:命名Maven项目并点击完成 Name the Maven Project and click Finish

点击完成后,将打开一个新的项目,通过pom.xml添加IronPDF for Java的Maven依赖。

如何使用Java合并两个PDF文件,图3:pom.xml文件 The pom.xml file

pom.xml文件中添加以下依赖,或者可以从以下Maven Central的IronPDF列表下载JAR文件。

<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf</artifactId>
    <version>YOUR_DESIRED_VERSION_HERE</version>
</dependency>
<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf</artifactId>
    <version>YOUR_DESIRED_VERSION_HERE</version>
</dependency>
XML

将依赖项放置在pom.xml文件中后,文件的右上角会出现一个小图标。

如何使用Java合并两个PDF文件,图4:单击浮动图标以自动安装Maven依赖项 Click the floating icon to install the Maven dependencies automatically

点击此图标安装 IronPDF for Java 的 Maven 依赖项。 根据您的网络连接情况,这只需要几分钟时间。

合并多个PDF文档

IronPDF for Java 允许您使用 Java 程序将多个 PDF 文档合并为一个 PDF 文档。 IronPDF 提供多种合并 PDF 文档的方法:

1.创建两个新的 PDF 文档并将它们合并为一个 PDF。 2.将输入的 PDF 文件打开,合并成 PDF。 3.合并两个以上的 PDF 文档。

创建两个新的PDF文档并将它们合并

import com.ironsoftware.ironpdf.PdfDocument;

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

// This class demonstrates how to create and merge two PDF documents using the IronPDF library.
public class Main {
    public static void main(String[] args) throws IOException {
        // Define the HTML content for the first PDF document
        String htmlA = "<p> [PDF_1] </p>"
                + "<p> Hi this is the first PDF </p>";
        // Define the HTML content for the second PDF document
        String htmlB = "<p> [PDF_2] </p>"
                + "<p> This is the 2nd PDF </p>";

        // Render the HTML content to create two separate PDF documents
        PdfDocument pdfA = PdfDocument.renderHtmlAsPdf(htmlA);
        PdfDocument pdfB = PdfDocument.renderHtmlAsPdf(htmlB);
        // Merge the two PDF documents into one
        PdfDocument merged = PdfDocument.merge(pdfA, pdfB);

        // Save the merged PDF document to the specified path
        merged.saveAs(Paths.get("assets/merged.pdf"));
    }
}
import com.ironsoftware.ironpdf.PdfDocument;

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

// This class demonstrates how to create and merge two PDF documents using the IronPDF library.
public class Main {
    public static void main(String[] args) throws IOException {
        // Define the HTML content for the first PDF document
        String htmlA = "<p> [PDF_1] </p>"
                + "<p> Hi this is the first PDF </p>";
        // Define the HTML content for the second PDF document
        String htmlB = "<p> [PDF_2] </p>"
                + "<p> This is the 2nd PDF </p>";

        // Render the HTML content to create two separate PDF documents
        PdfDocument pdfA = PdfDocument.renderHtmlAsPdf(htmlA);
        PdfDocument pdfB = PdfDocument.renderHtmlAsPdf(htmlB);
        // Merge the two PDF documents into one
        PdfDocument merged = PdfDocument.merge(pdfA, pdfB);

        // Save the merged PDF document to the specified path
        merged.saveAs(Paths.get("assets/merged.pdf"));
    }
}
JAVA

如何使用Java合并两个PDF文件,图5:新的PDF文件合并器 新的PDF文件合并器

将现有文件合并为一个PDF

IronPDF允许您将现有的PDF文件合并为一个公共PDF文件。只需指定PDF输入文件列表。 IronPDF将所有PDF文件合并为单个PDF文档并保存到目标文件。输出将包含成功合并的PDF文件的结果。

import com.ironsoftware.ironpdf.PdfDocument;

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

// This class demonstrates how to merge existing PDF files using the IronPDF library.
public class Main {
    public static void main(String[] args) throws IOException {
        // Load the existing PDF files from the specified paths
        PdfDocument pdfA = PdfDocument.fromFile(Paths.get("assets/1.pdf"));
        PdfDocument pdfB = PdfDocument.fromFile(Paths.get("assets/2.pdf"));
        // Merge the two PDF documents into one
        PdfDocument merged = PdfDocument.merge(pdfA, pdfB);

        // Save the merged PDF document to the specified path
        merged.saveAs(Paths.get("assets/merged.pdf"));
    }
}
import com.ironsoftware.ironpdf.PdfDocument;

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

// This class demonstrates how to merge existing PDF files using the IronPDF library.
public class Main {
    public static void main(String[] args) throws IOException {
        // Load the existing PDF files from the specified paths
        PdfDocument pdfA = PdfDocument.fromFile(Paths.get("assets/1.pdf"));
        PdfDocument pdfB = PdfDocument.fromFile(Paths.get("assets/2.pdf"));
        // Merge the two PDF documents into one
        PdfDocument merged = PdfDocument.merge(pdfA, pdfB);

        // Save the merged PDF document to the specified path
        merged.saveAs(Paths.get("assets/merged.pdf"));
    }
}
JAVA

如何使用Java合并两个PDF文件,图6:现有PDF合并输出 现有PDF合并输出

合并多于两个PDF文档

您可以使用 IronPDF for Java 轻松合并两个以上的 PDF 文件。

import com.ironsoftware.ironpdf.PdfDocument;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

// This class demonstrates how to merge more than two PDF documents using the IronPDF library.
public class Main {
    public static void main(String[] args) throws IOException {
        // Create a list to hold the PDF documents
        List<PdfDocument> pdfList = new ArrayList<>();
        // Add existing PDF files to the list
        pdfList.add(PdfDocument.fromFile(Paths.get("assets/1.pdf")));
        pdfList.add(PdfDocument.fromFile(Paths.get("assets/2.pdf")));
        pdfList.add(PdfDocument.fromFile(Paths.get("assets/3.pdf")));
        // Merge all PDF documents in the list into one
        PdfDocument merged = PdfDocument.merge(pdfList);
        // Save the merged PDF document to the specified path
        merged.saveAs(Paths.get("assets/merged.pdf"));
    }
}
import com.ironsoftware.ironpdf.PdfDocument;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

// This class demonstrates how to merge more than two PDF documents using the IronPDF library.
public class Main {
    public static void main(String[] args) throws IOException {
        // Create a list to hold the PDF documents
        List<PdfDocument> pdfList = new ArrayList<>();
        // Add existing PDF files to the list
        pdfList.add(PdfDocument.fromFile(Paths.get("assets/1.pdf")));
        pdfList.add(PdfDocument.fromFile(Paths.get("assets/2.pdf")));
        pdfList.add(PdfDocument.fromFile(Paths.get("assets/3.pdf")));
        // Merge all PDF documents in the list into one
        PdfDocument merged = PdfDocument.merge(pdfList);
        // Save the merged PDF document to the specified path
        merged.saveAs(Paths.get("assets/merged.pdf"));
    }
}
JAVA

结论

本文介绍了如何使用Java和IronPDF库合并多个PDF文件。 按照本文概述的步骤,您将能够设置环境、导入库、读取输入文件并将它们合并到一个文档中。

For more information about Merging PDF Files in Java Using IronPDF and for similar tutorials on how to Create PDFs from HTML and Format PDFs with IronPDF, Explore Our Comprehensive Documentation.

IronPDF for Java可免费用于开发目的,但在生产环境中使用时需要商业许可

常见问题解答

如何使用Java合并两个PDF文件?

您可以在IronPDF for Java中使用PdfDocument类来合并两个PDF文件。首先,使用PdfDocument.fromFile方法加载PDF文档,然后使用merge方法将它们合并为单个文档,最后使用saveAs保存输出。

设置IronPDF for Java的步骤是什么?

要设置IronPDF for Java,请确保安装了Java和Java IDEA(如IntelliJ)。通过从IronPDF网站或Maven Central包含必要的依赖项,将IronPDF添加为项目中的Maven依赖项。

IronPDF for Java可以合并多个PDF文件吗?

是的,IronPDF for Java可以合并多个PDF文件。您可以将多个PDF文档加载到列表中,并使用merge方法将它们合并为一个PDF文档。

如何在Java中从HTML创建PDF文档?

IronPDF for Java允许您使用HtmlToPdf.renderHtmlAsPdf方法从HTML创建PDF文档。您可以直接将HTML字符串或文件渲染为PDF。

IronPDF for Java适合生产环境吗?

IronPDF for Java可供开发目的免费使用,但在生产环境中部署需要商业许可证。

使用 IronPDF 在 Java 中的先决条件是什么?

先决条件包括安装和配置Java,使用Java IDE(如IntelliJ IDEA),并在项目中集成IronPDF作为Maven依赖项。

在哪里可以找到IronPDF for Java的文档?

IronPDF for Java的全面文档,包括关于合并PDF和从HTML创建PDF的指南,可以在IronPDF官方网站上找到。

如何在Java中合并PDF时排除问题?

确保所有PDF文件通过IronPDF的PdfDocument.fromFile方法正确加载,并验证IronPDF库是否已正确添加为Maven依赖项。查看IronPDF文档以获取其他故障排除技巧。

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

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

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

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