Donate

Generate Page Numbers Of Word Document Using OpenXML

Hello, I've been working on OpenXML framework specifically for generating reports on a Word Document. One of the task of the project is to add page numbers to a document with the format of "Page 1 of 72". After doing some researches on stackoverflow and google, I found a few links with functions on how to add page numbers but not in the format as specified for my requirement. In case you need to add page numbers to a word document specifically in the footer area, see function below.
private Footer GeneratePageNumbers(string FooterText)
{
 var element =
   new Footer(
    new Paragraph(
     new ParagraphProperties(
      new ParagraphStyleId() { Val = "Footer" },
      new Justification() { Val = JustificationValues.Center }),
     new Run(
      new Text() { Text = FooterText, Space = SpaceProcessingModeValues.Preserve }),
     new Run(
      new SimpleField() { Instruction = "Page" }),
     new Run(
      new Text() { Text = " of ", Space = SpaceProcessingModeValues.Preserve }),
     new Run(
      new SimpleField() { Instruction = "NUMPAGES" })
    ));

 return element;
}
Reference: How to dynamically add a page number in footer in Microsoft OpenXML C#

Comments

Donate

Popular Posts From This Blog

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

How To Insert Or Add Emojis In Microsoft Teams Status Message

TypeScript Error Or Bug: The term 'tsc' is not recognized as the name of a cmdlet, function, script file, or operable program.