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

Free Support Forum - aspose.com

Delete Table Row By Tag


We are trying to Delete table rows by using tags(Keyword) which is present in the Table cell. We tried below code and it is working, we need to add some condition with this code,

           Document doc = new Document(_dataDir + "SampleDocWithTableAndPara.docx");

            Regex regex = new Regex("<REMOVE_ROW>", RegexOptions.IgnoreCase);

            FindReplaceOptions options = new FindReplaceOptions
                Direction = FindReplaceDirection.Backward,
                ReplacingCallback = new ReplaceEvaluatorFindAndDeleteRow()

            doc.Range.Replace(regex, "", options);

Case 1: If tag(Keyword) is present in the Table, We should delete the row If all cells in the row are empty, otherwise we should not delete the the row, just delete the tag.

Case 2 : If tag(Keyword) is present in the Paragraph,
a) Delete the line / row, if line / row is empty,
b) Just delete the tag if line / row is not empty,

Please help me for find the proper solution for above cases.
Here attached the sample console application and sample input / output and expected output for your reference.
DeleteTableRowByTag.zip (5.7 MB)

Thanks and Regards,


In your case, you need to check the text of paragraph and table row. If the row text or paragraph text is equal to <REMOVE_ROW>, please remove the row and paragraph. You can use following modified code to get the desired output.

ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
    Node currentNode = e.MatchNode;
    // The first (and may be the only) run can contain text before the match,
    // in this case it is necessary to split the run.
    if (e.MatchOffset > 0)
        currentNode = SplitRun((Run)currentNode, e.MatchOffset);

    // This array is used to store all nodes of the match for further highlighting.
    ArrayList runs = new ArrayList();

    // Find all runs that contain parts of the match string.
    int remainingLength = e.Match.Value.Length;
    while (
    (remainingLength > 0) &&
    (currentNode != null) &&
    (currentNode.GetText().Length <= remainingLength))
        remainingLength = remainingLength - currentNode.GetText().Length;

        // Select the next Run node.
        // Have to loop because there could be other nodes such as BookmarkStart etc.
            currentNode = currentNode.NextSibling;
        while ((currentNode != null) && (currentNode.NodeType != NodeType.Run));

    // Split the last run that contains the match if there is any text left.
    if ((currentNode != null) && (remainingLength > 0))
        SplitRun((Run)currentNode, remainingLength);

    //modified code start...

    Row row = (Row)((Run)runs[0]).GetAncestor(NodeType.Row);

    if (row != null && row.ToString(SaveFormat.Text).Trim() == "<REMOVE_ROW>")
    else if ((((Run)runs[0]).ParentParagraph).ToString(SaveFormat.Text).Trim() == "<REMOVE_ROW>")

    //modified code end...
    return ReplaceAction.Skip;

HI @tahir.manzoor

Thanks for your quick support, Your suggestion is working for our use cases.

Thanks and Regards,