Page 1 of 1

How to replace existing list in word

PostPosted: Mon Oct 14, 2013 10:28 pm
by aarif
i have a list with a place holder value and i am replacing that value with my dynamic value , but the new contents are not adding to placeholder , rather on last page.
Code: Select all
List<Object> lstTexts = getAllElementFromObject(wordTemplate.getMainDocumentPart(), Text.class);
       
        for (Object text : lstTexts) {
            Text textElement = (Text) text;
            if (textElement.getValue().equalsIgnoreCase("p" + intPageNumber + "l1")) {
                textElement.setValue("");
                for (Iterator iterator = lstOEQB.iterator(); iterator.hasNext();) {
                    OpenEndedQuestionBean oeqb = (OpenEndedQuestionBean) iterator.next();
                    wordTemplate.getMainDocumentPart().addObject(createNumberedParagraph(1, 0, "List", wmlObjectFactory));
                }
            }
        }


List is generated here:
Code: Select all
    private P createNumberedParagraph(long numId, long ilvl, String paragraphText, ObjectFactory wmlObjectFactory) {
       
        P p = wmlObjectFactory.createP();
       
        Text t = wmlObjectFactory.createText();
        t.setValue(paragraphText);
       
        R run = wmlObjectFactory.createR();
        run.getRunContent().add(t);
       
        p.getParagraphContent().add(run);
       
        PPr ppr = wmlObjectFactory.createPPr();
        p.setPPr(ppr);

        // Create and add <w:numPr>
        PPrBase.NumPr numPr = wmlObjectFactory.createPPrBaseNumPr();
        ppr.setNumPr(numPr);

        // The <w:ilvl> element
        PPrBase.NumPr.Ilvl ilvlElement = wmlObjectFactory.createPPrBaseNumPrIlvl();
        numPr.setIlvl(ilvlElement);
        ilvlElement.setVal(BigInteger.valueOf(ilvl));

        // The <w:numId> element
        PPrBase.NumPr.NumId numIdElement = wmlObjectFactory.createPPrBaseNumPrNumId();
        numPr.setNumId(numIdElement);
        numIdElement.setVal(BigInteger.valueOf(numId));
        return p;
       
    }

Re: How to replace existing list in word

PostPosted: Mon Oct 14, 2013 10:41 pm
by jason
wordTemplate.getMainDocumentPart().addObject appends to the end of the content list.

See also http://stackoverflow.com/questions/1923 ... -docx-java

Why do you have textElement.setValue("")? You could set your value using that?

Re: How to replace existing list in word

PostPosted: Tue Oct 15, 2013 9:32 pm
by aarif
I want that list to be replaced at that same page and not at the end , how could i accomplish this??

Code: Select all
NumberingDefinitionsPart ndp = new NumberingDefinitionsPart();           
            ndp.setJaxbElement((Numbering) XmlUtils.unmarshalString(openXML));


The above code creates list and appends at last page., but i want to get this list at my already defined list by replacing that list(Placeholder). Is there any method so that i can get that NumberingDefinitionsPart part and add list there.