How to format page numbers in Aspose Word using java

Hi Team,

Can we format page numbers and TOC. By formatting, I mean, can we add font size and font family, etc to the page number and Toc content?

I am using the following code to add page numbers

documentBuilder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
documentBuilder.getCurrentParagraph().getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
documentBuilder.insertField(FieldType.page, true);

I have checked in MS word where we can change the font size, font family, etc of the page number.

Please let me know if this is possible.

Regards

@ashish29190,

Please ZIP and attach the following resources here for testing:

  • Your simplified source Word document
  • Aspose.Words for Java 21.4 generated output DOCX file showing the undesired behavior (if any)
  • Your expected DOCX file showing the desired output. You can create this document manually by using MS Word.

As soon as you get these pieces of information ready, we will start further investigation into your scenario and provide you code to achieve the same output by using Aspose.Words.

Hi Awais,

Thanks for reply.

I am using a licensed version of Aspose Word 18.9.

My content is in HTML and I am using

documentBuilder.insertHtml(htmlContentVariable);

The code which I have used to add page numbers is in the original question.

I am using following code to set the Style for Page Numbers in the footer.

Style style = doc.getStyles().getByStyleIdentifier(StyleIdentifier.PAGE_NUMBER);
style.getFont().setSize(10);
style.getFont().setName("Calibri");

I have attached word file with issue and word file of what I expect. Also I have attached an image of what issue I am talking about.
attach.zip (375.2 KB)

Do let me know if you need more details.

Regards

@ashish29190,

To change the font size and font name of Page fields in Word document, please use the following code of Aspose.Words for Java:

Document doc = new Document("C:\\Temp\\attach\\Doc having issue with page numbers font.docx");

for (Field field : doc.getRange().getFields()) {
    if (field.getType() == FieldType.FIELD_PAGE) {
        Node currentNode = field.getStart();

        while (currentNode != null && currentNode != field.getEnd()) {
            if (currentNode.getNodeType() == NodeType.RUN) {
                Run run = (Run) currentNode;
                run.getFont().setName("Calibri");
                run.getFont().setSize(10);
            }

            Node nextNode = currentNode.nextPreOrder(currentNode.getDocument());
            currentNode = nextNode;
        }
    }
}

doc.save("C:\\Temp\\attach\\awjava-21.4.docx");

@ashish29190,

And try the following Java code that should change the Font formatting of Table of Content (TOC) field’s page numbers in output DOCX document.

Document doc = new Document("C:\\Temp\\attach\\Doc having issue with page numbers font.docx");

for (FieldStart field : (Iterable<FieldStart>) doc.getChildNodes(NodeType.FIELD_START, true)) {
    if (field.getFieldType() == (FieldType.FIELD_PAGE_REF)) {
        FieldPageRef pageRef = (FieldPageRef) field.getField();
        if (pageRef.getBookmarkName() != null && pageRef.getBookmarkName().startsWith("_Toc")) {

            Node currentNode = pageRef.getStart();
            while (currentNode != null && currentNode != pageRef.getEnd()) {
                if (currentNode.getNodeType() == NodeType.RUN) {
                    Run run = (Run) currentNode;
                    // run.getFont().setName("Verdana");
                    // run.getFont().setItalic(true);
                    // run.getFont().setSize(18);
                    run.getFont().setColor(Color.BLUE);
                }
                currentNode = currentNode.nextPreOrder(currentNode.getDocument());
            }
        }
    }
}

doc.save("C:\\Temp\\attach\\awjava-21.4.docx");