5

我在 Visual Studio 中使用 OXML 创建一个 word 文档。我不知道它会持续多久,我需要在文档的页脚中添加一个简单的页码。

为了生成页眉和页脚,我使用了这个: https ://msdn.microsoft.com/en-us/library/ee355228(v=office.12).aspx

据我了解,这会在我在文档中写任何内容之前预设默认的页眉/页脚。所以我不太确定是否可以为此添加页码?我真的很感激你的帮助,因为我已经坚持了一整天......

4

2 回答 2

9

SimpleField您可以通过添加 a和Instructionof来添加动态页码"PAGE"。Word 将使用正确的页码自动更新任何此类字段。

为了编写代码,您可以调整GeneratePageFooterPart您提供的链接中的 以包含SimpleField添加Run到的Footer

private static Footer GeneratePageFooterPart(string FooterText)
{
    var element =
        new Footer(
            new Paragraph(
                new ParagraphProperties(
                    new ParagraphStyleId() { Val = "Footer" }),
                new Run(
                    new Text(FooterText),
                    // *** Adaptation: This will output the page number dynamically ***
                    new SimpleField() { Instruction = "PAGE" })
            ));

    return element;
}

PAGE请注意,您可以通过后缀文本来更改页码的格式。来自Ecma Office Open XML Part 1 - Fundamentals And Markup Language Reference.pdf

当前页码为 19 并更新以下字段时:

PAGE
PAGE \* ArabicDash
PAGE \* ALPHABETIC
PAGE \* 罗马

结果是:

19 - 19
-
第十九

因此,例如要获取罗马数字,您需要将SimpleField上面的代码行更改为:

new SimpleField() { Instruction = "PAGE \\* roman" })

或(如果您愿意)

new SimpleField() { Instruction = @"PAGE \* roman" })
于 2016-07-18T16:53:14.533 回答
-1

尝试这个:

   private static void GenerateFooterPartContent(WordprocessingDocument package, string text = null)
    {
        FooterPart footerPart1 = package.MainDocumentPart.FooterParts.FirstOrDefault();
        if (footerPart1 == null)
        {
            footerPart1 = package.MainDocumentPart.AddNewPart<FooterPart>();
        }

        var relationshipId = package.MainDocumentPart.GetIdOfPart(footerPart1);
        // Get SectionProperties and set HeaderReference and FooterRefernce with new Id
        SectionProperties sectionProperties1 = new SectionProperties();
        FooterReference footerReference2 = new FooterReference() { Type = HeaderFooterValues.Default, Id = relationshipId };
        sectionProperties1.Append(footerReference2);
        package.MainDocumentPart.Document.Body.Append(sectionProperties1);


        Footer footer1 = new Footer();


        Paragraph paragraph2 = CreateParagraph(package, string.Empty, "Footer");
        Run r = new Run(new SimpleField() { Instruction = "DATE" });          

        paragraph2.Append(r);

        if (!string.IsNullOrWhiteSpace(text))
        {
            r = new Run();
            PositionalTab positionalTab1 = new PositionalTab() { Alignment = AbsolutePositionTabAlignmentValues.Center, 
                RelativeTo = AbsolutePositionTabPositioningBaseValues.Margin, 
                Leader = AbsolutePositionTabLeaderCharValues.None };

            r.Append(positionalTab1);
            paragraph2.Append(r);
            r = new Run(new Text(text) { Space = SpaceProcessingModeValues.Preserve });
            paragraph2.Append(r);
        }

        r = new Run();
        PositionalTab positionalTab2 = new PositionalTab() { Alignment = AbsolutePositionTabAlignmentValues.Right, 
            RelativeTo = AbsolutePositionTabPositioningBaseValues.Margin, 
            Leader = AbsolutePositionTabLeaderCharValues.None };

        r.Append(positionalTab2);
        paragraph2.Append(r);

        r = new Run(new Text("Page: ") { Space = SpaceProcessingModeValues.Preserve },
              // *** Adaptation: This will output the page number dynamically ***
              new SimpleField() { Instruction = "PAGE" },
              new Text(" of ") { Space = SpaceProcessingModeValues.Preserve },
              // *** Adaptation: This will output the number of pages dynamically ***
              new SimpleField() { Instruction = "NUMPAGES" });
        paragraph2.Append(r);

        footer1.Append(paragraph2);

        footerPart1.Footer = footer1;
    }

有关更多说明,请参阅以下链接

于 2020-05-28T09:44:30.997 回答