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).
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.
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.
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");
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