title: replace string with unicode text in pdf file using PDFbox?
body: <p>I need to read the strings from PDF file and replace it with the Unicode text.If it is ASCII chars everything is fine. But with Unicode characters, it showing question marks/junk text.No problem with font file(ttf) I am able to write a unicode text to the pdf file with a different class (PDFContentStream). With this class, there is no option to replace text but we can add new text.</p>
  
  <h2>Sample unicode text</h2>
  
  <p>Bɐɑɒ</p>
  
  <h2>issue (Address column)</h2>
  
  <p><a href="https://drive.google.com/file/d/1DbsApTCSfTwwK3txsDGW8sXtDG_u-VJv/view?usp=sharing" rel="nofollow noreferrer">https://drive.google.com/file/d/1DbsApTCSfTwwK3txsDGW8sXtDG_u-VJv/view?usp=sharing</a></p>
  
  <p>I am using PDFBox.
  Please help me with this.....</p>
  
  <p>check the code I am using.....</p>
  
  <pre><code>    enter image description herepublic static PDDocument _ReplaceText(PDDocument document, String searchString, String replacement)
          throws IOException {
      if (StringUtils.isEmpty(searchString) || StringUtils.isEmpty(replacement)) {
          return document;
      }
  
      for (PDPage page : document.getPages()) {
  
          PDResources resources = new PDResources();
          PDFont font = PDType0Font.load(document, new File("arial-unicode-ms.ttf"));
          //PDFont font2 = PDType0Font.load(document, new File("avenir-next-regular.ttf"));
          resources.add(font);
          //resources.add(font2);
          //resources.add(PDType1Font.TIMES_ROMAN);
          page.setResources(resources);
          PDFStreamParser parser = new PDFStreamParser(page);
          parser.parse();
          List 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;
  
                  String pstring = "";
                  int prej = 0;
  
                  // 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();
                      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 (j == prej) {
                                  pstring += string;
                              } else {
                                  prej = j;
                                  pstring = string;
                              }
                          }
                      }
  
                      if (searchString.equals(pstring.trim())) {
                          COSString cosString2 = (COSString) previous.getObject(0);
                          cosString2.setValue(replacement.getBytes());
  
                          int total = previous.size() - 1;
                          for (int k = total; k &gt; 0; k--) {
                              previous.remove(k);
                          }
                      }
                  }
              }
          }
  
          // now that the tokens are updated we will replace the page content stream.
          PDStream updatedStream = new PDStream(document);
          OutputStream out = updatedStream.createOutputStream(COSName.FLATE_DECODE);
          ContentStreamWriter tokenWriter = new ContentStreamWriter(out);
          tokenWriter.writeTokens(tokens);
          out.close();
          page.setContents(updatedStream);
      }
  
      return document;
  }
  </code></pre>