如何使用 C# 为 docx 中的每个页脚自动创建

问题描述

我正在创建一个 docx,我需要一个带有时间戳和页面的页脚(对于文档的每一页)(采用这种格式:[总页数] 的 [当前页]。 我在网上阅读的每个帖子/代码似乎都表明我必须为每个页面手动创建页脚并手动编写页面信息:这是真的吗?难道不能以这样一种方式编写代码,即在主文档中附加一个页脚,docx 会自动创建自己的每个页脚并用正确的信息填充它吗?

有什么建议吗?

[更新] 我正在使用 DocumentFormat.OpenXml.Wordprocessing 并且,按照 Heinzi 的示例,我创建了一个新文档,添加一个页脚,然后我通过 Open XML SDK 2.5 分析了 docx:页脚是通过这种方法创建的:

private void GeneratePartContent(FooterPart part)
    {
        Footer footer1 = new Footer(){ MCAttributes = new MarkupCompatibilityAttributes(){ Ignorable = "w14 w15 w16se w16cid w16 w16cex w16sdtdh wp14" }  };
        footer1.AddNamespaceDeclaration("wpc","http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas");
        footer1.AddNamespaceDeclaration("cx","http://schemas.microsoft.com/office/drawing/2014/chartex");
        footer1.AddNamespaceDeclaration("cx1","http://schemas.microsoft.com/office/drawing/2015/9/8/chartex");
        footer1.AddNamespaceDeclaration("cx2","http://schemas.microsoft.com/office/drawing/2015/10/21/chartex");
        footer1.AddNamespaceDeclaration("cx3","http://schemas.microsoft.com/office/drawing/2016/5/9/chartex");
        footer1.AddNamespaceDeclaration("cx4","http://schemas.microsoft.com/office/drawing/2016/5/10/chartex");
        footer1.AddNamespaceDeclaration("cx5","http://schemas.microsoft.com/office/drawing/2016/5/11/chartex");
        footer1.AddNamespaceDeclaration("cx6","http://schemas.microsoft.com/office/drawing/2016/5/12/chartex");
        footer1.AddNamespaceDeclaration("cx7","http://schemas.microsoft.com/office/drawing/2016/5/13/chartex");
        footer1.AddNamespaceDeclaration("cx8","http://schemas.microsoft.com/office/drawing/2016/5/14/chartex");
        footer1.AddNamespaceDeclaration("mc","http://schemas.openxmlformats.org/markup-compatibility/2006");
        footer1.AddNamespaceDeclaration("aink","http://schemas.microsoft.com/office/drawing/2016/ink");
        footer1.AddNamespaceDeclaration("am3d","http://schemas.microsoft.com/office/drawing/2017/model3d");
        footer1.AddNamespaceDeclaration("o","urn:schemas-microsoft-com:office:office");
        footer1.AddNamespaceDeclaration("r","http://schemas.openxmlformats.org/officeDocument/2006/relationships");
        footer1.AddNamespaceDeclaration("m","http://schemas.openxmlformats.org/officeDocument/2006/math");
        footer1.AddNamespaceDeclaration("v","urn:schemas-microsoft-com:vml");
        footer1.AddNamespaceDeclaration("wp14","http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing");
        footer1.AddNamespaceDeclaration("wp","http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
        footer1.AddNamespaceDeclaration("w10","urn:schemas-microsoft-com:office:word");
        footer1.AddNamespaceDeclaration("w","http://schemas.openxmlformats.org/wordprocessingml/2006/main");
        footer1.AddNamespaceDeclaration("w14","http://schemas.microsoft.com/office/word/2010/wordml");
        footer1.AddNamespaceDeclaration("w15","http://schemas.microsoft.com/office/word/2012/wordml");
        footer1.AddNamespaceDeclaration("w16cex","http://schemas.microsoft.com/office/word/2018/wordml/cex");
        footer1.AddNamespaceDeclaration("w16cid","http://schemas.microsoft.com/office/word/2016/wordml/cid");
        footer1.AddNamespaceDeclaration("w16","http://schemas.microsoft.com/office/word/2018/wordml");
        footer1.AddNamespaceDeclaration("w16sdtdh","http://schemas.microsoft.com/office/word/2020/wordml/sdtdatahash");
        footer1.AddNamespaceDeclaration("w16se","http://schemas.microsoft.com/office/word/2015/wordml/symex");
        footer1.AddNamespaceDeclaration("wpg","http://schemas.microsoft.com/office/word/2010/wordprocessingGroup");
        footer1.AddNamespaceDeclaration("wpi","http://schemas.microsoft.com/office/word/2010/wordprocessingInk");
        footer1.AddNamespaceDeclaration("wne","http://schemas.microsoft.com/office/word/2006/wordml");
        footer1.AddNamespaceDeclaration("wps","http://schemas.microsoft.com/office/word/2010/wordprocessingShape");

        Paragraph paragraph1 = new Paragraph(){ RsidParagraphAddition = "007C3796",RsidRunAdditionDefault = "007C3796",ParagraphId = "5404E3E7",TextId = "621D9DAC" };

        ParagraPHProperties paragraPHProperties1 = new ParagraPHProperties();
        ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId(){ Val = "Footer" };

        paragraPHProperties1.Append(paragraphStyleId1);

        Run run1 = new Run();
        FieldChar fieldChar1 = new FieldChar(){ FieldCharType = FieldCharValues.Begin };

        run1.Append(fieldChar1);

        Run run2 = new Run();
        FieldCode fieldCode1 = new FieldCode(){ Space = SpaceProcessingModeValues.Preserve };
        fieldCode1.Text = " PAGE   \\* MERGEFORMAT ";

        run2.Append(fieldCode1);

        Run run3 = new Run();
        FieldChar fieldChar2 = new FieldChar(){ FieldCharType = FieldCharValues.Separate };

        run3.Append(fieldChar2);

        Run run4 = new Run();

        RunProperties runProperties1 = new RunProperties();
        noproof noproof1 = new noproof();

        runProperties1.Append(noproof1);
        Text text1 = new Text();
        text1.Text = "1";

        run4.Append(runProperties1);
        run4.Append(text1);

        Run run5 = new Run();
        FieldChar fieldChar3 = new FieldChar(){ FieldCharType = FieldCharValues.End };

        run5.Append(fieldChar3);

        paragraph1.Append(paragraPHProperties1);
        paragraph1.Append(run1);
        paragraph1.Append(run2);
        paragraph1.Append(run3);
        paragraph1.Append(run4);
        paragraph1.Append(run5);

        footer1.Append(paragraph1);

        part.Footer = footer1;
    }

我复制了这个方法调用

FooterPart footerPart1 = mainDocumentPart1.AddNewPart<FooterPart>("rId6");
        GenerateFooterPart1Content(footerPart1);

但我没有页脚,有什么建议吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)