Insert page number in header of word document

Dear Team,

How to insert page number in header of the document using aspose.words java. please give solution for this scenario.

@Vadivel_S_S,

Please try using the following code:

Document doc = new Document("D:\\temp\\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
builder.insertField(FieldType.FIELD_PAGE, true);

doc.save("D:\\temp\\awjava-18.1.docx");

Dear Team,

Possible to add the same page stamp within body text instead of header.I have using this code code.zip (517 Bytes)

My output : output.zip (17.4 KB)

Expected Output : Exp_out.zip (15.6 KB)

Regards,
S S Vadivel

@Vadivel_S_S,

Please try using the following code:

Document doc = new Document("D:\\temp\\input.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

NodeCollection paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);
LayoutCollector collector = new LayoutCollector(doc);

NodeCollection paras = doc.getChildNodes(NodeType.PARAGRAPH, true);
int pageIndex = 1;
for (int i=0; i<paras.getCount(); i++)
{
    Paragraph para = (Paragraph)paras.get(i);
    if (collector.getStartPageIndex(para) == pageIndex)
    {
        builder.moveToParagraph(i, 0);
        builder.insertField(FieldType.FIELD_PAGE, false);

        pageIndex++;
    }
}

doc.updatePageLayout();
doc.updateFields();

doc.save("D:\\temp\\awjava-18.2.docx");

Dear Hafeez,

Thanks for your quick replying. Its working fine, But some pages comes not properly.

I have attached my output awjava-18.2.zip (1.7 MB). Please check this document. Page 3,6 and 8 are not placed properly.

@Vadivel_S_S,

Please also ZIP and upload your input Word document (you have generated awjava-18.2.docx from) here for testing. We will investigate the issue further on our end and provide you more information.

Thanks for your quick replying.

Input Document: sample.zip (2.6 MB)

Please find.

@Vadivel_S_S,

Please try using the following code:

Document doc = new Document("D:\\temp\\Sample\\sample.doc");

Node[] runs = doc.getChildNodes(NodeType.RUN, true).toArray();
for (int i = 0; i < runs.length; i++) {
    Run run = (Run) runs[i];
    int length = run.getText().length();

    Run currentNode = run;
    for (int x = 1; x < length; x++) {
        currentNode = splitRun(currentNode, 1);
    }
}

DocumentBuilder builder = new DocumentBuilder(doc);

NodeCollection smallRuns = doc.getChildNodes(NodeType.RUN, true);
LayoutCollector collector = new LayoutCollector(doc);

int pageIndex = 1;
for (int i=0; i<smallRuns.getCount(); i++)
{
    Run smallRun = (Run)smallRuns.get(i);
    if (collector.getStartPageIndex(smallRun) == pageIndex)
    {
        builder.moveTo(smallRun);
        builder.insertField(FieldType.FIELD_PAGE, false);

        pageIndex++;
    }
}

doc.updatePageLayout();
doc.updateFields();

doc.save("D:\\temp\\Sample\\awjava-18.2.doc");

Dear Hafeez,

I have used below code but not clear. I need to add page number in every page in first of the line. In that code not properly added. Please give proper result for my expected output. I have attached sample for your reference.

Below code Defects : some page number is inserted twice in a page. some page number not in document.

Expected Output : Paper revised.zip (59.0 KB)

@Vadivel_S_S,

We are checking this scenario and will get back to you soon.

Thanks for your quick replying. Expecting soon.

@Vadivel_S_S,

Please see this output document:
awjava-18.2.zip (1.6 MB)

Please create an expected Word document from it by using MS Word that shows the expected behavior, ZIP it and attach it here for our reference. Thanks for your cooperation.

@awais.hafeez

We have attached that expected output. Please give solution for this one.

Exp_OP : Expected_OP.zip (1.7 MB)

@ssvel,

We can suggest you a simpler way to insert numbers on the top left side of each Page. Please see these [input.zip (2.6 MB) output.zip (1.6 MB)] documents and try using the following code:

private static void insertText(Document doc) throws Exception {
    Shape shape = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT);

    shape.setLeft(36);
    shape.setTop(57);
    shape.setWidth(72);
    shape.setHeight(36);

    shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
    shape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
    shape.setWrapType(WrapType.NONE);

    shape.appendChild(new Paragraph(doc));

    DocumentBuilder builder = new DocumentBuilder((doc));
    builder.moveTo(shape.getFirstChild());
    builder.insertField(FieldType.FIELD_PAGE, false);

    Paragraph shapePara = new Paragraph(doc);
    shapePara.appendChild(shape);

    for (Section sect : doc.getSections()) {
        insertIntoHeader(shapePara, sect, HeaderFooterType.HEADER_PRIMARY);
        insertIntoHeader(shapePara, sect, HeaderFooterType.HEADER_FIRST);
        insertIntoHeader(shapePara, sect, HeaderFooterType.HEADER_EVEN);
    }
}

private static void insertIntoHeader(Paragraph shapePara, Section sect, int headerType) throws Exception {
    HeaderFooter header = sect.getHeadersFooters().getByHeaderFooterType(headerType);

    if (header == null) {
        header = new HeaderFooter(sect.getDocument(), headerType);
        sect.getHeadersFooters().add(header);
    }

    header.appendChild(shapePara.deepClone(true));
}

Document doc = new Document("D:\\Temp\\Sample\\sample.doc");
insertText(doc);
doc.save("D:\\Temp\\Sample\\awjava-18.4.doc");