Find the specific word in the paragraph and copy that paragraph in Aspose.Words c#

I have document which contains all paragraph ,bullets, image’s, Table. In that document 1. I need Find the specific text in paragraph that text should be start with the specific word i needed of the paragraph and copy that paragraph to other document 2. Find the specific text in paragraph and copy that paragraph to other document.

Please find the input word document and expected output document.
Input word document.docx (35.7 KB)

Expected Output word document.docx (2.3 KB)

@Yuvarajshivananjappa You can either loop through paragraphs in your document and check whether text of paragraphs starts/contains particular text. For example see the following code:

NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);
foreach (Paragraph p in paragraphs)
{
    string paraText = p.ToString(SaveFormat.Text).Trim();
    // You can use StartsWith, Contains, EndsWith methods or Regular expression to check paragraph text.
    if (paraText.StartsWith("Some text"))
    {
        // Do something with the paragraph
        // ......
    }
}

Or you can use Find and Replace feature and IReplacingCallback to collect the paragraphs that contain matched text. For example:

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

ParagraphsCollector collector = new ParagraphsCollector();
FindReplaceOptions options = new FindReplaceOptions();
options.ReplacingCallback = collector;
doc.Range.Replace("Text you are interested in", "", options);

foreach (Paragraph p in collector.MatchedParagraphs)
{
    // Do something with paragraphs
    // ....
}
private class ParagraphsCollector : IReplacingCallback
{
    public ReplaceAction Replacing(ReplacingArgs args)
    {
        Paragraph p = args.MatchNode.NodeType == NodeType.Paragraph 
            ? (Paragraph)args.MatchNode 
            : (Paragraph)args.MatchNode.GetAncestor(NodeType.Paragraph);

        if (!mMatchedParagraphs.Contains(p))
            mMatchedParagraphs.Add(p);

        // Do nothing since we simply collect paragraphs.
        return ReplaceAction.Skip;
    }

    public List<Paragraph> MatchedParagraphs
    {
        get { return mMatchedParagraphs; }
    }

    private readonly List<Paragraph> mMatchedParagraphs = new List<Paragraph>();
}