Free Support Forum - aspose.com

Insert Page number problem

Dear Team,

We have problem with insert page number in document. i have attached documents Please find and give as solution.

Input : input.zip (147.5 KB)

FYI : Problem in second page.

Current OP : Currenyt_OP.zip (169.2 KB)

Exp OP : Exp_OP.zip (148.1 KB)

My current code : code.zip (609 Bytes)

@ssvel,

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

Thank you. And also the page number insert in inside of the equations. I need to insert the page number in each page start position.

@ssvel,

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

@ssvel,

As mentioned in your other thread, a much simpler technique is the one mentioned in the following article.

How to Add a Watermark to a Document

@awais.hafeez

Thanks for your quick reply. Here, page number was inserted in header part. But we need to insert in body of each page at the starting line(i.e. the starting point of the first line).

@ssvel,

Sure, you can find a node on each page inside the Body story and anchor the textbox shape containing the PAGE field to it. Instead of inserting it inline with content, you just need to float the shape to a fixed top-left position by using the code like bellow.

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

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

@awais.hafeez

Thanks for your quick response. Its working fine as expected in Microsoft office word 2013 . But the same scenario is not applicable for Microsoft office word 2007.

Please provide solution for above scenario.

@ssvel,

Please try specifying the following option:

doc.getCompatibilityOptions().optimizeFor(MsWordVersion.WORD_2007);

In case the problem still remains, please ZIP and upload Aspose.Words 18.4 generated output document showing the undesired behavior here for testing.

@awais.hafeez

Its not working. We have attached input and expected output.

Input : input.zip (890.2 KB)

Expected OP :Exp.zip (876.1 KB)

Current OP : Curr_OP.zip (869.4 KB) (We need to insert content in body of the part(Starting line))

please provide solution ASAP.

@ssvel,

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

@ssvel,

You can get idea from the following draft code as to how to find a node on each page inside the Body story and anchor the textbox shape containing the PAGE field to it. Instead of inserting it inline with content, you just need to float the shape to a fixed top-left position.

Document doc = new Document("D:\\Temp\\input\\input.docx");

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 (smallRun.getAncestor(NodeType.OFFICE_MATH) == null) {
        if (collector.getStartPageIndex(smallRun) == pageIndex) {
            builder.moveTo(smallRun);
            builder.startBookmark("bm_" + pageIndex);
            builder.endBookmark("bm_" + pageIndex);

            pageIndex++;
        }
    }
}

for(Bookmark bm : doc.getRange().getBookmarks()){
    PageSetup ps = ((Section) bm.getBookmarkStart().getAncestor(NodeType.SECTION)).getPageSetup();

    Shape shape = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT);
    shape.isLayoutInCell(false);

    shape.setLeft(48);
    shape.setTop(78);
    shape.setWidth(36);
    shape.setHeight(36);

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

    shape.appendChild(new Paragraph(doc));

    builder.moveTo(shape.getFirstChild());
    builder.insertField(FieldType.FIELD_PAGE, false);

    builder.moveTo(bm.getBookmarkStart());
    builder.insertNode(shape);
}

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

doc.save("D:\\Temp\\input\\awjava-18.4.docx");

@awais.hafeez

Thanks for your response. It’s working fine. But some pages are collapsed. We have attached sample for your reference.

FYI : Page no 6 and 8 are collapsed.

Current OP : Out.zip (941.3 KB)

@ssvel,

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

@ssvel,

Regarding the input.docx that you attached in this post, Aspose.Words for Java is currently not able to correctly render content to PDF. Starting from 6th page in Aspose.Words for Java 18.4 PDF, the content is shifted downwards to the next pages which cause this issue. For the sake of correction, we have logged this problem in our issue tracking system. The ID of this issue is WORDSNET-16784. We will further look into the details of this problem and will keep you updated on the status of this issue. We apologize for your inconvenience.

The issues you have found earlier (filed as ) have been fixed in this update. This message was posted using BugNotificationTool from Downloads module by MuzammilKhan