How to replace placeholders in header of docx in java using poi 3.8

本秂侑毒 提交于 2019-11-27 07:12:21

问题


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


回答1:


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);
        }
    }
}



回答2:


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. :)




回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/19393505/how-to-replace-placeholders-in-header-of-docx-in-java-using-poi-3-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!