Search code examples
docxdocx4j

How to append new list item to a list


I would like to find a list paragraph (starting with a. ), and append another list item to this list (it depend on the text of first list element). I have tried many ways of creating new paragraph, but all what I achieved is that new list elements are created, but org.docx4j.wml.Text objects are appended to paragraph the new paragraph was appended. The new paragraph text is empty. How can be new list element created and appended to the right element?

  • a. list element 1 |test| //|test| should be appended to b.
  • b. //new items are created, but there is no text
  • 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);

    }...}

Solution

  • My solution, just append to the body with incremented index. I' am creating deep copy to preserwe style.

        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;
    }