title: How to replace centered text in a PDF with PDFBox
body: <p>I use the <code>PDFTextReplacement</code> example.
  It does the replacement as expected, In case my text is left aligned.
  But if my input pdf has a text centered, it replaces the text as a left aligned.
  Ok, so I have to recalculate the right starting point.</p>
  
  <p>For that reason I have two targets or questions:</p>
  
  <ul>
  <li>How to determine the alignment?</li>
  <li>How to calculate the right starting point?</li>
  </ul>
  
  <p>Here is my code:</p>
  
  <pre><code>public PDDocument doIt(String inputFile, Map&lt;String, String&gt; text)
          throws IOException, COSVisitorException {
      // the document
      PDDocument doc = null;
  
      doc = PDDocument.load(inputFile);
      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
  
                  String pstring = "";
                  int prej = 0;
                  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(j + " " + string);
                      if (j == prej) {
                          pstring += string;
                      } else {
                          prej = j;
                          pstring = string;
                      }
  
                      previous.reset();
                      previous.append(string.getBytes("ISO-8859-1"));
                  } else if (op.getOperation().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;
                              }
  
                              cosString.reset();
                              // cosString.append(string
                              // .getBytes("ISO-8859-1"));
                          }
  
                      }
  
                      COSString cosString2 = (COSString) previous
                              .getObject(0);
  
                      for (int t = 1; t &lt; previous.size(); t++)
                          previous.remove(t);
  
                      // cosString2.setNeedToBeUpdate(true);
  
                      if (text.containsKey(pstring.trim())) {
  
                          String textValue = text.get(pstring.trim());
                          cosString2.append(textValue.getBytes("ISO-8859-1"));
  
                          for (int k = 1; k &lt; previous.size(); k++) {
                              previous.remove(k);
  
                          }
                      }
  
                  }
              }
          }
          // 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);
      }
      return doc;
  }
  </code></pre>