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

根据poi-tl做的,场景是,有些需要套红文件,需要把一个word内容,插入到另一个word中,这种写法,只支持docx,因为poi的xwpf写法,只能读取xml,而docx解析出来的是xml,doc就得是另一种写法了。
但对于编号的识别不是太友好,有时候识别出来就变成%1的情况了,也许是我用的版本不是最新的,但,因为对编号的需求比较高,所以这种方法也就没再继续研究下去,后来使aspose.words做了

 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());
                }
            }
        }
    }
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值