为什么 openxml 页脚没有发布到文档

Why is openxml footer not posting to document

我遵循了几个示例,但我根本无法使用 openxml 将页脚打印到文字处理文档。我敢打赌我在这里遗漏了一些微不足道的东西,但我不能 see/find 它。

            MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();
            mainPart.Document = new Document();
            Body docBody = mainPart.Document.AppendChild(new Body());
            FooterPart footerPart = mainPart.AddNewPart<FooterPart>();
            string footerId = mainPart.GetIdOfPart(footerPart);

            SectionProperties sectionProperties = new SectionProperties();
            FooterReference footerReference = new FooterReference() { Id = footerId, Type = HeaderFooterValues.Default };sectionProperties.Append(footerReference);
            PageMargin pageMargin = new PageMargin() { Top = 1440, Right = (UInt32Value)1440U, Bottom = 1440, Left = (UInt32Value)1440U, Header = (UInt32Value)720U, Footer = (UInt32Value)1080U, Gutter = (UInt32Value)0U };
            sectionProperties.Append(pageMargin);

            Footer footer = new Footer();
            Paragraph paragraph50 = new Paragraph();
            ParagraphProperties footerProperties = new ParagraphProperties();

            footerProperties.Append(new ParagraphStyleId() { Val = "Footer" });
            footerProperties.Append(new Justification() { Val = JustificationValues.Both });
            footerProperties.Append(sectionProperties);
            paragraph50.Append(footerProperties);
            footer.Append(paragraph50);
            footerPart.Footer = footer;

            RunProperties frp = new RunProperties();
            frp.Color = new Color() { Val = "C0C0C0" };
            frp.FontSize = new FontSize() { Val = "15" };

            Run frun1 = new Run();
            frun1.Append(frp);
            frun1.Append(new Text("UNIFORM INSTRUMENT"));

            paragraph50.Append(frun1);

眼前的问题是您将节属性附加到 footerPart 而它们应该附加到 docBody

即如果你改变

        footerProperties.Append(sectionProperties);

        docBody.Append(sectionProperties);

然后您应该能够打开生成的 .docx 并看到页脚。

就个人而言,我发现 Office Open SDK 生产力工具对于理解如何构建 Word 文档非常有用 - WIndows 只有我认为,但它是可用的下载之一 here