java - Docx4j 页脚未显示在 MS-Word 2016 中

标签 java ms-word docx4j

我正在使用 Java 中的 DOCx4j 库创建一个 docx 文件。我创建的页脚在 Libra Office 中完美显示,但在 MS-Word 2016 中未显示。

页脚代码:

    public static Relationship createFooterPageNumPart(
                WordprocessingMLPackage wordprocessingMLPackage) throws Exception {

           FooterPart footerPart = new FooterPart();
              MainDocumentPart t = wordprocessingMLPackage.getMainDocumentPart();

            footerPart.setPackage(wordprocessingMLPackage);
        //  footerPart.setJaxbElement(createFooterWithPageNr());

            footerPart.setJaxbElement(createFooterWithPageNr(wordprocessingMLPackage,footerPart));
            return t.addTargetPart(footerPart);
        }




       public static Ftr createFooterWithPageNr(WordprocessingMLPackage wordprocessingMLPackage, Part sourcePart) throws Exception {
            Ftr ftr = objectFactory.createFtr();
            P paragraph = objectFactory.createP();
            RPr fontRPr = getRPr(objectFactory, "Frutiger LT Arabic 45 Light", "000000", "20", STHint.EAST_ASIA,
                    false, false, false, false);
            R run = objectFactory.createR();
            run.setRPr(fontRPr);
            paragraph.getContent().add(run);

            setParagraphAlign(objectFactory, paragraph, JcEnumeration.RIGHT);
            ftr.getContent().add(paragraph);
            return ftr;
        }

 public static void createFooterReference(
            WordprocessingMLPackage wordprocessingMLPackage,
            Relationship relationship)
            throws InvalidFormatException {

        List<SectionWrapper> sections = wordprocessingMLPackage
                .getDocumentModel().getSections();
        SectPr sectPr = sections.get(sections.size() - 1).getSectPr();
        // There is always a section wrapper, but it might not contain a sectPr
        if (sectPr == null) {
            sectPr = objectFactory.createSectPr();
             wordprocessingMLPackage.getMainDocumentPart().addObject(sectPr);
            sections.get(sections.size() - 1).setSectPr(sectPr);
        }
        FooterReference footerReference = objectFactory.createFooterReference();
        footerReference.setId(relationship.getId());
        footerReference.setType(HdrFtrRef.DEFAULT);
        sectPr.getEGHdrFtrReferences().add(footerReference);
    }

请帮我找出问题所在。

最佳答案

您的文档开头为:

<w:body>
    <w:sectPr>
        <w:headerReference w:type="default" r:id="rId4"/>
    </w:sectPr>

并以以下内容结尾:

    <w:sectPr>
        <w:footerReference w:type="default" r:id="rId5"/>

尝试将 w:headerReference 从开头的 sectPr(这没什么意义)移动到末尾的 sectPr。

但是根据 Word 2016,您发布的 docx 已损坏,因此它还有其他问题需要解决。

关于java - Docx4j 页脚未显示在 MS-Word 2016 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54195998/

相关文章:

java - websockets api中session.getBasicRemote().sendText()和session.getAsyncRemote().sendText()有什么区别

ms-word - 如何在 Microsoft Word 2010 大纲 View 中禁用换行

java - 为 DocX4J 生成的 Excel 行添加背景颜色

c# - Interop.Word 不适用于阿拉伯语文本

ruby-on-rails - ruby rails : In there a way to convert word to html?

java - 如何以编程方式在 Java 中将 doc、docx 文件转换为 pdf

java - 如何使用docx4j读取/打印excel文件的内容?

java - 在 Java 中是否有 'this' 的静态版本

java - 何时从 MDC 中删除

java - Spring Boot Controller 不重定向