Free Support Forum - aspose.com

How to insert page number started from specific number using C#

Hi,

We are replacing word interop functionalities in our project with Aspose Word DotNet library and one of the requirement is having a custom generated sequential page numbers in the right corner header of the document in all pages. So if the document has 10 pages the page numbers could start from 25 and end in 25+10=35. Based on how the document is used each time this starting numbering will change. I am using the code from this below link to read the sections count and assign numbers in a loop of section count. The problem I am facing is the client documents are skipping the first and last page of the document for assigning page numbers. The numbers are getting assigned to the middle pages in sequential order correctly but not getting assigned to the first and last pages.

Please find link and correctly generated document from word interop and wrongly generated document from aspose.
Samples.zip (38.7 KB)

@arunmanohar

Please use the following code example to get the desired output. Hope this helps you.

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

for (int i = 0; i < 10; i++)
{
    builder.InsertBreak(BreakType.PageBreak);
}

builder.MoveToSection(0);
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.Write("Page ");
builder.InsertField("PAGE", "");

// Set first section page numbering
Section section = doc.Sections[0];
section.PageSetup.RestartPageNumbering = true;
section.PageSetup.PageStartingNumber = 25;

doc.UpdateFields();
doc.Save(MyDir + "Field.Num.docx");

Thanks Tahir, I will try it out. One question though, is there any way to give a specific font color to the page field. As I want the number to be red in color. Right now I am using builder.writeln to write a variable value and move to that header and assign a font color.

Hi I did test it with the attached sample document but when the pagebreak is adding to the document, its adding extra blank pages to the starting of the document. I want to keep the page numbers same. This document has 4 pages and when tested with the above code it generated 4 additional blank pages in the start of the document.

Please provide any resolution.

8a2e3730-919e-4249-a117-c49b0badc2a4Working_1199298.zip (11.8 KB)

Thanks

@arunmanohar

We are working over your query and will get back to you soon.

Thanks Tahir, also please note the page number is also not generated in the first page and present in all other pages correctly.

@arunmanohar

You can set the font color of text as shown below.

builder.MoveToSection(0);
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.Font.Color = Color.Red;
builder.ParagraphFormat.Alignment = ParagraphAlignment.Right;
builder.Write("Page ");
builder.InsertField("PAGE", "");

In your document, different header and footer for first page is set. You and disable it using PageSetup.DifferentFirstPageHeaderFooter property as shown below.

doc.FirstSection.PageSetup.DifferentFirstPageHeaderFooter = false;

To ensure a timely and accurate response, please attach the following resources here for testing:

  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.
  • Please create a standalone console application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

Hi Tahir,

I have been testing the code provided with whole lot of documents and for most of the documents its working fine. A particular document is giving problem though. It has most of the pages in landscape mode and in the middle, the 35th page is in portrait mode. The page number is not written in that page alone and starting to continue in the 36th page on wards to the end.

Please review and let me know what can be done with this.
eDoc_2020-00194_landscape-Proposed_rule_FCC_19-53_final_46pages_Original_1206004.zip (83.9 KB)

I am setting the differentfirstpageheaderfooter as per your suggestion and its been working for the documents till this case.

Thanks

@arunmanohar

We have tested the scenario using the latest version of Aspose.Words for .NET 20.3 and have not found the shared issue. So, please use Aspose.Words for .NET 20.3.

Document doc = new Document(MyDir + "eDoc_2020-00194_landscape-Proposed_rule_FCC_19-53_final_46pages_Original_1206004.docx");
foreach (Section section in doc.Sections)
{
    section.PageSetup.DifferentFirstPageHeaderFooter = false;
}
doc.Save(MyDir + "20.3.docx");

Hi Tahir,

I have been testing the code provided with whole lot of documents and for most of the documents its working fine. A particular document is giving problem though. It has different header on page 17 compared to page 16 and 18 and there by page number is not written in that page alone and starting to continue from 1 to end after not printing any page number on page 17 .I am attaching the code cs file and the
Please find the documenttemp.zip (48.2 KB)

@arunmanohar

We are working over your query and will get back to you soon.

@arunmanohar

You need to insert the page number field for HeaderFooterType HeaderFirst and FooterFirst. Please use the following code example to get the desired output. Hope this helps you.

Aspose.Words.Document Document = new Aspose.Words.Document(MyDir + "eDoc_input.docx");
RemoveHeaders(Document);

foreach (Aspose.Words.Section footerSection in Document.Sections)
{
    //section.HeadersFooters.RemoveAt(0);
    Aspose.Words.HeaderFooter footer;
    // Primary Header is the header used for odd pages.
    footer = footerSection.HeadersFooters[Aspose.Words.HeaderFooterType.FooterPrimary];

    if (footer != null)
        footer.Remove();
}

int pageCount1 = Document.BuiltInDocumentProperties.Pages;
Document.FirstSection.PageSetup.DifferentFirstPageHeaderFooter = false;
//Insert section breaks at each page
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(Document);
int start = 1;

int pageCount = Document.PageCount;
int end = start + (pageCount - 1);
builder.MoveToSection(0);
builder.MoveToHeaderFooter(Aspose.Words.HeaderFooterType.HeaderPrimary);
//builder.Write("Page ");
builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Right;
builder.Font.Color = System.Drawing.Color.Red;
builder.Write("-");
builder.InsertField("PAGE", "");
builder.Write("-");

builder.MoveToHeaderFooter(Aspose.Words.HeaderFooterType.FooterPrimary);
//builder.Write("Page ");
builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Right;
builder.Write("-");
builder.InsertField("PAGE", "");
builder.Write("-");


builder.MoveToSection(1);
builder.MoveToHeaderFooter(Aspose.Words.HeaderFooterType.HeaderFirst);
//builder.Write("Page ");
builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Right;
builder.Font.Color = System.Drawing.Color.Red;
builder.Write("-");
builder.InsertField("PAGE", "");
builder.Write("-");

builder.MoveToSection(1);
builder.MoveToHeaderFooter(Aspose.Words.HeaderFooterType.FooterFirst);
//builder.Write("Page ");
builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Right;
builder.Write("-");
builder.InsertField("PAGE", "");
builder.Write("-");

// Set first section page numbering
Aspose.Words.Section section = Document.Sections[0];
section.PageSetup.RestartPageNumbering = true;
section.PageSetup.PageStartingNumber = start;

section = Document.Sections[1];
section.PageSetup.RestartPageNumbering = false;

Document.UpdateFields();
Console.WriteLine(Document.PageCount);
Document.Save(MyDir+ "out.docx");

Thanks Tahir,
The code works for this document.We get huge variety of documents in our Production System and with different format testing all the scenarios is not possible. I want to make sure if I need to add different sections for Aspose.Words.HeaderFooterType.HeaderEven and Aspose.Words.HeaderFooterType.FooterEven to make sure we cover all the scenarios.Example code attached.code - Copy.zip (1005 Bytes)

@arunmanohar

Please check the HeaderFooterType enumeration. You need to add page number field in all header and footer types.