I have a template which contains the text %%WEBSITE%%. I would like to replace all occurrences of this text with a website.
I've been able to locate the org.docx4j.wml.Text object that contains this text; my attempts of replace the text with a hyperlink have failed.
- Code: Select all
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(templateFile);
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document) documentPart.getJaxbElement();
String xpath = "//w:r[w:t[contains(text(),'%%WEBSITE%%')]]";
System.out.println("XPATH " + xpath);
List<Object> list = documentPart.getJAXBNodesViaXPath(xpath, true);
for (Object obj : list) {
org.docx4j.wml.ObjectFactory factory = new org.docx4j.wml.ObjectFactory();
HyperlinkPlaceHolder placeHolder = new HyperlinkPlaceHolder("%%WEBSITE%%", "http://val", "http://url");
P para = factory.createP();
para.getContent().add(placeHolder.getValue(wordMLPackage)); // getValue() returns a Hyperlink
((R) obj).getParent().getContent().add(para);
}