poi-tl 在一个word里写入另一个word,保持word内容无变化

根据poi-tl做的,场景是,有些需要套红文件,需要把一个word内容,插入到另一个word中,这种写法,只支持docx,因为poi的xwpf写法,只能读取xml,而docx解析出来的是xml,doc就得是另一种写法了。

但对于编号的识别不是太友好,有时候识别出来就变成%1的情况了,也许是我用的版本不是最新的,但,因为对编号的需求比较高,所以这种方法也就没再继续研究下去,后来使aspose.words做了

java 复制代码
 public static void main(String[] args) throws IOException {
            // 主文档路径
            String mainDocPath = "D:\\test\\aa.docx";
            // 要嵌入的文档路径
            String embedDocPath = "D:\\test\\oc.docx";
            // 输出文件路径
            String outputDocPath = "D:\\test\\out2.docx";

            // 读取主文档
            try (FileInputStream fis = new FileInputStream(mainDocPath);
                 XWPFDocument mainDoc = new XWPFDocument(fis)) {

                // 读取嵌入文档
                try (FileInputStream fisEmbed = new FileInputStream(embedDocPath);
                     XWPFDocument embedDoc = new XWPFDocument(fisEmbed)) {

                    // 获取主文档中的所有段落
                    List<XWPFParagraph> paragraphs = mainDoc.getParagraphs();

                    // 确定插入位置,例如第7行(索引6)
                    int insertIndex = 6;
                    if (insertIndex < paragraphs.size()) {
                        // 在目标位置插入嵌入文档的内容
                        XWPFParagraph targetParagraph = paragraphs.get(insertIndex);

//                        // 将嵌入文档中的每一段插入到目标文档中
//                        for (XWPFParagraph paragraph : embedDoc.getParagraphs()) {
//                            XWPFRun newRun = targetParagraph.createRun();
//                            newRun.setText(paragraph.getText());
//                            newRun.addBreak();
//                        }
                        // 遍历嵌入文档中的段落并复制格式和内容
                        for (XWPFParagraph paragraph : embedDoc.getParagraphs()) {
                            // 创建一个新的段落
                            XWPFParagraph newParagraph = mainDoc.insertNewParagraph(targetParagraph.getCTP().newCursor());
//                            XWPFParagraph newParagraph = mainDoc.createParagraph();
                            copyParagraphFormatting(paragraph, newParagraph);

                            // 复制每个Run的内容和格式
                            for (XWPFRun run : paragraph.getRuns()) {
                                XWPFRun newRun = newParagraph.createRun();
                                copyRunFormatting(run, newRun);
                                newRun.setText(run.getText(0));
                            }
                            newParagraph.setSpacingAfter(200); // 添加段落之间的间距
                        }
                    }

                    // 将合并后的文档保存到输出文件
                    try (FileOutputStream out = new FileOutputStream(outputDocPath)) {
                        mainDoc.write(out);
                    }
                }
            }

            System.out.println("Document saved successfully.");
        }

    // 复制段落的格式
    private static void copyParagraphFormatting(XWPFParagraph srcParagraph, XWPFParagraph destParagraph) {
        // 将源段落的对齐方式设置到目标段落
        destParagraph.setAlignment(srcParagraph.getAlignment());
        // 将源段落的垂直对齐方式设置到目标段落
        destParagraph.setVerticalAlignment(srcParagraph.getVerticalAlignment());
        // 设置目标段落在源段落之后的间距(段落间距)
        destParagraph.setSpacingAfter(srcParagraph.getSpacingAfter());
        // 设置目标段落在源段落之前的间距
        destParagraph.setSpacingBefore(srcParagraph.getSpacingBefore());
        // 设置目标段落的行间距(段落内部行与行之间的距离)
        destParagraph.setSpacingBetween(srcParagraph.getSpacingBetween());
//        // 设置目标段落的首行缩进量
        destParagraph.setIndentationFirstLine(srcParagraph.getIndentationFirstLine());
//        // 设置目标段落的悬挂缩进量
//        destParagraph.setIndentationHanging(srcParagraph.getIndentationHanging());
        // 设置目标段落的左边缩进量
        destParagraph.setIndentationLeft(srcParagraph.getIndentationLeft());
        // 设置目标段落的右边缩进量
        destParagraph.setIndentationRight(srcParagraph.getIndentationRight());
        // 将多行倍距调整为1
        destParagraph.setSpacingBetween(1.0);

        if (srcParagraph.getNumFmt() != null){
            String numLevelText = srcParagraph.getNumLevelText();
            BigInteger numID = srcParagraph.getNumID();
            BigInteger numIlvl = srcParagraph.getNumIlvl();

            destParagraph.setNumID(numID);
            destParagraph.setNumILvl(numIlvl);

            // 创建新的运行并设置编号文本
            XWPFRun run = destParagraph.createRun();
            // 将编号文本设置为运行的内容
            run.setText(numLevelText);
        }
    }

    // 复制Run的格式
    private static void copyRunFormatting(XWPFRun srcRun, XWPFRun destRun) {
        // 设置目标文本是否为粗体,与源文本相同
        destRun.setBold(srcRun.isBold());
        // 设置目标文本是否为斜体,与源文本相同
        destRun.setItalic(srcRun.isItalic());
        // 设置目标文本的下划线样式,与源文本相同
        destRun.setUnderline(srcRun.getUnderline());
        // 设置目标文本是否为删除线,与源文本相同
        destRun.setStrike(srcRun.isStrike());
        // 设置目标文本的字体类型,与源文本相同
        destRun.setFontFamily(srcRun.getFontFamily());
        // 设置目标文本的字体大小,与源文本相同
        destRun.setFontSize(srcRun.getFontSize());
        // 设置目标文本的字体颜色,与源文本相同
        destRun.setColor(srcRun.getColor());
        // 设置目标文本的下标状态,与源文本相同
        destRun.setSubscript(srcRun.getSubscript());
        // 设置目标文本的垂直位置,与源文本相同
        destRun.setTextPosition(srcRun.getTextPosition());
        if (srcRun.getCTR() != null) {
            if (srcRun.getCTR().isSetRPr()) {
                if (srcRun.getCTR().getRPr().isSetB()) {
                    destRun.setBold(srcRun.isBold());
                }
                if (srcRun.getCTR().getRPr().isSetI()) {
                    destRun.setItalic(srcRun.isItalic());
                }
                if (srcRun.getCTR().getRPr().isSetStrike()) {
                    destRun.setStrike(srcRun.isStrike());
                }
                if (srcRun.getCTR().getRPr().isSetColor()) {
                    destRun.setColor(srcRun.getColor());
                }
                if (srcRun.getCTR().getRPr().isSetSz()) {
                    destRun.setFontSize(srcRun.getFontSize());
                }
                if (srcRun.getCTR().getRPr().isSetRFonts()) {
                    destRun.setFontFamily(srcRun.getFontFamily());
                }
            }
        }
    }
相关推荐
EatFan2 小时前
Spring Boot 4.0 升级清单:JDK 基线、Jakarta 命名空间、Observation API 与第三方组件兼容一次讲清
java·spring boot·后端
辰海Coding3 小时前
平台如何接入一台物联网设备?又该如何设计它的通信协议?
java·物联网
代码民工正电荷3 小时前
JAVA重要知识点和集合
java·开发语言
李少兄4 小时前
IDEA Apifox Helper 插件踩坑:2.0.15-243 版本“导出指定接口”参数描述丢失问题解析与修复方案
java·ide·intellij-idea
努力攀登的小k5 小时前
《新手练表:从建表到删表全操作|从零学会表操作》
java·数据库·mysql
Yetian_20005 小时前
Postman测试上传图片文件接口
java·postman
爪哇碎碎念6 小时前
【Java】从一次 cache.get(key) 进去:缓存击穿一瞬间打出 200 次 DB,JDK 17 实测三种解法
java·redis·缓存
csdn_aspnet6 小时前
Java 使总距离最短的最佳点位置(Optimum location of point to minimize total distance)
java·算法
源码_空间6 小时前
基于Springboot的在线编程练习判题平台(源码+文档)
java·spring boot·后端
小手cool7 小时前
二分查找:在有序数组中查找最接近 target 的元素索引
java·数据结构·算法