Free Support Forum - aspose.com

After and Befor text for Commented context

Hello Support

Please find an attached document ExtractAfterbeforSample.zip (10.5 KB)
, It has comment on context, I want to extract 10 characters before the context and 10 characters after the context.
How to do this?

Thanks in advance.

Regards
Mandar

@mandar_limaye,

Thanks for your inquiry. Could you please share your expected output here for our reference? We will then provide you more information about your query along with code.

Hello Tahir,

I want to output as

sBeforeText=raph1 This
sContextText=parag
sAfterText= raph2 This

And rich text content to be saved into files sBeforeText.rtf having richtext content for same and like wise for sConextText and sAfterText.

We do not have any problem to extract context text using startcommentnode and endcommentnode.

Regards
Mandar

@mandar_limaye,

Thanks for sharing the detail. We suggest you please check the attached DOM image of your document. DOM.png (14.7 KB)

In your case, you need to get the text before CommentRangeStart and after CommentRangeEnd nodes. Please use the following code example to get the desired output.

Document doc = new Document(MyDir + "ExtractAfterbeforSample.docx");
Comment comment = (Comment)doc.GetChild(NodeType.Comment, 0, true);

String sBeforeText = "";
String sAfterText = "";
Paragraph ParentPara = comment.ParentParagraph;
Node node = ParentPara.GetChild(NodeType.CommentRangeStart, 0, true);

while (node.NodeType != NodeType.Body || sBeforeText.Length < 10)
{
    node = node.PreviousPreOrder(doc);
    if (node != null && node.NodeType == NodeType.Paragraph)
        sBeforeText = " " + sBeforeText;

    if (node.NodeType == NodeType.Run)
    {
        sBeforeText = ((Run)node).Text + sBeforeText;
    }
                     
}

node = ParentPara.GetChild(NodeType.CommentRangeEnd, 0, true);

while (node !=null || sAfterText.Length < 10)
{
    node = node.NextPreOrder(doc);
    if (node != null &&  node.NodeType == NodeType.Paragraph)
        sAfterText += " ";
    if (node != null && node.NodeType == NodeType.Run && node.GetAncestor(NodeType.Comment) == null)
        sAfterText += ((Run)node).Text;

}

Console.WriteLine(sBeforeText.Substring(sBeforeText.Length-11));
Console.WriteLine(sAfterText.Substring(0, 11));

Hello Tahir,
This is working great.
There is one additional need is there and I am using the same approach to know in which bookmark region comment has been made.
Please find attached document ExtractAfterbeforSample2.zip (10.8 KB)
I am trying to find out that comment1 is Bk1_S and Bk1_E range.
Any suggestion will be helpful.

Thanks in advance.

Regards
Mandar

@mandar_limaye,

Thanks for your inquiry. In this case, you need to iterate over nodes between bookmarks. Please check the following code example. We suggest you please read following article.
Aspose.Words Document Object Model

Document doc = new Document(MyDir + "ExtractAfterbeforSample2.docx");
Bookmark Bk_1S = doc.Range.Bookmarks["Bk_1S"];
Bookmark Bk_1E = doc.Range.Bookmarks["Bk_1E"];

Boolean blnFound = false;
if (Bk_1S != null && Bk_1E != null)
{
    Node currentNode = Bk_1S.BookmarkStart;
    Console.WriteLine(currentNode.NodeType);
    while (currentNode != Bk_1E.BookmarkEnd)
    {
        currentNode = currentNode.NextPreOrder(doc);
        if (currentNode.NodeType == NodeType.Comment)
        {
            blnFound = true;
            break;
        }
    }

    if (blnFound)
        Console.WriteLine(currentNode.GetText());
}

Hello
Thank you for suggestion. This works when there is only one comment in given region.
Generally in our case we have mutliple comments and and comment contexts are overlapping.
So I am using it other way i.e. looping for each comment then finding start bookmark.

Regards
Mandar

@mandar_limaye,

Thanks for your feedback. It is nice to hear from you that you have found the solution of your query. If you still face problem, please share your input document and expected output here for our reference. We will then provide you more information about your query.