We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Locate and replace page numbers

We are performing mail merge through Aspose.Words. We need to replace total and initial page number in a document, since it’s going to be printed together with other documents.

Currently, we are doing this through Word automation, by searching page fields in all footer and header sections, and replacing its contents. Here’s the code:

Interop.Word.PageNumbers pagesNumbers= wDoc.Sections.First.Footers.Item (WdHeaderFooterIndex.wdHeaderFooterPrimary).PageNumbers;
pagesNumbers.RestartNumberingAtSection = true;
pagesNumbers.StartingNumber = startingNumber;
Interop.Word.Range range = wDoc.Sections.First.Footers.Item (WdHeaderFooterIndex.wdHeaderFooterPrimary).Range;
for (int i=1; i<= range.Fields.Count;i++)
{
if (range.Fields.Item(i).Type == WdFieldType.wdFieldNumPages)
{
Interop.Word.Field field = range.Fields.Item(i);
Interop.Word.Range rangeField = range.Fields.Item(i).Code;
field.Delete();
rangeField.Text = numPages.ToString();
}
}
//Same for headers…

Is there any way to perform this through Aspose.Words?

Thank you!

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. I think you can specify PageStartingNumber. Please see the following link for more information:

http://www.aspose.com/documentation/file-format-components/aspose.words-for-.net-and-java/aspose.words.pagesetup.pagestartingnumber.html

MS Word will update page numbers automatically during printing the document.

Best regards.