Free Support Forum - aspose.com

How to get the char position of the text where a comment is put

Hi,
Here’s the situation:
I have a document where there is comment put on a text, as shown below (comment is put across the word ‘sample’:

----RTF content start ----

This is a [sample] document

—RTF content end ----

Now, I need to get the char position of where the comment was put. Say for eg, in the above document, i should get 10. In MS Word automation, i could get the Start pos from Range object. Range.Start but there is nothing like that in Aspose API.

Please help.
~Rosh

Hi

Thanks for your request. There is no direct way to determine index of the comment range start. However, you can calculate it yourself. You should just find a CommentRangeStart node associated with a particular Comment:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/aspose.words.commentrangestart.html

Also, could you please explain why you need to know this position? Maybe there is a simpler way to achieve what you need.

Best regards,

Hi Alexey,
Thanks a lot for the reply.

I intend to get the full sentence where the comment is put. Is this possible through some API?. I am pretty new to Aspose. Hence it would be gr8 if you can provide some samples on how to use CommentRangeStart object. (my understanding was CommentRangeStart was for writing comments)

~Rosh K. M.

Hi

Thanks for your request. Here is a simple code example, which allows insertion commented range into the document:

// Create an empty document and DocumentBuilder object.

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

// Create a Comment.

Comment comment = new Comment(doc);

// Insert some text into the comment.

Paragraph commentParagraph = new Paragraph(doc);

commentParagraph.AppendChild(new Run(doc, "This is comment!!!"));

comment.AppendChild(commentParagraph);

// Create CommentRangeStart and CommentRangeEnd.

int commentId = 0;

CommentRangeStart start = new CommentRangeStart(doc, commentId);

CommentRangeEnd end = new CommentRangeEnd(doc, commentId);

// Insert some text into the document.

builder.Write("This is text before comment ");

// Insert comment and comment range start.

builder.InsertNode(comment);

builder.InsertNode(start);

// Insert some more text.

builder.Write("This is commented text ");

// Insert end of comment range.

builder.InsertNode(end);

// And finaly insert some more text.

builder.Write("This is text after comment");

// Save output document.

doc.Save("out.doc");

Best regards,

Hi Andrey,
Thanks for the reply. What I really needed was to get back the sentence where the comment was put.

Here in the above case of ‘out.doc’, Can I get the sentence ‘This is the text before comment’, using the comment object or CommentRangeStart object. ? if not, is there any other way?

I originally thought to get it from the Index ( char position of comment) and build back the sentence.

~Rosh K M

Hello

Thanks for your inquiry. Aspose.Words represents a document as a tree of nodes. An integral feature of the tree is the ability to navigate between the nodes. You can inspect structure of Word document using DocumentExplorer (Aspose.Words demo application). Please follow the link to learn more:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/document-tree-navigation.html

DocumentExplorer is included to Aspose.Words installation package. You can find it here:

C:\Program Files (x86)\Aspose\Aspose.Words for .NET\Demos\CSharp\DocumentExplorer

Please check the attached screenshot to see how Aspose.Words represents CommentRangeStart/ CommentRangeEnd.

So using the following code you can get CommentRangeStart/ CommentRangeEnd nodes.

Document doc = new Document("C:\\Temp\\in.doc");

//Get CommentRangeStart and CommentRangeEnd

CommentRangeStart start = (CommentRangeStart)doc.GetChild(NodeType.CommentRangeStart, 0, true);

CommentRangeEnd end = (CommentRangeEnd)doc.GetChild(NodeType.CommentRangeEnd, 0, true);

Then you can try using PreviousSibling, NextSibling, ParentNode etc. to get particular nodes.

Best regards,

Thanks a lot Andrey. That was really helpful.

~Rosh K M