Page Number in Header and Footer

Hello Team,

We would like to know whether it is possible to show only Page Numbers in the Header and Footer section of the aspose document.

We are aware of Page X of Y as we are uisng this in our application.

Do let us know how to ahieve the Page Number property in the document.

Thanks & Regards,
Varsha

Attached is the document for your reference.

Hi

Thanks for your request. Yes, of course you can do that using Aspose.Words. you should just insert PAGE field into header and footer of your document. for example see the following code:

// Create new Docuemnt and DocumentBuilder
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert PAGE field into the header and footer
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.Write("- ");
builder.InsertField("PAGE", null);
builder.Write(" -");
// do the same for primary footer
builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.Write("- ");
builder.InsertField("PAGE", null);
builder.Write(" -");
// Now move cursor to the document's body and insert page break (just for testing)
builder.MoveToDocumentStart();
builder.InsertBreak(BreakType.PageBreak);
// Save output document
doc.Save(@"Test174\out.doc");

In addition, I think the following article could be useful for you:
https://docs.aspose.com/words/net/working-with-headers-and-footers/
Hope this helps.
Best regards.

Thanks Team for the Solution you have provided. It worked in our application.
Now we have one more requirement like if user wants to set some Free Text in Header and Footer along with the Page Number, how the same can can be achieved?

Attached is the sample document for your reference.

Kindly take a look at it and let us know.

Once again thanks very much for your timely support.

Best Regards,
Varsha

Hi

Thanks for your request. Code is the same as I already provided in my previous answer. There are just small changes:

// Create new Docuemnt and DocumentBuilder
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert PAGE field into the header and footer
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.Write("- ");
builder.InsertField("PAGE", null);
builder.Write(" -");
// Here you can insert more text
// Also you can change formation of text, for example color
builder.Font.Color = Color.LightGreen;
builder.Write("Company Name: ABC Pvt Ltd.");
// do the same for primary footer
builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.Write("Page ");
builder.InsertField("PAGE", null);
builder.Write(" of ");
builder.InsertField("NUMPAGES", null);
// Here you can insert more text
// Also you can change formation of text, for example color
builder.Font.Color = Color.Red;
builder.Write("Company Name: ABC Pvt Ltd.");
// Now move cursor to the document's body and insert page break (just for testing)
builder.MoveToDocumentStart();
builder.InsertBreak(BreakType.PageBreak);
// Save output document
doc.Save(@"Test174\out.doc");

Best regards.

Hi @alexey.noskov,

I am trying to add footer without borders as below

builder.startTable();

// Clear table borders
builder.getCellFormat().clearFormatting();

builder.insertCell();

When builder.insertCell(); line executes it is creating a cell with border in footer as below

I want to remove the border in below snip

I do not want that border visible in footer section. I want footer as below where I need to add three items in the footer as below

Could you pls help me to get that border removed and adding those 3 items in footer as in above snip

Thanks

@ramesh676 Please use the following code to clear table borders:

// Clear table borders
builder.getCellFormat().getBorders().setLineStyle(LineStyle.NONE);

Hi @alexey.noskov,

The above line worked fine for me thanks!!

I have one more query , I want to decrease the font size in footer. How can I control the font size?

Ramesh.

@ramesh676 You can specify font formatting using Font class properties. For example see the following code:

builder.getFont().setSize(36.0);
builder.write("Some text");