We're sorry Aspose doesn't work properply without JavaScript enabled.

# Adding Bookmark for Math equations in MS word documents taking too much time

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);
if (updateLayout)
{
updatePageLayoutForTheDoc((Document)parentNode.getDocument());
}
return bookmarkStart.getBookmark();
}

1. 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.
2. If Document has too many mathematical equations then this processing time is in too many minutes.
3. Do we know how we can optimise this?
4. 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.

1. We are extracting mathematical equations using officeMath for that we need Layout information like pageNumber etc, for that we are inserting bookMark and calling updateLayout().
2. Also is there anyway to get count of officeMath nodes from Document?

@ELSSAM_elsevier_com

1. In this case you can first insert all bookmarks and then call Document.updatePageLayout once.
2. 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);