Page 1 of 1

Replace text with bullet list

PostPosted: Wed Nov 16, 2011 5:12 pm
by qbl
Hi, this is my first time posting here. Anyway, I'd like to thank the people who made this Docx4j available, you guys are great.

Now to the main question, I have a scenario where I have to find for a certain place holder and then replace it with bullet list. I have searched and found several threads in this forum that very useful. As a result, I have been able to replace text with text and create my own bullet list. Here is some of the codes I gather so far.

First, the bullet list:
Code: Select all
  WordprocessingMLPackage template = WordprocessingMLPackage.load("Some File.docx");
  NumberingDefinitionsPart ndp = new NumberingDefinitionsPart();
  template.getMainDocumentPart().addTargetPart(ndp);
  ndp.setJaxbElement((Numbering) XmlUtils.unmarshalString(initialNumbering));

  P p = createNumberedParagraph(1, 0, "Entry #1");
  template.getMainDocumentPart().addObject(p);


With the createNumberedParagraph function being:
Code: Select all
  P createNumberedParagraph(long numId, long ilvl, String paragraphText ) {
    ObjectFactory factory = new org.docx4j.wml.ObjectFactory();
    P  p = factory.createP();
   
    org.docx4j.wml.Text  t = factory.createText();
    t.setValue(paragraphText);
   
    org.docx4j.wml.R  run = factory.createR();
    run.getContent().add(t);
   
    p.getContent().add(run);
   
    org.docx4j.wml.PPr ppr = factory.createPPr();
    p.setPPr( ppr );
   
    // Create and add <w:numPr>
    NumPr numPr =  factory.createPPrBaseNumPr();
    ppr.setNumPr(numPr);
   
    // The <w:ilvl> element
    Ilvl ilvlElement = factory.createPPrBaseNumPrIlvl();
    numPr.setIlvl(ilvlElement);
    ilvlElement.setVal(BigInteger.valueOf(ilvl));
   
    // The <w:numId> element
    NumId numIdElement = factory.createPPrBaseNumPrNumId();
    numPr.setNumId(numIdElement);
    numIdElement.setVal(BigInteger.valueOf(numId));
   
    return p;
  }


While the replace text with text code looks like this:
Code: Select all
  WordprocessingMLPackage template = WordprocessingMLPackage.load(new File("Some File.docx"));

  // Replacing place holders with text only values
  List<Object> texts = template.getMainDocumentPart().getJAXBNodesViaXPath(xpath, true);
  for (Object obj : texts) {
    Text text = (Text) ((JAXBElement) obj).getValue();
    text.setValue(replacePH(text, "\\$\\{PlaceHolder1\\}", "Value1"));
    text.setValue(replacePH(text, "\\$\\{PlaceHolder2\\}", "Value2"));
  }

  String replacePH(Text text, String placeHolder, String inputValue) {
    return text.getValue().replaceAll(placeHolder, inputValue);
  }


Is there any way I can modify these codes so it replaces the place holder text with numbered list I created? Any help is highly appreciated.

Thanks in advance.