title: Apache PDFBox: problems with encoding
body: <p>I have a PDF template &amp; trying to replace some words in it. I use this code:</p>
  
  <pre><code>private PDDocument replaceText(PDDocument document, String searchString, String replacement) throws IOException {
      if (searchString.isEmpty() || replacement.isEmpty()) {
          return document;
      }
      PDPageTree pages = document.getDocumentCatalog().getPages();
      for (PDPage page : pages) {
          PDFStreamParser parser = new PDFStreamParser(page);
          parser.parse();
          List&lt;Object&gt; tokens = parser.getTokens();
          for (int j = 0; j &lt; tokens.size(); j++) {
              Object next = tokens.get(j);
              if (next instanceof Operator) {
                  Operator op = (Operator) next;
                  //Tj and TJ are the two operators that display strings in a PDF
                  if (op.getName().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 (searchString.equals(string)) {
                          System.out.println(string);
                      }
                      string = string.replaceFirst(searchString, replacement);
                      previous.setValue(string.getBytes());
                  } else if (op.getName().equals("TJ")) {
                      COSArray previous = (COSArray) tokens.get(j - 1);
                      for (int k = 0; k &lt; previous.size(); k++) {
                          Object arrElement = previous.getObject(k);
                          if (arrElement instanceof COSString) {
                              COSString cosString = (COSString) arrElement;
                              String string = cosString.getString();
                              if (searchString.equals(string)) {
                                  System.out.println(string);
                              }
                              string = StringUtils.replaceOnce(string, searchString, replacement);
                              cosString.setValue(string.getBytes());
                          }
                      }
                  }
              }
          }
          // now that the tokens are updated we will replace the page content stream.
          PDStream updatedStream = new PDStream(document);
          OutputStream out = updatedStream.createOutputStream();
          ContentStreamWriter tokenWriter = new ContentStreamWriter(out);
          tokenWriter.writeTokens(tokens);
          page.setContents(updatedStream);
          out.close();
      }
      return document;
  }
  </code></pre>
  
  <p>My PDF template have only 3 strings: "file:///C/Users/Mi/Downloads/converted.txt", "[10.03.2020 18:43:57]" and "hello!!!".
  First 2 strings searching correctly, but third looks like "KHOOR...":</p>
  
  <p><a href="https://i.stack.imgur.com/HTebQ.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HTebQ.jpg" alt="enter image description here"></a></p>
  
  <p>There is an encoding mismatch, as I understand. When I try to replace "file:///C/Users/Mi/Downloads/converted.txt" with "Hello!", it replaces as "ello", not shows uppercases and marks. As I understand, key difference is in fonts. "hello" have font settings, others not have.</p>
  
  <p>Source PDF is here:
  <a href="https://yadi.sk/i/l0OAcFkAkUHKYg" rel="nofollow noreferrer">https://yadi.sk/i/l0OAcFkAkUHKYg</a></p>
  
  <p>Please, advice, how to get text from PDF as correct strings and to replace it.</p>