我正在尝试使用Apache POI将Word文档转换为HTML。我有一个Word文档,在段落后有一条水平线。水平线的OOXML如下所示:

          <w:p w14:paraId="721E1052" w14:textId="05637367" w:rsidR="002D1248" w:rsidRPr="00BB3E82" w:rsidRDefault="00B3113F" w:rsidP="00797596">
            <w:pPr>
              <w:rPr>
                <w:rFonts w:eastAsia="Times New Roman" w:cs="Courier New"/>
                <w:snapToGrid w:val="0"/>
                <w:color w:val="000000"/>
                <w:lang w:eastAsia="fi-FI"/>
              </w:rPr>
            </w:pPr>
            <w:r>
              <w:rPr>
                <w:rFonts w:eastAsia="Times New Roman" w:cs="Courier New"/>
                <w:snapToGrid w:val="0"/>
                <w:color w:val="000000"/>
                <w:lang w:eastAsia="fi-FI"/>
              </w:rPr>
              <w:pict w14:anchorId="534EEFD0">
                <v:rect id="_x0000_i1025" style="width:0;height:1.5pt" o:hralign="center" o:hrstd="t" o:hr="t" fillcolor="#a0a0a0" stroked="f"/>
              </w:pict>
            </w:r>
          </w:p>


与此水平线相对应,我想在HTML中添加HR标签。但是,我无法在“ pict”中检索“ rect”元素。到目前为止,这是我尝试过的:

List<org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPicture> pics = run.getCTR().getPictList();
        if(pics!=null) {
            log.debug("Size of pics = "+pics.size());
            for (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPicture pic : pics) {
                Node picNode = pic.getDomNode();
                CTGroup ctGroup = CTGroup.Factory.parse(picNode);
                if(ctGroup!=null) {
                    log.debug("Size of rects= "+ctGroup.getRectList().size());
                }
            }



上面的代码给出:
图片大小= 1
矩形的大小= 0
我不知道为什么会这样。在理解如何检索“ rect”对象方面的任何帮助将不胜感激。谢谢。

最佳答案

您不能从com.microsoft.schemas.vml.CTGroup dom节点解析org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPicture元素。

但是所有ooxml-schemas对象都从org.apache.xmlbeans.XmlObject继承。因此,他们可以使用XmlObject.selectChildren通过元素URI和元素本地名称选择子代。我们需要知道的是com.microsoft.schemas.vml.*的名称空间URI是“ urn:schemas-microsoft-com:vml”。

例:

import java.io.FileInputStream;

import org.apache.poi.xwpf.usermodel.*;

import org.apache.xmlbeans.XmlObject;

import java.util.List;

public class WordReadCTPictureContent {

 public static void main(String[] args) throws Exception {

  String inFilePath = "./HRBetweenParagraphs.docx";

  XWPFDocument document = new XWPFDocument(new FileInputStream(inFilePath));

  for (XWPFParagraph paragraph : document.getParagraphs()) {
   for (XWPFRun run : paragraph.getRuns()) {

    List<org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPicture> pics = run.getCTR().getPictList();
    System.out.println("Size of pics = " + pics.size());
    for (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPicture pic : pics) {
     //select com.microsoft.schemas.vml.CTRect children by elementUri and elementLocalName
     XmlObject[] rects = pic.selectChildren("urn:schemas-microsoft-com:vml", "rect");
     System.out.println("Count of rects = " + rects.length);
     for (XmlObject obj : rects) {
      com.microsoft.schemas.vml.CTRect rect = (com.microsoft.schemas.vml.CTRect)obj;
      //now we can work with found com.microsoft.schemas.vml.CTRect
      System.out.println("Id of found rect = " + rect.getId());
     }

    }

   }
  }

  document.close();
 }

}

10-05 21:12