Sample Code:
public static Bookmark insertBookmarkAroundNode(Node node, boolean updateLayout)
{
CompositeNode parentNode = node.getParentNode();
if (Objects.isNull(parentNode))
{
return null;
}
String bookmarkName = UUID.randomUUID().toString();
BookmarkStart bookmarkStart = new BookmarkStart(parentNode.getDocument(), bookmarkName);
BookmarkEnd bookmarkEnd = new BookmarkEnd(parentNode.getDocument(), bookmarkName);
parentNode.getChildNodes().insert(0, bookmarkStart);
parentNode.getChildNodes().add(bookmarkEnd);
if (updateLayout)
{
updatePageLayoutForTheDoc((Document)parentNode.getDocument());
}
return bookmarkStart.getBookmark();
}
- From above sample code “updatePageLayoutForTheDoc” ultimately calling “document.updatePageLayout()” of Aspose.words and this particular method is taking around 1 to 2 seconds for execution.
- If Document has too many mathematical equations then this processing time is in too many minutes.
- Do we know how we can optimise this?
- We are using “22.8” version of Aspose, we tried by using latest version of Aspose as well, after upgrading it was taking more time than current one so reverted that.
@ELSSAM_elsevier_com Document.updatePageLayout
is quite time and resource consuming operation. When you call this method Aspose.Words rebuilds layout of the whole document. So it is not recommended to call this method if it is not really required.
Usually Document.updatePageLayout
should be called once, for example when TOC field page numbers should be updated. Could you please elaborate why you call Document.updatePageLayout
after inserting a bookmark? Adding bookmark does not change the document’s layout, so there is no need to call this method.
@ELSSAM_elsevier_com
- In this case you can first insert all bookmarks and then call
Document.updatePageLayout
once.
- Sure, you can get all
OfficeMath
nodes from your document and get their count.
Please see the following code that demonstrates the technique for both questions:
Document doc = new Document("C:\\Temp\\in.docx");
// Get OfficeMath Nodes
NodeCollection<OfficeMath> maths = doc.getChildNodes(NodeType.OFFICE_MATH, true);
// Output the number of office math nodes in the document.
// note the returned value will include both top level and child OfficeMath nodes.
System.out.println(maths.getCount());
// Wrap top level OfficeMath nodes into bookmarks.
int index = 0;
ArrayList<String> mathBookmakrs = new ArrayList<String>();
for (OfficeMath math : maths)
{
if (math.getAncestor(NodeType.OFFICE_MATH) == null)
{
String bkName = "math_bookmark_" + index;
index++;
math.getParentNode().insertBefore(new BookmarkStart(doc, bkName), math);
math.getParentNode().insertAfter(new BookmarkEnd(doc, bkName), math);
mathBookmakrs.add(bkName);
}
}
// Output number of top level OfficeMath Nodes
System.out.println(index);
// Call update page layout and get layout information of TopLevel OfficeMaths.
// .....
1 Like