How to replace placeholders in header of docx in j

2020-02-06 10:39发布

I'm tying to replace tokens in the header of docx file.I have handled the token replacement in paragraphs and tables but its not picking the header data. Im using apache poi 3.8 and coding in java using eclipse ID. Thanx

4条回答
混吃等死
2楼-- · 2020-02-06 10:59

This methods will replace all selected text, in tables, headers and paragraph, in the entire document.

public XWPFDocument replacePOI(XWPFDocument doc, String placeHolder, String replaceText){
    // REPLACE ALL HEADERS
    for (XWPFHeader header : doc.getHeaderList()) 
        replaceAllBodyElements(header.getBodyElements(), placeHolder, replaceText);
    // REPLACE BODY
    replaceAllBodyElements(doc.getBodyElements(), placeHolder, replaceText);
    return doc;
}

private void replaceAllBodyElements(List<IBodyElement> bodyElements, String placeHolder, String replaceText){
    for (IBodyElement bodyElement : bodyElements) {
        if (bodyElement.getElementType().compareTo(BodyElementType.PARAGRAPH) == 0)
            replaceParagraph((XWPFParagraph) bodyElement, placeHolder, replaceText);
        if (bodyElement.getElementType().compareTo(BodyElementType.TABLE) == 0)
            replaceTable((XWPFTable) bodyElement, placeHolder, replaceText);
    }
}

private void replaceTable(XWPFTable table, String placeHolder, String replaceText) {
    for (XWPFTableRow row : table.getRows()) {
        for (XWPFTableCell cell : row.getTableCells()) {
            for (IBodyElement bodyElement : cell.getBodyElements()) {
                if (bodyElement.getElementType().compareTo(BodyElementType.PARAGRAPH) == 0) {
                    replaceParagraph((XWPFParagraph) bodyElement, placeHolder, replaceText);
                }
                if (bodyElement.getElementType().compareTo(BodyElementType.TABLE) == 0) {
                    replaceTable((XWPFTable) bodyElement, placeHolder, replaceText);
                }
            }
        }
    }  
}

private void replaceParagraph(XWPFParagraph paragraph, String placeHolder, String replaceText) {
    for (XWPFRun r : paragraph.getRuns()) {
        String text = r.getText(r.getTextPosition());
        if (text != null && text.contains(placeHolder)) {
            text = text.replace(placeHolder, replaceText);
            r.setText(text, 0);
        }
    }
}
查看更多
聊天终结者
3楼-- · 2020-02-06 11:00

Code referred by (edited Sep 30 '16 at 1:19, Julio Villane) works for only headers across the document. Thanks for the code. To Replace in Footer, Same code has to called inside iteration of FooterList. To Replace across the document other than Header and Footer, You have to call the replaceParagraph(), replaceTable() again,to replace text across the document, other than Header and Footer.

查看更多
放我归山
4楼-- · 2020-02-06 11:06

I don't know if you have got the solution for this question. But, I've tried to replace tokens in document header and it worked for me.

public XWPFDocument setHeader(XWPFDocument document, String token, String textToReplace){
    XWPFHeaderFooterPolicy policy= document.getHeaderFooterPolicy();
    XWPFHeader header = policy.getHeader(0);
    replaceInParagraphs(header.getParagraphs(), token, textToReplace);
    return document;
}

private void replaceInParagraphs(List<XWPFParagraph> paragraphs, String placeHolder, String replaceText){
    for (XWPFParagraph xwpfParagraph : paragraphs) {
        List<XWPFRun> runs = xwpfParagraph.getRuns();
        for (XWPFRun run : runs) {
            String runText = run.getText(run.getTextPosition());

            if(placeHolder !="" && !placeHolder.isEmpty()){
                if(runText != null &&
                        Pattern.compile(placeHolder, Pattern.CASE_INSENSITIVE).matcher(runText).find()){
                    runText = replaceText;
                }
            }
            run.setText(runText, 0);
        }
    }
}

Hope this helps. :)

查看更多
Anthone
5楼-- · 2020-02-06 11:15

You can leverage "content controls" in MS Word. Then you can access the content controls using the openxml library. Content controls act as placeholders/input sections in Word documents. I'm not a Java guy, but just letting you know this is another method

查看更多
登录 后发表回答