How to exclude the first page from page numbering and set the second page as "page 1"?

Hi
I want to exclude the first page from page numbering, with page numbering starting from “page 1” on the second page. The table of contents should also reflect the correct page numbers. How can I achieve this?

@weiyaochen

To exclude the first page from page numbering and start numbering from “page 1” on the second page in Aspose.Words for .NET, you can follow these steps:

  1. Create a New Section: You need to create a new section in your document. The first page will be in one section, and the second page will be in another section.

  2. Set Up Page Numbering:

    • For the first section (which contains the first page), you will not set any page numbering.
    • For the second section (starting from the second page), you will enable page numbering and set it to start from 1.
  3. Adjust the Table of Contents: Ensure that the table of contents is updated to reflect the correct page numbers.

Here is a code example demonstrating how to achieve this:

using Aspose.Words;
using Aspose.Words.Section;

class Program
{
    static void Main()
    {
        // Load your document
        Document doc = new Document("YourDocument.docx");

        // Create a new section for the second page
        Section section = new Section(doc);
        doc.AppendChild(section);

        // Set the first section to not show page numbers
        section.PageSetup.RestartPageNumbering = true;
        section.PageSetup.SectionStart = SectionStart.NewPage;

        // Create a header/footer for the second section
        HeaderFooter footer = new HeaderFooter(doc, HeaderFooterType.FooterPrimary);
        section.HeadersFooters.Add(footer);
        footer.AppendChild(new Paragraph(doc));

        // Add page numbering to the footer of the second section
        footer.FirstParagraph.AppendField("PAGE");

        // Save the document
        doc.Save("UpdatedDocument.docx");
    }
}

Explanation:

  • The code creates a new section for the second page and sets the RestartPageNumbering property to true.
  • It adds a footer to the second section that includes the page number field.
  • The first section will not display any page numbers, effectively excluding the first page from numbering.

Make sure to update your table of contents after making these changes to reflect the correct page numbers.

For more detailed information on managing page numbering in Aspose.Words, you can refer to the official documentation on restarting page numbering here.

If you have any further questions or need additional assistance, feel free to ask!

1 Like

Thank you for providing the solution, it is indeed feasible.

Additionally, I would like to ask for your advice. Since the footer requires other settings, such as inserting text separately on the left and right, here is the original code for the footer:

// Set the footer
builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
// Set the footer text
builder.Font.Size = 7;
// Set the width and height of the image
double customWidth = ConvertUtil.InchToPoint(6.6);
double customHeight = 6;

// Insert the image into the header
Shape imageShape_footer = builder.InsertImage("TopLine.png", customWidth, customHeight);

// Disable aspect ratio lock, set WrapType to None to avoid affecting other content
imageShape_footer.WrapType = WrapType.None;

// Set the horizontal alignment of the image to center
imageShape_footer.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
imageShape_footer.Left = (builder.PageSetup.PageWidth - customWidth) / 2;
// Insert a line break
builder.InsertParagraph();

// Set paragraph formatting
builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
// Add a right-aligned tab stop
builder.ParagraphFormat.TabStops.Add(new TabStop(builder.PageSetup.PageWidth - builder.PageSetup.RightMargin - builder.PageSetup.LeftMargin, TabAlignment.Right, TabLeader.None));

// Insert left-aligned text
builder.Write("Copyright © 2024 TEST Inc.");

// Insert tab, right-align the page number
builder.Write("\t"); // Use tab to align the page number
builder.InsertField("PAGE", ""); // Insert page number field
builder.InsertBreak(BreakType.ParagraphBreak);

// Insert left-aligned text — second line
builder.Writeln("Continuity Computing System Inc.");

@weiyaochen Could you please elaborate the problem? If possible please attach the expected output documents. We will check it and provide you more information.

@alexey.noskov
This is the file I expected to output.
I need to make the following 3 settings:

  1. The homepage has no header or footer.
  2. There is a watermark image in the middle of each page.
  3. Pictures need to be inserted at the top and bottom of the page.

範例檔案.docx (382.7 KB)

@weiyaochen You can use code like the following:

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

// Home page.
builder.Font.Size = 48;
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.Write("HOME PAGE");
// Insert section break.
builder.InsertBreak(BreakType.SectionBreakNewPage);
builder.Font.ClearFormatting();
builder.ParagraphFormat.ClearFormatting();
// Configure page numbering.
builder.PageSetup.RestartPageNumbering = true;
builder.PageSetup.PageStartingNumber = 1;
// Insert TOC
builder.Writeln("TOC");
builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u");
builder.Writeln();
// Insert page break.
builder.InsertBreak(BreakType.PageBreak);
builder.Writeln();
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
builder.Write("Page 3");

// Create header and footer.
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.InsertImage(@"C:\Temp\line.png");
builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
builder.InsertImage(@"C:\Temp\line.png");
builder.StartTable();
builder.CellFormat.Borders.LineStyle = LineStyle.None;
builder.InsertCell();
builder.Write("footer text");
builder.InsertCell();
builder.ParagraphFormat.Alignment = ParagraphAlignment.Right;
builder.InsertField("PAGE");
builder.EndRow();
builder.EndTable();

// Insert watermark.
doc.Watermark.SetImage(@"C:\Temp\watermark.png", new ImageWatermarkOptions());

// Update fields.
doc.UpdateFields();

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

out.docx (666.4 KB)

1 Like

Wow that’s amazing, thanks so much for the help :100:

1 Like