Find position of text in document

Hello,

I’m fighting an issue I have here (PDF text replace not wrapping to a new line) and until a fix is implemented, I’m trying to approach my problem another way.

My goal is to find and replace text in a document. Then after I have gone through the document and replaced all text I wish to, I need to then go back through the document and find the position of other text. The reason I don’t know the position of the text up front is because during the find/replace process it could have shifted other text around, so I need to find the position of the text AFTER I have performed my find and replace.

Aspose.Pdf has a way to find the position of text, but has a bug with find/replace (again, refer to thread link above). Aspose.Word seems to replace text and re-arrange text as I expect, but does not seem to let me find the position of a word.

Is there a way I can get Aspose.Word to find/replace text in a document AND find the position of it? Either pixel position, or the relative position on the page. I really don’t care which, I just need one!

Hi Patrick,

Thanks for your inquiry. We suggest you please read the input file format for Aspose.Words and find and replace article from here:

LoadFormat Enumeration
Find and Repalce

The Aspose.Words.Layout namespace provides classes that allow to access information such as on what page and where on a page particular document elements are positioned, when the document is formatted into pages.

In your case we suggest you following solution.

  1. Implement IReplacingCallback interface
  2. Move the cursor to the matched node and insert a bookmark
  3. Get the BookmarkStart node and use following code snippet to get the desired location.

Hope this helps you.

LayoutCollector layoutCollector = new LayoutCollector(doc);
LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);
// Your code... 
BookmarkStart bmStart = ....;
var renderObject = layoutCollector.GetEntity(bmStart);
layoutEnumerator.Current = renderObject;
RectangleF location = layoutEnumerator.Rectangle;