What I have implemented is a find replace feature(from here);
In my template docx file I am using "id like texts", __heading1__, __subjectname__, Then replacing with them using code below. For tables @axel-richters solution may be suitable.
private void findReplace(String a, String b, CustomXWPFDocument document){
for (XWPFParagraph p : document.getParagraphs()) {
List<XWPFRun> runs = p.getRuns();
if (runs != null) {
for (XWPFRun r : runs) {
String text = r.getText(0);
if (text != null && text.contains(a)) {
text = text.replace(a, b);
r.setText(text, 0);
}
}
}
}
}
ID
are you talking about? How will you set thoseID
in Word? AWord
table may haveTitle
in Table Properties - Alt Text. Is this meant? – Axel RichterWord
document except their occurrence from top to down. So in general case iterate over allIBodyElement
s, determine whatBodyElementType
you got and then do with that element what you need to do. In special cases the requirement must be clear. For example you could replace place holder texts or filling form fields or looking for headings/bookmarks/captions/alt-texts/... to determine elements. But for this the requirement must also be clear for the author of theWord
document since he must write those things into the document. – Axel Richter