title: replace a text using pdfbox for PDF file
body: <p>I have 4 pdf files that came from one .doc file and I use 4 methods to convert my doc to a pdf (foxite reader, nitro, webservice and Word).</p>
  
  <p>Then I used pdfbox to search and replace some words. The problem is, for some reason it only works for the file from foxite reader and Word, but not for the files created by nitro and the webservice.
  Can any one have a clue?</p>
  
  <p>This is the code I used:</p>
  
  <pre><code>public static void replace(String s) {
      PDDocument doc = null;
      int occurrences = 0;
      try {
          doc = PDDocument.load(s); // Input PDF File Name
          System.out.println("+e" + doc);
          List pages = doc.getDocumentCatalog()
                  .getAllPages();
          for (int i = 0; i &lt; pages.size(); i++) {
              PDPage page = (PDPage) pages.get(i);
              // System.out.println("ddd");
              PDStream contents = page.getContents();
              PDFStreamParser parser = new PDFStreamParser(contents.getStream());
              parser.parse();
              List tokens = parser.getTokens();
              for (int j = 0; j &lt; tokens.size(); j++) {
                  // System.out.println("jjjj");
                  Object next = tokens.get(j);
                  if (next instanceof PDFOperator) {
                      PDFOperator op = (PDFOperator) next;
                      // Tj and TJ are the two operators that display strings in a PDF
                      if (op.getOperation()
                              .equals("Tj")) {
                          // Tj takes one operator and that is the string
                          // to display so lets update that operator
                          COSString previous = (COSString) tokens.get(j - 1);
                          String string = previous.getString();
                          if (string.contains("#signature#")) {
                              string = string.replace("#signature#", "sam");
                              occurrences++;
                          }
                          // Word you want to change.
                          // Currently this code changes word "Good" to "Bad"
                          previous.reset();
                          previous.append(string.getBytes("ISO-8859-1"));
                      } else if (op.getOperation()
                              .equals("TJ")) {
                          COSArray previous = (COSArray) tokens.get(j - 1);
                          COSString temp = new COSString();
  
                          String tempString = "";
                          for (int t = 0; t &lt; previous.size(); t++) {
  
                              if (previous.get(t) instanceof COSString) {
                                  tempString += ((COSString) previous.get(t)).getString();
  
                              }
                          }
  
                          temp.append(tempString.getBytes("ISO-8859-1"));
                          tempString = "";
                          tempString = temp.getString();
                          if (tempString.contains("#signature#")) {
                              tempString = tempString.replace("#signature#", "sam");
                              occurrences++;
                          }
                          previous.clear();
  
                          String[] stringArray = tempString.split(" ");
  
                          for (String string : stringArray) {
                              COSString cosString = new COSString();
                              string = string + " ";
                              cosString.append(string.getBytes("ISO-8859-1"));
                              previous.add(cosString);
                          }
  
                      }
                  }
              }
              // now that the tokens are updated we will replace the page content stream.
              PDStream updatedStream = new PDStream(doc);
              OutputStream out = updatedStream.createOutputStream();
              ContentStreamWriter tokenWriter = new ContentStreamWriter(out);
              tokenWriter.writeTokens(tokens);
              page.setContents(updatedStream);
          }
          System.out.println("number of matches found: " + occurrences);
          doc.save(s + "_convert.pdf"); // Output file name
      } catch (Exception ex) {
          System.out.println("eee+" + ex.getMessage());
      } finally {
          if (doc != null) {
              try {
                  doc.close();
              } catch (IOException ex) {
                  ex.getStackTrace();
              }
          }
      }
  }
  </code></pre>