Thanks for the reply, I had written almost the same code for my requirement, but this fails if we have same search terms existing in multiple pages.
Ex - Lets consider we have a List in which string is our search term present in document.
Lets say we have below search terms -
[0] - “aspose” - this is in first page in the document
[1] - “family” - this is in first page in the document
[2] - “eduardo” - this is in first page in the document
[3] - “aspose” - this is in second page in the document
[4] - “chetan” - this is in second page in the document
[5] - “aspose” - this is in second page in the document
In the above list, search term “aspose” is repeated thrice in the document and using the code that you have shared the callback.ElemPages will be formed as shown in below screenshot-
image.png (41.6 KB)
Since my search terms are only 6 and I would need the page numbers of only these search terms. We are getting 12 items in the callback.ElemPages list and this is because ReplacingCallback is called thrice for each occurrence of word “aspose” as it exists three time in the document.
In Office.Word.Interop libraries we were able to find the Range of particular word or text in the document which is the position of the search term (“aspose”) in document and search term’s Range’s start values will be like - first occurrence is at 244, 2nd occurrence is at 1244 and 3rd occurrence is at 2455.
I was looking for some way in Aspose to pass the range (position) of the particular search term and find the page number of it so that I would get the exact page number of search terms. I think we don’t have such option in Aspose. Please let me know if we have this.
My search terms list is ordered as in the order of appearance in the document. So, I tried below approach.
For first “aspose” term in the search list, ReplacingCallback will be called thrice since it has 3 occurrences in the document. In first call I am finding the page number of first “aspose” node and trying to insert Field element like builder.MoveTo(currentNode); builder.InsertField(“SkipSearch”); and adding return ReplaceAction.Stop. So that we skip 2 mores callbacks for the same search term and go to the next item searchlist[1] (“family”) in search list.
When it reaches searchlist[3] i.e., second occurrence of “aspose”, I am checking if the previous sibling is Field element like this - if (currentNode.PreviousSibling.NodeType == NodeType.FieldEnd) this way I can avoid the node whose page number is already calculated.
But in the above approach when I insert Field element the page numbers of search terms are getting changed because when this code - builder.InsertField(“SkipSearch”) is executed the user document is altered and also its page number.
Please let me know which would be the best way to mark the node whose page number is already calculated and at the same time we need to make sure that page numbers of search terms are not getting altered due to adding something to the document.
I tried my best to explain the scenario, please let me know if you have any queries. Thanks.