cricktard
cricktard

Reputation: 1

How can I replace a string in the footer of a XWPFDocument using Apache POI

I am using Apache POI to replace text in word templates. I can search and replace in paragraphs and tables, but I cannot find how to replace a string in the footer of an XWPFDocument.

Upvotes: 0

Views: 2142

Answers (1)

magnusson
magnusson

Reputation: 153

Get the footers and then get their paragraphs and replace the text just as Gagravarr wrote. I use the code below to replace text in footers in the document.

private void replaceTextInFooter(XWPFDocument doc, String findText, String replaceText) {
    for (XWPFFooter footer : doc.getFooterList()) {
        for (XWPFParagraph paragraph : footer.getParagraphs()) {
            for (XWPFRun run : paragraph.getRuns()) {
                String text = run.text();
                if (text.contains(findText)) {
                    run.setText(replaceText, 0);
                }
            }
        }
    }
}

Upvotes: 3

Related Questions