Tags

, , ,

Adding “Page X of Y” pagination type into a Word document from C# into a Word add-in project can be very difficult. I have spent hours to figure out how to do this. It seems to be a special need for this, because sometimes people are requested to scan documents and miss some pages. Now looking back, code seems to be easy, but believe me, wasn’t easy at all to put pieces side by side to obtain this effect because there is lack of documentation about Word add-ins projects.

foreach(Section section in Globals.ThisAddIn.Application.ActiveDocument.Sections)
{
                Range footer = section.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
 
                footer.Collapse(WdCollapseDirection.wdCollapseStart);
                footer.Fields.Add(footer, WdFieldType.wdFieldNumPages);
                footer.Collapse(WdCollapseDirection.wdCollapseStart);
                footer.InsertAfter(" of ");
                footer.Collapse(WdCollapseDirection.wdCollapseStart);
                footer.Fields.Add(footer, WdFieldType.wdFieldPage);
                footer.Collapse(WdCollapseDirection.wdCollapseStart);
                footer.InsertAfter("Page ");
}

Feel to extend it and use this code. Simple as this you can accomplish what is requested.

Advertisements