We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Get particular string's paragraph index without iterating

Hi,
I have requirement, I want to find particular string (lets say “Abstract:” given in attached screenshot) without iterating paragraph & get Index of that paragraph(lets say abstractIndex).
So if, I want to access “Status:” paragraph i will not again search I will find it from index of “Abstract” as abstractIndex+1.
This I wanted to do because, in future if anybody added any paragraph before “Abstract” then the index for Status will change. so to avoid this i wanted to make it dynamic.
So is there anyway to do it with aspose.

Paragraph.PNG (43.1 KB)

@komalm2921

We suggest you please implement IReplacingCallback interface as shown below to get the index of desired paragraph. Please read the following article.

Find and Replace

var document = new Document(MyDir + "in.docx");

FindReplaceOptions findReplaceOptions = new FindReplaceOptions();
findReplaceOptions.ReplacingCallback = new FindParagraphIndex();
document.Range.Replace("Abstract", "", findReplaceOptions);

class FindParagraphIndex : IReplacingCallback
{
    public int index;
    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
    {

        Node currentNode = e.MatchNode;
        Paragraph paragraph = (Paragraph)currentNode.ParentNode;
        index = paragraph.Document.GetChildNodes(NodeType.Paragraph, true).IndexOf(paragraph);
        Console.WriteLine(index);
        return ReplaceAction.Skip;
    }
}

@tahir.manzoor
Thank you for the solution. It works for me.