Custom Page Numbers 1/2,2/2 in Footers Aspose Words

I want to add custom page numbers (like 1/2,2/2) to word document with using Aspose.Words. But I couldn’t find any sample for c# language. Pls help! Thanks!

@kshah05 In MS Words page number and number of pages are inserted as fields {PAGE} and NUMPAGES. You can achieve this using DocumentBuilder.InsertField method. For example see the following code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Insert few page breaks just for demonstration urposes.
builder.Write("First page");
builder.InsertBreak(BreakType.PageBreak);
builder.Write("Second page");
builder.InsertBreak(BreakType.PageBreak);
builder.Write("Third page");

// Move DocumentBuilder into the primary footer and insert page numbers.
builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
builder.InsertField(FieldType.FieldPage, true);
builder.Write("/");
builder.InsertField(FieldType.FieldNumPages, true);

// Save the result.
doc.Save(@"C:\Temp\out.docx");

Here is the output produced by this code: out.docx (7.5 KB)

Thanks, I see the Custom page numbers showing up. But its showing up on the left of the footer. How do we show it to the right?
Thanks
Kapil

@kshah05 You should simply specify ParagraphAlignment.Right as shown in the code below:

// Move DocumentBuilder into the primary footer and insert page numbers.
builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Right;
builder.InsertField(FieldType.FieldPage, true);
builder.Write("/");
builder.InsertField(FieldType.FieldNumPages, true);