How to find text inside a content control Aspose.words C#

Hi,

I have created a word template using content controls. I am trying to delete a row based on the text inside content control. Suppose text inside custom control is "REMOVEROW" I want this row to be deleted from my document. Problem is I am unable to fine the text which I set inside the content control. How can I get this text "REMOVEROW" so that I can delete the row.

Thanks,

Ashish

Hi Ashish,

Thanks for your inquiry. You can achieve your requirement by implementing IReplacingCallback interface. Please use the same approach shared at following documentation link to find text and remove the table's row. See the highlighted code snippet below.
http://www.aspose.com/docs/display/wordsnet/How+to+Find+and+Highlight+Text

Please read the following article about 'Find and Replace' and check the following code example for your kind reference.
http://www.aspose.com/docs/display/wordsnet/Find+and+Replace+Overview

Hope this helps you. Please let us know if you have any more queries.

private class FindandRemoveRow : IReplacingCallback

{

///

/// NOTE: This is a simplistic method that will only work well when the match

/// starts at the beginning of a run.

///

ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)

{

if (e.MatchNode.GetAncestor(NodeType.Row) != null)

{

Node node = e.MatchNode.GetAncestor(NodeType.Row);

node.Remove();

}

e.Replacement = "";

return ReplaceAction.Replace;

}

}


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

doc.Range.Replace(new Regex("Remove"), new FindandRemoveRow(), false);

doc.Save(MyDir + "Out.docx");