【问题标题】:How to append new list item to a list如何将新列表项附加到列表
【发布时间】:2011-12-09 14:51:31
【问题描述】:

我想找到一个列表段落(以 a. 开头),并将另一个列表项附加到此列表(它取决于第一个列表元素的文本)。 我尝试了许多创建新段落的方法,但我所取得的只是创建了新的列表元素,但是 org.docx4j.wml.Text 对象被附加到段落中,新段落被附加了。新段落文本为空。如何创建新的列表元素并将其附加到正确的元素?

  • 一个。列出元素 1 |测试| //|测试|应附加到 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);

    }...}

【问题讨论】:

    标签: docx docx4j


    【解决方案1】:

    我的解决方案,只需将索引附加到正文。我正在为 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;
    }
    

    【讨论】:

      猜你喜欢
      • 2011-10-12
      • 2021-08-01
      • 2021-07-11
      • 2017-09-20
      • 1970-01-01
      • 2013-12-24
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      相关资源
      最近更新 更多