How to add page number

Hi,
I have this process:
I have some documents and I need to put all them in a document. But my problem is that I need to put page numbering in a new document. I use this code:

For i = 0 To oBuilder.Document.Sections.Count - 1
    oBuilder.MoveToSection(i)
    oBuilder.MoveToHeaderFooter(Aspose.Words.HeaderFooterType.FooterPrimary)
    oBuilder.InsertField("PAGE", "")
    oBuilder.CurrentParagraph.ParagraphFormat.Alignment = oAlign
Next

But If a previous document have a page numbering the result is:
Page 1 -> 11
Page 2 -> 22
Duplicate the number.
Thanks for all.

Hi
Thanks for your request. Your code is correct. To prevent duplication of page numbers, you should just check whether footer contains PAGE field. For example you can try using the following code:

private static bool HasPageField(Story story)
{
    // Get all FieldStart nodes from the current story.
    NodeCollection starts = story.GetChildNodes(NodeType.FieldStart, true);
    // Check whether one of field starts is start of PAGE field.
    foreach(FieldStart start in starts)
    {
        if (start.FieldType == FieldType.FieldPage)
            return true;
    }
    // Return false if we did not found any PAGE fields.
    return false;
}

Hope this helps. Please let me know if you need more information. I will be glad to help you.
Best regards,

Perfect! This is a good solution:)
But now, I need to start page numbering in a specific number not 1 for default. How to this?
Thanks for all.

Hello
Thanks for your inquiry. In this case you can try using PageStartingNumber property. Please see the following link to learn more:
https://reference.aspose.com/words/net/aspose.words/pagesetup/pagestartingnumber/
Best regards,