【问题标题】:Docx4j Footer not showing in MS-Word 2016MS-Word 2016 中未显示 Docx4j 页脚
【发布时间】:2019-01-15 09:28:32
【问题描述】:

我正在使用 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);
    }

请帮我找出问题所在。

【问题讨论】:

标签: java ms-word docx4j


【解决方案1】:

您的 docx 开头为:

<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(这没什么意义)移动到末尾的那个。

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

【讨论】:

  • 感谢@JasonPlutext,它现在正在工作...您能否提供一些我可以调试损坏部分的步骤..仅供参考,我正在导入 html 文件以创建 docx..
  • 围绕 21 桌你有:

    (空 tc、不正确的嵌套表和放错位置的 tblPr 元素)。从 之前的 删除到

    (即删除包含空 tc 的 tr,然后是 2 个嵌套表),它可以工作
  • ps,如果这个结构是由 Import-XHTML 在你的 XHTML 上运行产生的,请发给我。我想处理得更好。
  • 嗨@JasonPlutext ..是的输出是由docx4j自动生成的..但是HTML表格标签和XHTML中的其他标签有一些误导..所以我将它固定在xhtml级别,现在它是产生完美的输出..感谢您的支持..
猜你喜欢
  • 1970-01-01
  • 2012-08-11
  • 2015-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多