JAVA合并word和pdf文档

参考:

https://blog.csdn.net/yqwang75457/article/details/101147683
https://blog.csdn.net/qq_35024768/article/details/110005218

操作:

1、导入POM依赖

    
        org.apache.poi
        poi-ooxml
        4.1.2
    
    
        org.apache.pdfbox
        pdfbox
        2.0.4
    

2、工具类:

import java.io.*;
import java.util.*;

import cn.hutool.core.io.FileUtil;
import org.apache.pdfbox.io.MemoryUsageSetting;
import org.apache.pdfbox.multipdf.PDFMergerUtility;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.usermodel.BreakType;
import org.apache.poi.xwpf.usermodel.Document;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFPictureData;
import org.apache.xmlbeans.XmlOptions;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBody;
/**
* 合成文档工具类
* @author ppp
* @date 2023/5/20
*/
public class CompoundFileUtil {
    public static void main (String[] args) throws Exception {
        String dir = "C:\\Users\\ppp\\Documents\\试卷";
        File file = FileUtil.file(dir);
        String[] list = file.list();

        List pdfList = new ArrayList<>();
        List docxList = new ArrayList<>();
        for (int i = 0; i < Objects.requireNonNull(list).length; i++) {
            if (list[i].contains(".pdf")){
                File file1 = FileUtil.file(dir, list[i]);
                pdfList.add(file1);
            }
            if (list[i].contains(".docx")){
                File file1 = FileUtil.file(dir, list[i]);
                docxList.add(file1);
            }
        }
        mulPdfFile2One(pdfList, dir+"\\"+"合并.pdf");
        mulWordFile2One(docxList, dir+"\\"+"合并.docx");
    }
    public static void mulWordFile2One(List files, String targetPath) throws Exception {
        try( OutputStream dest = new FileOutputStream(targetPath);) {
            ArrayList documentList = new ArrayList<>();
            XWPFDocument doc = null;
            for (File file : files) {
                try(FileInputStream in =new FileInputStream(file.getAbsoluteFile())) {
                    OPCPackage open = OPCPackage.open(in);
                    XWPFDocument document = new XWPFDocument(open);
                    documentList.add(document);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
            for (int i = 0; i < documentList.size(); i++) {
                doc = documentList.get(0);
                if(i == 0){//首页直接分页,不再插入首页文档内容
                    documentList.get(i).createParagraph().createRun().addBreak(BreakType.PAGE);
//                    appendBody(doc,documentList.get(i));
                }else if(i == documentList.size()-1){//尾页不再分页,直接插入最后文档内容
                    appendBody(doc,documentList.get(i));
                }else{
                    documentList.get(i).createParagraph().createRun().addBreak(BreakType.PAGE);
                    appendBody(doc,documentList.get(i));
                }
            }
            assert doc != null;
            doc.write(dest);
        }
    }

    public static void mulPdfFile2One(List files, String targetPath) throws IOException{
        // pdf合并工具类
        PDFMergerUtility mergePdf = new PDFMergerUtility();
        for (File f : files) {
            if(f.exists() && f.isFile()){
                // 循环添加要合并的pdf
                mergePdf.addSource(f);
            }
        }
        // 设置合并生成pdf文件名称
        mergePdf.setDestinationFileName(targetPath);
        // 合并pdf
        mergePdf.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());
        FileUtil.touch(targetPath);
    }

    public static void appendBody(XWPFDocument src, XWPFDocument append) throws Exception {
        CTBody src1Body = src.getDocument().getBody();
        CTBody src2Body = append.getDocument().getBody();

        List allPictures = append.getAllPictures();
        // 记录图片合并前及合并后的ID
        Map map = new HashMap();
        for (XWPFPictureData picture : allPictures) {
            String before = append.getRelationId(picture);
            //将原文档中的图片加入到目标文档中
            String after = src.addPictureData(picture.getData(), Document.PICTURE_TYPE_PNG);
            map.put(before, after);
        }
        appendBody(src1Body, src2Body,map);
    }

    private static void appendBody(CTBody src, CTBody append,Map map) throws Exception {
        XmlOptions optionsOuter = new XmlOptions();
        optionsOuter.setSaveOuter();
        String appendString = append.xmlText(optionsOuter);

        String srcString = src.xmlText();
        String prefix = srcString.substring(0,srcString.indexOf(">")+1);
        String mainPart = srcString.substring(srcString.indexOf(">")+1,srcString.lastIndexOf("<"));
        String sufix = srcString.substring( srcString.lastIndexOf("<") );
        String addPart = appendString.substring(appendString.indexOf(">") + 1, appendString.lastIndexOf("<"));
        if (map != null && !map.isEmpty()) {
            //对xml字符串中图片ID进行替换
            for (Map.Entry set : map.entrySet()) {
                addPart = addPart.replace(set.getKey(), set.getValue());
            }
        }
        //将两个文档的xml内容进行拼接
        CTBody makeBody = CTBody.Factory.parse(prefix+mainPart+addPart+sufix);
        src.set(makeBody);
    }
}

你可能感兴趣的:(poi,java工具类,java,word,pdf)