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

[复制链接]
发表于 2026-2-1 02:32:32 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
根据poi-tl做的,场景是,有些必要套红文件,必要把一个word内容,插入到另一个word中,这种写法,只支持docx,由于poi的xwpf写法,只能读取xml,而docx分析出来的是xml,doc就得是另一种写法了。
但对于编号的辨认不是太友好,偶然候辨认出来就酿成%1的环境了,大概是我用的版本不是最新的,但,由于对编号的需求比力高,以是这种方法也就没再继续研究下去,厥后使aspose.words做了
  1. public static void main(String[] args) throws IOException {
  2.             // 主文档路径
  3.             String mainDocPath = "D:\\test\\aa.docx";
  4.             // 要嵌入的文档路径
  5.             String embedDocPath = "D:\\test\\oc.docx";
  6.             // 输出文件路径
  7.             String outputDocPath = "D:\\test\\out2.docx";
  8.             // 读取主文档
  9.             try (FileInputStream fis = new FileInputStream(mainDocPath);
  10.                  XWPFDocument mainDoc = new XWPFDocument(fis)) {
  11.                 // 读取嵌入文档
  12.                 try (FileInputStream fisEmbed = new FileInputStream(embedDocPath);
  13.                      XWPFDocument embedDoc = new XWPFDocument(fisEmbed)) {
  14.                     // 获取主文档中的所有段落
  15.                     List<XWPFParagraph> paragraphs = mainDoc.getParagraphs();
  16.                     // 确定插入位置,例如第7行(索引6)
  17.                     int insertIndex = 6;
  18.                     if (insertIndex < paragraphs.size()) {
  19.                         // 在目标位置插入嵌入文档的内容
  20.                         XWPFParagraph targetParagraph = paragraphs.get(insertIndex);
  21. //                        // 将嵌入文档中的每一段插入到目标文档中
  22. //                        for (XWPFParagraph paragraph : embedDoc.getParagraphs()) {
  23. //                            XWPFRun newRun = targetParagraph.createRun();
  24. //                            newRun.setText(paragraph.getText());
  25. //                            newRun.addBreak();
  26. //                        }
  27.                         // 遍历嵌入文档中的段落并复制格式和内容
  28.                         for (XWPFParagraph paragraph : embedDoc.getParagraphs()) {
  29.                             // 创建一个新的段落
  30.                             XWPFParagraph newParagraph = mainDoc.insertNewParagraph(targetParagraph.getCTP().newCursor());
  31. //                            XWPFParagraph newParagraph = mainDoc.createParagraph();
  32.                             copyParagraphFormatting(paragraph, newParagraph);
  33.                             // 复制每个Run的内容和格式
  34.                             for (XWPFRun run : paragraph.getRuns()) {
  35.                                 XWPFRun newRun = newParagraph.createRun();
  36.                                 copyRunFormatting(run, newRun);
  37.                                 newRun.setText(run.getText(0));
  38.                             }
  39.                             newParagraph.setSpacingAfter(200); // 添加段落之间的间距
  40.                         }
  41.                     }
  42.                     // 将合并后的文档保存到输出文件
  43.                     try (FileOutputStream out = new FileOutputStream(outputDocPath)) {
  44.                         mainDoc.write(out);
  45.                     }
  46.                 }
  47.             }
  48.             System.out.println("Document saved successfully.");
  49.         }
  50.     // 复制段落的格式
  51.     private static void copyParagraphFormatting(XWPFParagraph srcParagraph, XWPFParagraph destParagraph) {
  52.         // 将源段落的对齐方式设置到目标段落
  53.         destParagraph.setAlignment(srcParagraph.getAlignment());
  54.         // 将源段落的垂直对齐方式设置到目标段落
  55.         destParagraph.setVerticalAlignment(srcParagraph.getVerticalAlignment());
  56.         // 设置目标段落在源段落之后的间距(段落间距)
  57.         destParagraph.setSpacingAfter(srcParagraph.getSpacingAfter());
  58.         // 设置目标段落在源段落之前的间距
  59.         destParagraph.setSpacingBefore(srcParagraph.getSpacingBefore());
  60.         // 设置目标段落的行间距(段落内部行与行之间的距离)
  61.         destParagraph.setSpacingBetween(srcParagraph.getSpacingBetween());
  62. //        // 设置目标段落的首行缩进量
  63.         destParagraph.setIndentationFirstLine(srcParagraph.getIndentationFirstLine());
  64. //        // 设置目标段落的悬挂缩进量
  65. //        destParagraph.setIndentationHanging(srcParagraph.getIndentationHanging());
  66.         // 设置目标段落的左边缩进量
  67.         destParagraph.setIndentationLeft(srcParagraph.getIndentationLeft());
  68.         // 设置目标段落的右边缩进量
  69.         destParagraph.setIndentationRight(srcParagraph.getIndentationRight());
  70.         // 将多行倍距调整为1
  71.         destParagraph.setSpacingBetween(1.0);
  72.         if (srcParagraph.getNumFmt() != null){
  73.             String numLevelText = srcParagraph.getNumLevelText();
  74.             BigInteger numID = srcParagraph.getNumID();
  75.             BigInteger numIlvl = srcParagraph.getNumIlvl();
  76.             destParagraph.setNumID(numID);
  77.             destParagraph.setNumILvl(numIlvl);
  78.             // 创建新的运行并设置编号文本
  79.             XWPFRun run = destParagraph.createRun();
  80.             // 将编号文本设置为运行的内容
  81.             run.setText(numLevelText);
  82.         }
  83.     }
  84.     // 复制Run的格式
  85.     private static void copyRunFormatting(XWPFRun srcRun, XWPFRun destRun) {
  86.         // 设置目标文本是否为粗体,与源文本相同
  87.         destRun.setBold(srcRun.isBold());
  88.         // 设置目标文本是否为斜体,与源文本相同
  89.         destRun.setItalic(srcRun.isItalic());
  90.         // 设置目标文本的下划线样式,与源文本相同
  91.         destRun.setUnderline(srcRun.getUnderline());
  92.         // 设置目标文本是否为删除线,与源文本相同
  93.         destRun.setStrike(srcRun.isStrike());
  94.         // 设置目标文本的字体类型,与源文本相同
  95.         destRun.setFontFamily(srcRun.getFontFamily());
  96.         // 设置目标文本的字体大小,与源文本相同
  97.         destRun.setFontSize(srcRun.getFontSize());
  98.         // 设置目标文本的字体颜色,与源文本相同
  99.         destRun.setColor(srcRun.getColor());
  100.         // 设置目标文本的下标状态,与源文本相同
  101.         destRun.setSubscript(srcRun.getSubscript());
  102.         // 设置目标文本的垂直位置,与源文本相同
  103.         destRun.setTextPosition(srcRun.getTextPosition());
  104.         if (srcRun.getCTR() != null) {
  105.             if (srcRun.getCTR().isSetRPr()) {
  106.                 if (srcRun.getCTR().getRPr().isSetB()) {
  107.                     destRun.setBold(srcRun.isBold());
  108.                 }
  109.                 if (srcRun.getCTR().getRPr().isSetI()) {
  110.                     destRun.setItalic(srcRun.isItalic());
  111.                 }
  112.                 if (srcRun.getCTR().getRPr().isSetStrike()) {
  113.                     destRun.setStrike(srcRun.isStrike());
  114.                 }
  115.                 if (srcRun.getCTR().getRPr().isSetColor()) {
  116.                     destRun.setColor(srcRun.getColor());
  117.                 }
  118.                 if (srcRun.getCTR().getRPr().isSetSz()) {
  119.                     destRun.setFontSize(srcRun.getFontSize());
  120.                 }
  121.                 if (srcRun.getCTR().getRPr().isSetRFonts()) {
  122.                     destRun.setFontFamily(srcRun.getFontFamily());
  123.                 }
  124.             }
  125.         }
  126.     }
复制代码
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表