How to reset page number for each section start at 1 in a word document in C#

Sections are widely used by developers to set different formatting or layout options to each different section, such as use the different header and footer information for different sections and reset page number for each section dynamically. With the help of Spire.Doc for .NET, we can easily insert word section and remove word section in C# and VB.NET. We will show you how to reset page numbering that starts at 1 for each section easily by using Spire.Doc.

Firstly make sure Spire.Doc for .NET has been installed correctly and then add Spire.Doc.dll as reference in the downloaded Bin folder though the below path: "..\Spire.Doc\Bin\NET4.0\ Spire.Doc.dll". Here comes to the codes of how to reset page numbering for each section.

Step 1: Load three different word documents

Document document1 = new Document();
document1.LoadFromFile("..\\..\\1.docx");
         
Document document2 = new Document();
document2.LoadFromFile("..\\..\\2.docx");

Document document3 = new Document();
document3.LoadFromFile("..\\..\\3.docx");

Step 2: Use section method to combine all documents into one word document

foreach (Section sec in document2.Sections)
{
document1.Sections.Add(sec.Clone());
}
foreach (Section sec in document3.Sections)
{
document1.Sections.Add(sec.Clone());     
}

Step 3: Traverse the document

//Traverse every section of document1 
foreach (Section sec in document1.Sections)
{
//Traverse every object of the footer
foreach (DocumentObject obj in sec.HeadersFooters.Footer.ChildObjects)
{
if (obj.DocumentObjectType == DocumentObjectType.StructureDocumentTag)
{
DocumentObject para = obj.ChildObjects[0];
foreach (DocumentObject item in para.ChildObjects)
                        {
if (item.DocumentObjectType == DocumentObjectType.Field)

Step 4: Find the field type FieldNumPages and change it to FieldSectionPages

//Find the item and its field type is FieldNumPages
if ((item as Field).Type == FieldType.FieldNumPages)
{
//Change field type to FieldSectionPages
(item as Field).Type = FieldType.FieldSectionPages;

Step 5: Restart page number of section and set the starting page number to 1

document1.Sections[1].PageSetup.RestartPageNumbering = true;
document1.Sections[1].PageSetup.PageStartingNumber = 1;
       
document1.Sections[2].PageSetup.RestartPageNumbering = true;
document1.Sections[2].PageSetup.PageStartingNumber = 1;

Step 6: Save the document to file and launch it.

document1.SaveToFile("sample.docx",FileFormat.Docx);  
System.Diagnostics.Process.Start("sample.docx");

Full codes:

namespace ResetPageNumber
{
    class Program
    {
        static void Main(string[] args)
        {
            Document document1 = new Document();
            document1.LoadFromFile("..\\..\\1.docx");
         
            Document document2 = new Document();
            document2.LoadFromFile("..\\..\\2.docx");

            Document document3 = new Document();
            document3.LoadFromFile("..\\..\\3.docx");

            foreach (Section sec in document2.Sections)
            {
                document1.Sections.Add(sec.Clone());
            }

            foreach (Section sec in document3.Sections)
            {
                document1.Sections.Add(sec.Clone());     
               
            }
            foreach (Section sec in document1.Sections)
            {
                foreach (DocumentObject obj in sec.HeadersFooters.Footer.ChildObjects)
                {
                    if (obj.DocumentObjectType == DocumentObjectType.StructureDocumentTag)
                    {
                        DocumentObject para = obj.ChildObjects[0];
                        foreach (DocumentObject item in para.ChildObjects)
                        {
                            if (item.DocumentObjectType == DocumentObjectType.Field)
                            {
                                if ((item as Field).Type == FieldType.FieldNumPages)
                                {
                                    (item as Field).Type = FieldType.FieldSectionPages;
                                }
                            }
                        }
                    }       
                }
            }

            document1.Sections[1].PageSetup.RestartPageNumbering = true;
            document1.Sections[1].PageSetup.PageStartingNumber = 1;
       
            document1.Sections[2].PageSetup.RestartPageNumbering = true;
            document1.Sections[2].PageSetup.PageStartingNumber = 1;

            document1.SaveToFile("sample.docx",FileFormat.Docx);
            System.Diagnostics.Process.Start("sample.docx");
        }
    }
}