Java 如何将新的footer1.xml绑定到word文档?

Java 如何将新的footer1.xml绑定到word文档?,java,docx4j,Java,Docx4j,我有一个已经存在的word文档,它的页脚带有文本。我找到了要替换的关系部分,并且有了要替换的页脚部分 据我所知,我应该能够只使用.addTargetPart(Part foo)覆盖旧的footer1.xml,使用我创建的新footer,但它似乎什么也没做 FooterPart footerPart = ((FooterPart) relationshipPart.getPart(relationship)); footerPart.setJaxbElement((Ftr) footerObj);

我有一个已经存在的word文档,它的页脚带有文本。我找到了要替换的关系部分,并且有了要替换的页脚部分

据我所知,我应该能够只使用.addTargetPart(Part foo)覆盖旧的footer1.xml,使用我创建的新footer,但它似乎什么也没做

FooterPart footerPart = ((FooterPart) relationshipPart.getPart(relationship));
footerPart.setJaxbElement((Ftr) footerObj);
wordMLPackage.getMainDocumentPart().addTargetPart(footerPart);

替换现有零件的内容即可:

FooterPart footerPart = ((FooterPart) relationshipPart.getPart(relationship));
footerPart.setJaxbElement((Ftr) footerObj);
无需再次添加零件

但您是正确的,如果存在\u NAME\u,则按您的方式调用addTargetPart将覆盖\u:

public Relationship addTargetPart(Part targetpart) throws InvalidFormatException {
    return this.addTargetPart(targetpart, AddPartBehaviour.OVERWRITE_IF_NAME_EXISTS, null);
}
因此,我怀疑您正在替换的页脚不是您在docx中看到的页脚。它可能有第一页页脚?把它解开看看。或者运行HeaderFooterList示例代码