Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Tue May 31, 2022 4:49 pm

I use findAllPatern to search for patterns in a word document. I need to remove the entire paragraph where I have found patterns.
I have been able to empty them, clean them or replace them with an empty string, but I don't see how to completely eliminate the paragraph, the paragraph break always remains in the final document.

Thanks.

jmparada
 
Posts: 47
Joined: Wed May 25, 2022 7:50 am

Wed Jun 01, 2022 10:05 am

Hello,

Thanks for your inquiry.
Please refer to the following code to remove the paragraph where the search results are located. If there is any other question, please feel free to write back.
Code: Select all
Document document = new Document();
document.LoadFromFile(@"input.docx");
Regex regex = new Regex("regex");
TextSelection[] selections = document.FindAllPattern(regex);
foreach (TextSelection selection in selections)
{
    TextRange range = selection.GetAsOneRange();
    Paragraph paragraph = range.OwnerParagraph;
    Body body = paragraph.OwnerTextBody;
    body.ChildObjects.Remove(paragraph);
}
String result = "Result.docx";
document.SaveToFile(result, FileFormat.Docx2013);

William
E-iceblue
support team
User avatar

William.Zhang
 
Posts: 196
Joined: Mon Dec 27, 2021 2:23 am

Wed Jun 01, 2022 4:28 pm

Thanks for the reply. I have tried the code but something is wrong. When I am going to use the body.getChildObjects.Remove(paragraph); method, the Remove method does not exist with parameter paragraph, only remove(IDocumentObject entity) or removeAt(int index) seem to be available.

jmparada
 
Posts: 47
Joined: Wed May 25, 2022 7:50 am

Thu Jun 02, 2022 2:20 am

Hello,

Thanks for your response.
Kindly note that the Paragraph class is the implementation class of the interface IDocumentObjct, so the remove(IDocumentObject entity) method can receive a parameter of type Paragraph. Please refer to the previous code to retest, If it doesn't work, to help us investigate further, please provide us with your full test code and your input Word file. Thanks in advance.

William
E-iceblue
support team
User avatar

William.Zhang
 
Posts: 196
Joined: Mon Dec 27, 2021 2:23 am

Thu Jun 02, 2022 9:00 am

Java keeps saying there is an error in the remove method call. The error message is: "The method Remove(Paragraph) is undefined for the type DocumentObjectCollection".

The code is:

paragraph = range.getOwnerParagraph();
body = paragraph.ownerTextBody();
body.getChildObjects().Remove(paragraph);

jmparada
 
Posts: 47
Joined: Wed May 25, 2022 7:50 am

Fri Jun 03, 2022 4:21 am

Hello,

Thanks for your feedback.
I tested the following java code with our latest Spire.Doc for Java Version:10.5.10, but I didn't encounter your issue. Here I also attached my testing files for your reference. If you were using an old version, I suggest you upgrade to the latest one and try again, if your issue still exists after trying, please share your input file with us for further testing. You also can send it to us via email(support@e-iceblue.com).
Code: Select all
        Document doc = new Document();
        doc.loadFromFile("in.docx");
        Pattern regex=Pattern.compile("\\#[^\\#]+\\#");
        TextSelection[] textSelections = doc.findAllPattern(regex, true);
        for (TextSelection selection : textSelections)
        {
            TextRange range = selection.getAsOneRange();
            Paragraph paragraph = range.getOwnerParagraph();
            Body body = paragraph.ownerTextBody();
            body.getChildObjects().remove(paragraph);
        }
        doc.saveToFile("final.docx", FileFormat.Docx_2013);

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1261
Joined: Wed Apr 25, 2018 3:20 am

Fri Jun 03, 2022 3:15 pm

Works. I have downloaded the indicated version and now it has gone well. Thank you

jmparada
 
Posts: 47
Joined: Wed May 25, 2022 7:50 am

Mon Jun 06, 2022 6:25 am

Hello,

Thanks for your response.
Glad to hear that it works. If you encounter any issues related to our products in the future, just feel free to contact us.

Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 196
Joined: Mon Dec 27, 2021 2:23 am

Return to Spire.Doc