title: find a text and apply character style using docx4j's native method
body: <p>I've already referred this link:<a href="https://stackoverflow.com/questions/36161718/apply-style-to-specific-text-in-a-styled-paragraph-using-docx4j">apply-style-to-specific-text-in-a-styled-paragraph-using-docx4j</a>. I've taken the other approach.
  The following are the steps I'm doing to find and apply character style using docx4j:</p>
  
  <ol>
  <li>Get the paragraph using bookmark</li>
  <li>Split the runs from that paragraph into character by character </li>
  <li>Finding the text in the split runs and applying the character style.</li>
  </ol>
  
  <p>For example, I'm searching for the text Table 1 in the following text:</p>
  
  <p>II &lt; 1 (very low-risk group). Population characteristics and the immediate postoperative adverse events in both groups are shown in Table 1. Among preoperative characteristics, there was no case of active endocarditis, dialysis or prior cardiac surgery (redo) in very low-risk patients. When comparing risk groups, low-risk patients were older, with higher prevalence of women, longer cardiopulmonary bypass and cross-clamping times, and less isolated coronary surgery than in very low-risk patients.</p>
  
  <p>Before splitting the run content:</p>
  
  <blockquote>
    <p>&lt;w:r w:rsidRPr="00F83849"&gt; &lt;w:rPr&gt; &lt;w:lang
    w:val="en-US"/&gt; &lt;/w:rPr&gt; &lt;w:t&gt;EuroSCORE&lt;/w:t&gt;
    &lt;/w:r&gt; &lt;w:proofErr w:type="spellEnd"/&gt; &lt;w:r
    w:rsidRPr="00F83849"&gt; &lt;w:rPr&gt; &lt;w:lang w:val="en-US"/&gt;
    &lt;/w:rPr&gt; &lt;w:t xml:space="preserve"&gt; II &lt; 1 (very
    low-risk group). Population characteristics and the immediate
    postoperative adverse events in both groups are shown in Table 1.
    Among preoperative characteristics, there was no case of active
    endocarditis, dialysis or prior cardiac surgery (redo) in very
    low-risk patients. When comparing risk groups, low-risk patients were
    older, with higher prevalence of women, longer cardiopulmonary bypass
    and cross-clamping times, and less isolated coronary surgery than in
    very low-risk patients.&lt;/w:t&gt; &lt;/w:r&gt;</p>
  </blockquote>
  
  <p>After Splitting(I'm just copying part of the text):</p>
  
  <blockquote>
    <p>&lt;w:r&gt;
                    &lt;w:rPr&gt;
                        &lt;w:lang w:val="en-US"/&gt;
                    &lt;/w:rPr&gt;
                    &lt;w:t xml:space="preserve"&gt;E&lt;/w:t&gt;
                &lt;/w:r&gt;
                &lt;w:r&gt;
                    &lt;w:rPr&gt;
                        &lt;w:lang w:val="en-US"/&gt;
                    &lt;/w:rPr&gt;
                    &lt;w:t xml:space="preserve"&gt;u&lt;/w:t&gt;
                &lt;/w:r&gt;
                &lt;w:r&gt;
                    &lt;w:rPr&gt;
                        &lt;w:lang w:val="en-US"/&gt;
                    &lt;/w:rPr&gt;
                    &lt;w:t xml:space="preserve"&gt;r&lt;/w:t&gt;
                &lt;/w:r&gt;
                &lt;w:r&gt;
                    &lt;w:rPr&gt;
                        &lt;w:lang w:val="en-US"/&gt;
                    &lt;/w:rPr&gt;
                    &lt;w:t xml:space="preserve"&gt;o&lt;/w:t&gt;
                &lt;/w:r&gt;
                &lt;w:r&gt;
                    &lt;w:rPr&gt;
                        &lt;w:lang w:val="en-US"/&gt;
                    &lt;/w:rPr&gt;
                    &lt;w:t xml:space="preserve"&gt;S&lt;/w:t&gt;
                &lt;/w:r&gt;
                &lt;w:r&gt;
                    &lt;w:rPr&gt;
                        &lt;w:lang w:val="en-US"/&gt;
                    &lt;/w:rPr&gt;
                    &lt;w:t xml:space="preserve"&gt;C&lt;/w:t&gt;
                &lt;/w:r&gt;
                &lt;w:r&gt;
                    &lt;w:rPr&gt;
                        &lt;w:lang w:val="en-US"/&gt;
                    &lt;/w:rPr&gt;
                    &lt;w:t xml:space="preserve"&gt;O&lt;/w:t&gt;
                &lt;/w:r&gt;
                &lt;w:r&gt;
                    &lt;w:rPr&gt;
                        &lt;w:lang w:val="en-US"/&gt;
                    &lt;/w:rPr&gt;
                    &lt;w:t xml:space="preserve"&gt;R&lt;/w:t&gt;
                &lt;/w:r&gt;
                &lt;w:r&gt;
                    &lt;w:rPr&gt;
                        &lt;w:lang w:val="en-US"/&gt;
                    &lt;/w:rPr&gt;
                    &lt;w:t xml:space="preserve"&gt;E&lt;/w:t&gt;
                &lt;/w:r&gt;
                &lt;w:proofErr w:type="spellEnd"/&gt;</p>
  </blockquote>
  
  <p>I'm able to extract runs of "<strong>Table 1</strong>" using the following code:</p>
  
  <pre><code>public static P ApplyCharStyleOccurBaseInParagraph(P p,ObjectFactory factory, String search,
          String CharStyle, boolean matchCase, int occurence) throws Exception 
  {
  
      String[] searchArray = search.split("(?!^)");
      int occurCount = 0;
  
      ClassFinder runs = new ClassFinder(R.class);
      new TraversalUtil(p,runs);
      ClassFinder texts = new ClassFinder(Text.class);
      new TraversalUtil(p,texts);
      StringBuilder sb = new StringBuilder();
  
      for (Object text : texts.results) 
      {
          sb.append(((Text)text).getValue());
      }
  
  
      if (sb.toString().contains(search)|| (!matchCase &amp;&amp; sb.toString().toUpperCase().contains(search.toUpperCase()))) 
      {
          System.out.println("Strings Match !!");
  
          boolean cont = false;
          for (int i = 0; i &lt;= (runs.results.size()) - searchArray.length; ++i)
          {
              boolean match = true;
              for (int c = 0; c &lt; searchArray.length; ++c)
              {
                  R CurrRunNode = (R) runs.results.get(i + c);
                  String textElement = "";
                  String nextTextElement = "";
                  R nextRunNode = null;
                  int nextRunIndex = c + 1;
  
                  ClassFinder Currtext = new ClassFinder(Text.class);
                  new TraversalUtil(CurrRunNode,Currtext);
  
  
  
                  if (Currtext.results.size()&gt; 0) 
                  {
                      for(Object txt : Currtext.results)
                      {
                          textElement += ((Text)txt).getValue();
                      }
                  }
  
                  if (textElement == "") 
                  {
                      ClassFinder currTempText = new ClassFinder(Text.class);
                      new TraversalUtil(CurrRunNode,currTempText);
                      if(CurrRunNode != null &amp;&amp; currTempText.results.size() == 0)
                      {
                          i++;
                          c--;
                          continue;
                      }
                      else
                      {
                          match = false;
                          cont = true;
                          break;
                      }
                  }
  
                  if (textElement.equals(searchArray[c].toString())) 
                  {
                      logger.info("Match String run index :"+((ContentAccessor) ((R) runs.results.get(i + c)).getParent()).getContent().indexOf((R) runs.results.get(i + c)));
                      if(nextRunIndex &lt; searchArray.length)
                      {
                          nextRunNode = (R) runs.results.get(i + nextRunIndex);
                          ClassFinder nextRuntext = new ClassFinder(Text.class);
                          new TraversalUtil(nextRunNode,nextRuntext);
  
                          if (nextRunNode != null &amp;&amp; nextRuntext.results.size()&gt; 0) 
                          {
                              /*For testing purpose*/
  
                              /*For testing purpose*/
                              for(Object txt1 : nextRuntext.results)
                              {
                                  nextTextElement += ((Text)txt1).getValue();
                              }
                          }
  
  
                          if(nextTextElement == "") {
                              //For testing purpose
                              if(nextRunNode != null &amp;&amp; nextRuntext.results.size() == 0)
                              {                                   
                                  continue;
                              }
                              else
                              {
                              match = false;
                              cont = true;
                              break;
                              }
                          }
                          if (nextTextElement.equals(searchArray[nextRunIndex].toString()))
                          {
                              continue;
                          }
                          else
                          {
                              match = false;
                              cont = true;
                              break;
                          }
                      }
                      else
                      {
                      continue;
                      }
                  }
  
                  if (!matchCase &amp;&amp; textElement.toUpperCase()
                          .equals(searchArray[c].toString().toUpperCase())) {
                      continue;
                  }
                  match = false;
                  cont = true;
                  break;
              }
  
  
              if (match) 
              {
                  if (occurence == occurCount) 
                  {
                      for (int c = 0; c &lt; searchArray.length; c++) 
                      {
                          R CurrRunNode = (R) runs.results.get(i + c);
                          RPr runProps = CurrRunNode.getRPr();
  
                          if (runProps != null) 
                          {
                              if(runProps.getRStyle() != null)
                              {
                                  runProps.getRStyle().setVal(CharStyle);
                              }
                              else
                              {
                                  RStyle rstyle = factory.createRStyle();
                                  rstyle.setVal(CharStyle);
                                  runProps.setRStyle(rstyle);
                              }
                          }
                          else if (runProps == null) 
                          {
                              RPr rPr = factory.createRPr();
                              RStyle rstyle = factory.createRStyle();
                              rstyle.setVal(CharStyle);
                              rPr.setRStyle(rstyle);
                              CurrRunNode.setRPr(rPr);
                          }
                          /*
                           * Find and apply character style for the first
                           * occurence
                           */
                          cont = false;
                          /*For testing purpose*/
                          logger.info("char currRun"+c+": "+((ContentAccessor) (CurrRunNode).getParent()).getContent().indexOf(CurrRunNode)+XmlUtils.marshaltoString(CurrRunNode));
                      }
                  }
                  occurCount++;
              }
              if (!cont) 
              {
                  break;
              }
          }
      }
      return p;
  }
  </code></pre>
  
  <p>The above code itself should be able to apply to the character style for the found text. But it is applying character style for the entire text in this w:r in its split form</p>
  
  <blockquote>
    <p>&lt;w:r w:rsidRPr="00F83849"&gt; &lt;w:rPr&gt; &lt;w:lang
    w:val="en-US"/&gt; &lt;/w:rPr&gt; &lt;w:t xml:space="preserve"&gt; II
    &lt; 1 (very low-risk group). Population characteristics and the
    immediate postoperative adverse events in both groups are shown in
    Table 1. Among preoperative characteristics, there was no case of
    active endocarditis, dialysis or prior cardiac surgery (redo) in very
    low-risk patients. When comparing risk groups, low-risk patients were
    older, with higher prevalence of women, longer cardiopulmonary bypass
    and cross-clamping times, and less isolated coronary surgery than in
    very low-risk patients.&lt;/w:t&gt; &lt;/w:r&gt;</p>
  </blockquote>
  
  <p>instead of applying only for these w:r s(Runs):</p>
  
  <blockquote>
    <p>&lt;w:r&gt;
                    &lt;w:rPr&gt;
                        &lt;w:rStyle w:val="TableCitation"/&gt;
                        &lt;w:lang w:val="en-US"/&gt;
                    &lt;/w:rPr&gt;
                    &lt;w:t xml:space="preserve"&gt;T&lt;/w:t&gt;
                &lt;/w:r&gt;
                &lt;w:r&gt;
                    &lt;w:rPr&gt;
                        &lt;w:rStyle w:val="TableCitation"/&gt;
                        &lt;w:lang w:val="en-US"/&gt;
                    &lt;/w:rPr&gt;
                    &lt;w:t xml:space="preserve"&gt;a&lt;/w:t&gt;
                &lt;/w:r&gt;
                &lt;w:r&gt;
                    &lt;w:rPr&gt;
                        &lt;w:rStyle w:val="TableCitation"/&gt;
                        &lt;w:lang w:val="en-US"/&gt;
                    &lt;/w:rPr&gt;
                    &lt;w:t xml:space="preserve"&gt;b&lt;/w:t&gt;
                &lt;/w:r&gt;
                &lt;w:r&gt;
                    &lt;w:rPr&gt;
                        &lt;w:rStyle w:val="TableCitation"/&gt;
                        &lt;w:lang w:val="en-US"/&gt;
                    &lt;/w:rPr&gt;
                    &lt;w:t xml:space="preserve"&gt;l&lt;/w:t&gt;
                &lt;/w:r&gt;
                &lt;w:r&gt;
                    &lt;w:rPr&gt;
                        &lt;w:rStyle w:val="TableCitation"/&gt;
                        &lt;w:lang w:val="en-US"/&gt;
                    &lt;/w:rPr&gt;
                    &lt;w:t xml:space="preserve"&gt;e&lt;/w:t&gt;
                &lt;/w:r&gt;
                &lt;w:r&gt;
                    &lt;w:rPr&gt;
                        &lt;w:rStyle w:val="TableCitation"/&gt;
                        &lt;w:lang w:val="en-US"/&gt;
                    &lt;/w:rPr&gt;
                    &lt;w:t xml:space="preserve"&gt; &lt;/w:t&gt;
                &lt;/w:r&gt;
                &lt;w:r&gt;
                    &lt;w:rPr&gt;
                        &lt;w:rStyle w:val="TableCitation"/&gt;
                        &lt;w:lang w:val="en-US"/&gt;
                    &lt;/w:rPr&gt;
                    &lt;w:t xml:space="preserve"&gt;1&lt;/w:t&gt;
                &lt;/w:r&gt;</p>
  </blockquote>
  
  <p>I'm also copying the code here which I'm using for the splitting run:</p>
  
  <pre><code>public static P SplitRuns(P paragraph, ObjectFactory factory,WordprocessingMLPackage wordMLPackage,File outputFile) throws Exception
  {
      ClassFinder runs = new ClassFinder(R.class);
      new TraversalUtil(paragraph, runs);
      for (Object r : runs.results) 
      {
          R run = (R)r;
          ContentAccessor parent = (ContentAccessor)run.getParent(); 
          List&lt;Object&gt; childElements = getAllchildObj(run);
          /*For testing purpose*/
          /*For testing purpose*/
          if (childElements.size() &gt; 0) 
          {
              logger.info("Origianl Run Index before splitting: "+parent.getContent().indexOf(run));
              for(Object child : childElements)
              {
                  int currentRunIndex = parent.getContent().indexOf(run);
                  if(child.getClass().getName() == "org.docx4j.wml.RPr")
                  {
                      continue;
                  }
                  if(child.getClass().getName() == "org.docx4j.wml.Text")
                  {
                      String[] textElementString = ((Text)child).getValue().split("(?!^)");
                      for(int i = 0; i &lt;= textElementString.length - 1; i++)
                      {
                          currentRunIndex = parent.getContent().indexOf(run);
                          R newRun = factory.createR();
                          RPr runProps = factory.createRPr();
                          runProps = run.getRPr();
                          if(runProps != null)
                          {
                              newRun.setRPr(runProps);
                          }
                          Text t = factory.createText(); 
                          JAXBElement&lt;org.docx4j.wml.Text&gt; textWrapped = factory.createRT(t); 
                          t.setSpace("preserve");
                          t.setValue(textElementString[i]);
                          newRun.getContent().add(t);
  
                          /*For testing purpose*/
                          logger.info("Newly created run Index: " + currentRunIndex);
                          /*For testing purpose*/
  
                          parent.getContent().add(currentRunIndex, newRun);
                          parent.getContent().set(currentRunIndex, newRun);
  
                      }
                  }
                  else
                  {
                      currentRunIndex = parent.getContent().indexOf(run);
                      R newRun = factory.createR();
                      RPr runProps = factory.createRPr();
                      runProps = run.getRPr();
                      if(runProps != null)
                      {
                          newRun.setRPr(runProps);
                      }
                      newRun.getContent().add(child);
  
                      /*For testing purpose*/
  
                      logger.info("Newly created run Index: " + currentRunIndex);
  
                      /*For testing purpose*/
  
                      parent.getContent().add(currentRunIndex, newRun);
                      parent.getContent().set(currentRunIndex, newRun);
  
                  }
  
              }
          }
          logger.info("Original run Index before Deleting: " + parent.getContent().indexOf(run));
          parent.getContent().remove(run);
      }
      return paragraph;
  }
  </code></pre>
  
  <p>Can someone help me solving this issue?</p>