2011-10-17 70 views
1

我想查找列表段落(以。开头),并将另一个列表项添加到此列表(取决于第一个列表元素的文本)。 我已经尝试过很多创建新段落的方法,但是我所取得的所有成果都是创建了新的列表元素,但org.docx4j.wml.Text对象被追加到段落的后面,新段落被追加。新的段落文本是空的。如何可以创建新的列表元素并将其添加到正确的元素?如何将新列表项添加到列表中

  • a。列表元素1 | test | // |考试|应附加到b。
  • b。 //创建新项目,但没有文字
  • c。
//traverse through a document 
    public List<Object> apply(Object obj) { 
     if (obj instanceof org.docx4j.wml.P) { 
      if (p.getPPr() != null) { 
      if (p.getPPr().getPStyle() != null) { 
       if ((p.getPPr().getPStyle().getVal().equals("Akapitzlist"))) { 
        //there is a list paragraph 
         ObjectFactory factory = Context.getWmlObjectFactory(); 
         Object deepCopy = XmlUtils.deepCopy(obj); 
        //Create the paragraph 
        org.docx4j.wml.P para = factory.createP(); 

        // Create the text element 
        org.docx4j.wml.Text t = factory.createText(); 
        t.setValue("|test|"); 

        // Create the run 
        org.docx4j.wml.R run = factory.createR(); 
        run.getContent().add(t); 
        para.getContent().add(run); 
        //add new paragraph to the document 
        ((org.docx4j.wml.P) obj).getContent().add(para); 

    }...} 

回答

0

我的解决办法,只是追加到身体与增量索引。我正在为preserwe风格创建深层副本。

public List<Object> apply(Object obj) { 


    Object deepCopy = null; 



    if (obj instanceof org.docx4j.wml.P) { 

     org.docx4j.wml.P p = (org.docx4j.wml.P) obj; 


     if (p.getPPr() != null) { 
      if (p.getPPr().getPStyle() != null) { 
       if ((p.getPPr().getPStyle().getVal().equals("Akapitzlist")) && (akapListCounter < 10)) { 

        if (((org.docx4j.wml.P) obj).getPPr().getPStyle() != null) { 
         if ((((org.docx4j.wml.P) obj).getPPr().getPStyle().getVal().equals("Akapitzlist"))) { 
          deepCopy = XmlUtils.deepCopy(obj); 
          akapListCounter++; 
          int indexOf = wmlDocumentEl.getBody().getContent().indexOf(obj); 


          List<Object> content = ((org.docx4j.wml.P) deepCopy).getContent(); 
          for (Object el : content) { 
           System.out.println("class1:" + el.getClass().toString()); 
           if (el instanceof org.docx4j.wml.R) { 
            List<Object> subc = ((org.docx4j.wml.R) el).getContent(); 
            for (Object r : subc) { 
             ((javax.xml.bind.JAXBElement) r).setValue("tetetete"); 
            } 
           } 

          }// end for 


          wmlDocumentEl.getBody().getContent().add(indexOf + 1, deepCopy); 


         } 
        }//end get style 

       } 
      } 
     } else {} 


    } 

    return null; 
} 
相关问题