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.

Fri Jun 10, 2022 8:20 am

I search with findAllPattern in a document where I want to locate two marks, one at the beginning (eg: <start>) and another at the end (eg: <end>) and I want to eliminate any content between those two marks, whether they are paragraphs, images, tables , etc.

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

Fri Jun 10, 2022 8:56 am

Hello,

Such as another case, we can also use that code to get all objects from two marks and then delete all of them. We only need to add one line of code.

Code: Select all
        //....
        //.... (The same code as in the previous case is omitted.)
        //Get bookmark content
        BookmarksNavigator navigator = new BookmarksNavigator(doc);
        navigator.moveToBookmark("test_bookmark");

        TextBodyPart body = navigator.getBookmarkContent();
        //Now you can get all objects between the keywords from body.getBodyItems()
        //*****************Change******************//
        //Delete all objects in the body
        body.getBodyItems().clear();
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Mon Jun 13, 2022 6:22 pm

I have tried the code you indicate but it doesn't work for me. It does not give any error but the resulting document is without changing anything. Attached word document and java source used for the test.

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

Tue Jun 14, 2022 2:02 am

Hi,

Please test the modified code below.
Code: Select all
        //.....
        //TextBodyPart body = navigator.getBookmarkContent();
        //body.getBodyItems().clear();

        //Replace the current body with an empty one to delete all content between the two marks
        TextBodyPart body=new TextBodyPart(document);
        navigator.replaceBookmarkContent(body);
        //....
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Tue Jun 14, 2022 1:20 pm

The code works fine, but it seems if the marks are in the same paragraph it doesn't work well. I need a code that works for marks in the middle of a paragraph, a whole paragraph, multiple paragraphs or with multiple contents.
Attached word document used for testing

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

Wed Jun 15, 2022 1:54 am

Hello,

Thanks for your feedback.

I did notice that the previous code didn't work when two marks that were in a same paragraph. So we can handle this case separately. Please try the modified code below.
Code: Select all
        Document document = new Document();
        document.loadFromFile("PruebaWord2.docx");

        TextSelection startSelection = document.findPattern(Pattern.compile("<if-test>",Pattern.CASE_INSENSITIVE));
        TextSelection endSelection = document.findPattern(Pattern.compile("<\\/if-test>",Pattern.CASE_INSENSITIVE));

        TextRange startRange = startSelection.getAsOneRange();
        TextRange endRange = endSelection.getAsOneRange();
        Paragraph startPara = startRange.getOwnerParagraph();
        Paragraph endPara = endRange.getOwnerParagraph();
        int startIndex = startPara.getChildObjects().indexOf(startRange);
        int endIndex = endPara.getChildObjects().indexOf(endRange);
        //If start and end marks are in a same paragraph, use a loop to remove other objects between them
        if(startPara.equals(endPara)){
            for(int i=startIndex+1,j=endIndex;i<j;){
                startPara.getChildObjects().removeAt(i);
                j--;
            }
        }else{
            //Otherwise, use an empty body for replacing
            BookmarkStart bookmarkStart = new BookmarkStart(document, "test_bookmark");
            BookmarkEnd bookmarkEnd = new BookmarkEnd(document, "test_bookmark");
            if (startIndex == startPara.getChildObjects().getCount() - 1) {
                startPara.getChildObjects().add(bookmarkStart);
            } else {
                startPara.getChildObjects().insert(startIndex + 1, bookmarkStart);
            }
            endPara.getChildObjects().insert(endIndex, bookmarkEnd);
            BookmarksNavigator navigator = new BookmarksNavigator(document);
            navigator.moveToBookmark("test_bookmark");

            //Replace the current body with an empty one to delete all content between the two marks
            TextBodyPart body=new TextBodyPart(document);
            navigator.replaceBookmarkContent(body);
            //Remove the bookmark
            startPara.getChildObjects().remove(bookmarkStart);
            endPara.getChildObjects().remove(bookmarkEnd);
        }
        document.saveToFile("PruebaWord-procesado.docx", FileFormat.Docx);
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Wed Jun 15, 2022 8:50 am

I found an entry in this forum where there is some code that I have used and it works fine for what I need. Thank you anyway.

removing-text-between-two-keywords-t6845.html?hilit=text%20from%20two%20ranges

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

Thu Jun 16, 2022 8:54 am

OK. Wish you doing well.
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Return to Spire.Doc