title: Make some characters as invisible from an existing PDF document by using PDFBox
body: <p>I tried to read the "Tj" operator from an existing PDF by using Apache <strong>PDFBox.</strong>
  When i achieved completely this task, i tried then to replace some characters. For example let us consider a pdf document containing (hello world)Tj, so this code replaces "hello" to "hi123".
  Therefore the modified document becomes containing (hi123 world)Tj instead of (hello world)Tj.</p>
  
  <p>My big problem now is how to edit this code in order to make "hello" as text rendering mode 3 (in other terms: text rendering mode "invisible"). So i don't want to replace "hello" by "hi123", but to make "hello" as disappear (mode invisible). And therefore, the modified document becomes containing just "world" where "hello" became invisible.</p>
  
  <p>My code so far:</p>
  
  <pre><code>public class Test1 {
      private static Test1 tes; 
      private static final String src="...";
      private static PDPageContentStream content;
      private static PDType1Font font; 
  
      public static void CreatePdf(String src) throws IOException, COSVisitorException{
          PDRectangle rec= new PDRectangle(400,400);
          PDDocument document= null;
          document = new PDDocument();
          PDPage page = new PDPage(rec);
          document.addPage(page);
          PDDocumentInformation info=document.getDocumentInformation();
          info.setAuthor("PdfBox");
          info.setCreator("Pdf");
          info.setSubject("Stéganographie");
          info.setTitle("Stéganographie dans les documents PDF");
          info.setKeywords("Stéganographie, pdf");
          content= new PDPageContentStream(document, page);
          font= PDType1Font.HELVETICA;
          String texte="hello world";
          content.beginText();
          content.setFont(font, 12);
          content.moveTextPositionByAmount(15, 385);   
          // content.appendRawCommands("3 Tr");
          content.drawString(texte);
          content.endText();
          content.close();
          document.save("doc.pdf");
          document.close();       
      }
  
      public static void main(String[] args) throws IOException, COSVisitorException {
          tes= new Test1();
          tes.CreatePdf(src);
          PDDocument doc ;
          doc = PDDocument.load("doc.pdf");
          List pages = doc.getDocumentCatalog().getAllPages();  
          for (int i = 0; i &lt; pages.size(); i++)  {
              PDPage page = (PDPage) pages.get(i);  
              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++)  
              {  
                  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();  
                          System.out.println(string);
                          //Word you want to change. Currently this code changes word "hello" to "hi123"
                          string = string.replaceFirst("hello", "hi123"); 
                          previous.reset();  
                          previous.append(string.getBytes("ISO-8859-1"));
                      }
                  }      
              }
              // 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);
          }
          doc.save("a.pdf"); 
          doc.close();  
      }
  }
  </code></pre>