How find a text in a Word document and get its x-y position


Please, could anybody tell me how is possible to find a text in a Word document (.doc) and, when the text is found, get its x,y position?

I need to know this information because once the document has been converted in PDF a digital signature process is executed with other component and it needs to print the signature in those coordinates, so I need to know them.


Hi José,

Thanks for your inquiry. Sure you can achieve this by implementing IReplacingCallback interface. In the IReplacingCallback.Replacing event, you can get a handle to the matching nodes and obtain their positions using the classes of Aspose.Words.Layout Namespace (see LayoutEnumerator.Rectangle property). Please let me know if I can be of any further assistance.

Best regards,

Sorry, I need an example to understand how that works. I’m totally lost…

Is there any example similar to your proposed solution in the Example package?

Hi José,

Thanks for your inquiry. Sure, please find attached a sample document and try executing the following code:

Document doc = new Document(@"C:\Temp\input.docx");
doc.Range.Replace(new Regex("FIND MY COORDIANTES"), new MyReplaceEvaluator(), true);
private class MyReplaceEvaluator: IReplacingCallback
    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
        DocumentBuilder builder = new DocumentBuilder((Document) e.MatchNode.Document);
        BookmarkStart start = builder.StartBookmark("temp");
        LayoutCollector collector = new LayoutCollector((Document) e.MatchNode.Document);
        LayoutEnumerator enumerator = new LayoutEnumerator((Document) e.MatchNode.Document);
        enumerator.Current = collector.GetEntity(start);
        Console.WriteLine("({0}, {1})", enumerator.Rectangle.Left, enumerator.Rectangle.Top);
        return ReplaceAction.Skip;

I hope, this helps.

Best regards,

A post was split to a new topic: Unable to Debug class Implementing IReplacingCallback