我在 Visual Studio 中使用 OXML 创建一个 word 文档。我不知道它会持续多久,我需要在文档的页脚中添加一个简单的页码。
为了生成页眉和页脚,我使用了这个: https ://msdn.microsoft.com/en-us/library/ee355228(v=office.12).aspx
据我了解,这会在我在文档中写任何内容之前预设默认的页眉/页脚。所以我不太确定是否可以为此添加页码?我真的很感激你的帮助,因为我已经坚持了一整天......
我在 Visual Studio 中使用 OXML 创建一个 word 文档。我不知道它会持续多久,我需要在文档的页脚中添加一个简单的页码。
为了生成页眉和页脚,我使用了这个: https ://msdn.microsoft.com/en-us/library/ee355228(v=office.12).aspx
据我了解,这会在我在文档中写任何内容之前预设默认的页眉/页脚。所以我不太确定是否可以为此添加页码?我真的很感激你的帮助,因为我已经坚持了一整天......
SimpleField
您可以通过添加 a和Instruction
of来添加动态页码"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" })
尝试这个:
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;
}
有关更多说明,请参阅以下链接 。