Dynamic Font sizing on footer page number

Hi Team,

We have a requirement where footer page number size needs to be changed via dot net code. This page number itself is being done through dot net code by inserting a shape so as seen in the attached sample template there would be two shapes in the footer one has page number and other has some static text and we would need to dynamically change the font size of the page number to 10. While doing this content in the header shouldn’t be disturbed. Kindly share us a code sample to achieve this logic.

Dynamic Font sizing Sample template.docx (58.4 KB)

Regards & Thanks

@Karthik_Test_account,

On my side, there is only one shape in the footer of your document, containing the page number. Could you please verify if it is the same on your side?

To change the font size of the page number paragraph in the footer of your document please use the following code example:

Shape s = (Shape)doc.FirstSection.HeadersFooters[HeaderFooterType.FooterPrimary].GetChild(NodeType.Shape, 0, true);
((Paragraph)s.GetChild(NodeType.Paragraph, 0, true)).ParagraphFormat.Style.Font.Size = 10;

@sergey.lobanov, Thank you this code works but facing an issue as this reduce the font size of Draft text box in the header as well. That shouldn’t be resized only the number in the footer should be resized. Could you please suggest an altered code that doesn’t affect the header

Regards & Thanks

@Karthik_Test_account The problem occurs because font size is applied to paragraph style, which can be also applied to other paragraphs. In your case you can set font size explicitly to inline nodes in your shape. For example see the following code:

Document doc = new Document(@"C:\Temp\in.docx");
Shape s = (Shape)doc.FirstSection.HeadersFooters[HeaderFooterType.FooterPrimary].GetChild(NodeType.Shape, 0, true);

List<Node> inlines = s.GetChildNodes(NodeType.Any, true)
    .Where(n => n.NodeType == NodeType.Run || n.NodeType == NodeType.FieldStart ||
        n.NodeType == NodeType.FieldSeparator || n.NodeType == NodeType.FieldEnd).ToList();

foreach (Inline i in inlines)
    i.Font.Size = 10;

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

Thank you @alexey.noskov.

This doesn’t seem to work and the font size of the page number didn’t change on this. Not sure if I am missing something from my end. Could you please confirm from your end once.

Regards & Thanks

@Karthik_Test_account The code changes font size of primary footer, in your document there is also first page footer. If you need to change font size in the first page footer too you can use this code:

Shape s = (Shape)doc.FirstSection.HeadersFooters[HeaderFooterType.FooterFirst].GetChild(NodeType.Shape, 0, true);

Thank you @alexey.noskov This worked :))