Remove the line in word file when certain text match

Hi all,
I need to delete a line in the word file that matches with certain text.

For example, I have a word file that contains the following text:

form.excelsection.name.Excel 13. end

I need that when the text is found, the line of the file is deleted completely

I was looking for examples on the forums but not found the answer.

We are using Aspose.Word 13.6.0.0

Best Regards.
Martin Fernandez
Java Deleloper

Hi Martin,

Thanks for your inquiry. Please attach the following resources here for testing:

- Your sample input Word document you want to remove line from.
- Your target Word document showing the desired final output. You can create this document using Microsoft Word.

We will investigate the scenario on our end and provide you code to achieve this.

Best regards,

Hi attached two files, source and target.

The idea is to remove the line with the following piece of example:

form.excelsection.name.Excel 1. end

Best Regards
Martin Fernandez

Hi Martin,

Thanks for your inquiry. You can use the following code to achieve this:

Document doc = new Document("C:\temp\template-aspose-source.docx");
Pattern regex = Pattern.compile("form.excelsection.name.Excel 1.end", Pattern.CASE_INSENSITIVE);
doc.getRange().replace(regex, new ReplaceEvaluatorFindAndremoveParagraph(), false);
doc.save("C:\Temp\out.docx");
static class ReplaceEvaluatorFindAndremoveParagraph implements IReplacingCallback
{
    public int replacing(ReplacingArgs e) throws Exception
    {
        Node currentNode = e.getMatchNode();
        if (currentNode.getNodeType() == NodeType.RUN)
        {
            ((Run) currentNode).getParentParagraph().remove();
        }
        return ReplaceAction.SKIP;
    }
}

I hope, this helps.

Best regards,

Hi Awais,
thanks for the help, with the code that you sent me the text is removed, but it is the blank line in the document, the idea is that this line is removed avoiding blank spaces.

If you could compare the attachments, you’ll see that the “template-aspose-target.docx” file, the line of the text that is removed, was also deleted.

Content of the file “template-aspose-source.docx”:

Line 1: form.excelsection.name.Excel54.end
Line 2: form.excelsection.name.Excel 1. end
Line 3: form.wordsection.name.Contenido.end

Content of the file “template-aspose-target.docx”:

Line 1: form.excelsection.name.Excel54.end
Line 2: form.wordsection.name.Contenido.end

The text “form.excelsection.name.Excel 1.end” is removed and also the line, without blank spaces.
Best Regards
Martin Fernandez
Java Developer

Hi Martin,

Thanks for your inquiry. Since you’re using an old version of Aspose.Words for Java, we suggest you please upgrade to the latest version i.e. 14.3.0. I hope, this helps.

The code in my previous post produces the output equal to your “template-aspose-target” document. I have attached an “out.docx” i.e. produced with 14.3.0 on my side here for your reference. Also, please check the attached screenshots. Please let me know if I can be of any further assistance.

Best regards,

Hi Awais,
thanks a lot, grateful for your help, we will then update to the last version.

Best Regards
Martin Fernandez

Hi Martin,

Sure. In case you have further inquires or need any help, please let us know.

Best regards,