【Java】多pdf文件合并为一个.docx文件

当将多个 PDF 文件合并成单个 DOCX 文档时,利用 Java 中的 Apache PDFBox 和 Apache POI 库可以实现这一目标。这个过程可以分为几个步骤:

1. 导入所需的库

使用 Apache PDFBox 和 Apache POI 库来处理 PDF 和 DOCX 文件。你需要导入相关库,并确保在项目中使用了正确的依赖。

go 复制代码
<dependency>
     <groupId>org.apache.pdfbox</groupId>
     <artifactId>pdfbox</artifactId>
     <version>2.0.29</version>
</dependency>

2. 获取PDF文件列表

通过指定文件夹路径,遍历文件夹中的所有 PDF 文件,并将其存储在一个列表中。

3. 创建DOCX文档对象

使用 Apache POI 的 XWPFDocument 类创建一个新的空白 DOCX 文档对象。

4. 逐个处理PDF文件

对于每个 PDF 文件:

  • 使用 PDFBox 的 PDDocument 加载 PDF 文件。
  • 遍历 PDF 文件的每一页,将每一页渲染为图像(PNG 格式)。
  • 将图像插入到 DOCX 文档中对应的段落中,并保留文件名作为文字内容。
  • 创建一个空的XWPFDocument对象,即将用于存储合并后的文档内容。

5. 将结果保存为DOCX文件

最后,将生成的 DOCX 文档保存到指定的输出路径。

java 复制代码
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.*;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;

public class MergePDFsToDOCX {

    public static void main(String[] args) {
        String pdfFolderPath = "\\mergePdfsTODocument"; // 替换为包含PDF文件的文件夹路径
        String outputDOCXPath = "\\mergePdfsTODocument\\document.docx"; // 输出的DOCX文件路径

        try {
            List<File> pdfFiles = getPdfFilesInFolder(pdfFolderPath);
            mergePDFsToDocx(pdfFiles, outputDOCXPath);
            System.out.println("PDFs merged to DOCX successfully: " + outputDOCXPath);
        } catch (IOException e) {
            System.err.println("Failed to merge PDFs to DOCX.");
            e.printStackTrace();
        }
    }

    public static List<File> getPdfFilesInFolder(String folderPath) {
        File folder = new File(folderPath);
        List<File> pdfFiles = new ArrayList<>();

        if (folder.exists() && folder.isDirectory()) {
            File[] files = folder.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (file.isFile() && file.getName().toLowerCase().endsWith(".pdf")) {
                        pdfFiles.add(file);
                    }
                }
            }
        } else {
            System.err.println("Folder not found: " + folderPath);
        }
        return pdfFiles;
    }

    public static void mergePDFsToDocx(List<File> pdfFiles, String outputDOCXPath) throws IOException {
        XWPFDocument doc = new XWPFDocument();

        for (File pdfFile : pdfFiles) {
            try (PDDocument pdf = PDDocument.load(pdfFile)) {
                PDFRenderer pdfRenderer = new PDFRenderer(pdf);
                int numPages = pdf.getNumberOfPages();

                for (int pageIndex = 0; pageIndex < numPages; pageIndex++) {
                    BufferedImage bim = pdfRenderer.renderImageWithDPI(pageIndex, 300); // 渲染PDF页面为图像
                    String imgFileName = pdfFile.getName() + "_page_" + (pageIndex + 1) + ".png"; // 图像文件名
                    File imageFile = new File(imgFileName);

                    ImageIO.write(bim, "png", imageFile); // 将图像写入文件

                    FileInputStream imageStream = new FileInputStream(imageFile);
                    XWPFParagraph paragraph = doc.createParagraph();
                    XWPFRun run = paragraph.createRun();
                    run.setText(pdfFile.getName());
                    run.addBreak();

                    doc.createParagraph().createRun().addPicture(
                            imageStream, Document.PICTURE_TYPE_PNG, imgFileName, Units.toEMU(400), Units.toEMU(400));

                    imageStream.close();
                    imageFile.delete(); // 删除临时图像文件
                }
            } catch (IOException e) {
                System.err.println("Failed to process file: " + pdfFile.getName());
                e.printStackTrace();
            } catch (InvalidFormatException e) {
                throw new RuntimeException(e);
            }
        }

        try (FileOutputStream fos = new FileOutputStream(outputDOCXPath)) {
            doc.write(fos);
        }
    }
}
相关推荐
FuLLovers23 分钟前
2024-09-13 冯诺依曼体系结构 OS管理 进程
linux·开发语言
IT毕设梦工厂1 小时前
计算机毕业设计选题推荐-在线拍卖系统-Java/Python项目实战
java·spring boot·python·django·毕业设计·源码·课程设计
everyStudy1 小时前
JS中判断字符串中是否包含指定字符
开发语言·前端·javascript
luthane1 小时前
python 实现average mean平均数算法
开发语言·python·算法
Ylucius1 小时前
动态语言? 静态语言? ------区别何在?java,js,c,c++,python分给是静态or动态语言?
java·c语言·javascript·c++·python·学习
凡人的AI工具箱2 小时前
AI教你学Python 第11天 : 局部变量与全局变量
开发语言·人工智能·后端·python
sleP4o2 小时前
Python操作MySQL
开发语言·python·mysql
是店小二呀2 小时前
【C++】C++ STL探索:Priority Queue与仿函数的深入解析
开发语言·c++·后端
七夜zippoe2 小时前
分布式系统实战经验
java·分布式
洛寒瑜2 小时前
【读书笔记-《30天自制操作系统》-23】Day24
开发语言·汇编·笔记·操作系统·应用程序