title: Apache POI search .txt and replace .docm
body: <p>I'm using <strong>ApachePOI library</strong> to search&amp;replace in a <code>.docm</code> file.
  I have a <code>.txt</code> with extracted first <strong>3 words</strong> from every page from a PDF file <strong>(same content as <code>.docm</code> file).</strong></p>
  
  <p>I want to get every line from the <code>.txt</code> and search inside the <code>.docm</code>, when matches , before that particular 3 words I will add a <code>html</code> page tag like <code>&lt;page&gt;(pdfpagenumber)&lt;/page&gt;</code></p>
  
  <p>EDITED(30.03.2017) My <code>searchedTemp.txt</code> looks like:</p>
  
  <pre><code>Dieses Buch wendet
  Einführung in
  b) mit ihm 
  Straftäter werden 
  etc.
  </code></pre>
  
  <p>EDIT(30.03.2017) The code within APACHE POI looks like this:</p>
  
  <pre><code>package dgi.writetags;
  
  
  import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  import org.apache.poi.xwpf.usermodel.XWPFDocument;
  import org.apache.poi.xwpf.usermodel.XWPFParagraph;
  import org.apache.poi.xwpf.usermodel.XWPFRun;
  import java.io.BufferedReader;
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileOutputStream;
  import java.io.FileReader;
  import java.io.IOException;
  import java.util.Scanner;
  
  
  public class App {
      public static  String SOURCE_FILE = "D:" + File.separator + "ReadPageTags" + File.separator + "Doc" + File.separator + "test.docm";
      public static  String OUTPUT_FILE = "D:" + File.separator + "ReadPageTags" + File.separator + "Doc" + File.separator + "output.docm";
      public static  String OUTPUT_SEARCH = "D:" + File.separator + "ReadPageTags" + File.separator + "GeneratedList" + File.separator + "searchTemp.txt";
      public static  String actual;
  
  
      //MAIN METHOD
      public static void main(String[] args) throws Exception {
          //Application instances
          App docm = new App();
          System.out.println("[DOCM] App started.");
  
          //Search inside the TXT
          //docm.searchlist();
  
  
          //CheckStart
          //System.out.print(docm.searchlist());
          //CheckEnd
  
  
          //Replace text in DOCM
          docm.replaceText();
  
  
          //Exit after succeed
          //System.exit(0);
  
      }
  
  
      //replace text method
      private void replaceText() throws InvalidFormatException, IOException{
          App wt = new App();
          String[] lines = wt.searchlist().split("\n");
           XWPFDocument doc = new XWPFDocument(new FileInputStream(SOURCE_FILE));
              for (XWPFParagraph p : doc.getParagraphs()){
                  for (XWPFRun r : p.getRuns()){
  
                      String text = r.toString();
  
                        for(String s : lines){
  
  
                          text = text.replace(s, "pagetag "+s);
  
                          r.setText(text,0);
  
  
  
                  }
  
                  }
                System.out.println("[DOCM] Working...");
  
             }
             doc.write(new FileOutputStream(OUTPUT_FILE));
             doc.close();
  
             System.out.println("[DOCM] Finished");
  
  
  
  }
  
  
  
      //search into text file method
      //for the latest method return STRING
      private String searchlist() throws IOException, InvalidFormatException{
  
  
          BufferedReader br = new BufferedReader(new FileReader(OUTPUT_SEARCH));
          StringBuffer buf = new StringBuffer();
          String line;
          while (true) {
              line = br.readLine();
              buf.append(line);
              buf.append(System.lineSeparator());
              if (line == null) break;
  
          }
          br.close();
  
          return buf.toString();
  
  
      }
  </code></pre>
  
  <p>Even if I remove the <code>while loop</code>,or add some <code>regex</code>, or do it in other ways the same, it won't replace...</p>
  
  <p>P.S.: Oh, and if I get only the third line it throws me an error with the ")" part.</p>