Change attributes between two occurences

From NOA Documentation Wiki

Jump to: navigation, search

The following example will make the text between word1 and word2 bold, italic and font size 16:

// find word1
       SearchDescriptor searchDescriptor = new SearchDescriptor("word1");
       searchDescriptor.setIsCaseSensitive(false);
       /* cant use regular expression
        * (searchDescriptor.setUseRegularExpression(true))
        * because openoffice does not allow for regexes which break the line
        */

       ISearchResult searchResult = this.parentWizard.getDocument().getSearchService().findFirst(searchDescriptor);
       String itemText="";
       if(!searchResult.isEmpty()) {
           //...and now select the result
           ITextRange[] textRanges = searchResult.getTextRanges();
           ITextRange textRange=textRanges[0];
           // find word2
           SearchDescriptor searchDescriptor2 = new SearchDescriptor("word2");
           searchDescriptor.setIsCaseSensitive(false);
           ISearchResult searchResult2 = this.parentWizard.getDocument().getSearchService().findFirst(searchDescriptor2);
           if(!searchResult.isEmpty()) {
               //...and now select the result
               ITextRange[] textRanges2 = searchResult2.getTextRanges();
               ITextRange textRange2=textRanges2[0];
               XTextCursor textCursor=textRange.getXTextRange().getText().createTextCursor();
               ITextCursor cursor;
               try {
                   cursor = this.parentWizard.getDocument().getTextService().getText().getTextCursorService().getTextCursor();
// with the (invisible) text cursor, go, without marking (the false parameter), to the first occurence
                   cursor.gotoRange(textRange,false);
// now start marking (true) to the second occurence
                   cursor.gotoRange(textRange2,true);
//with sth like cursor.setString("new text"); you could also replace the text
                   cursor.getCharacterProperties().setFontBold(true);
                   cursor.getCharacterProperties().setFontItalic(true);
                   cursor.getCharacterProperties().setFontSize(16);
               } catch (TextException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
               }
             
           }
       }

   } 
Personal tools