title: Extract PDF comments into HTML
body: <p><strong>Input PDF document with comment</strong></p>
  
  <p>I have a PDF document with highlight and comment on the highlight ("my comment") (<a href="https://www.dropbox.com/s/lh4uq8w7lnbzn4x/text.pdf?dl=0" rel="nofollow noreferrer">downlload</a>). </p>
  
  <p><a href="https://i.stack.imgur.com/lGNxw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lGNxw.png" alt="enter image description here"></a></p>
  
  <p><strong>Desired output</strong></p>
  
  <p>I want to convert the PDF into text, where comment is in <b></b> tags, something like this:</p>
  
  <pre><code>ONE TWO THREE    
  FOUR &lt;b id="my comment"&gt;FIVE&lt;/b&gt; SIX SEVEN
  </code></pre>
  
  <p><strong>Question</strong></p>
  
  <p>Can anyone help me how to implement method:</p>
  
  <pre><code>private double getDistance(PDAnnotation ann, TextPosition firstProsition) {...}
  </code></pre>
  
  <p>or the method </p>
  
  <pre><code>private boolean isTextAnnotated()
  </code></pre>
  
  <p>to determine if the annotation ann is at the position of the text? If possible also the text position of the comment would be nice to determine.</p>
  
  <p><strong>JAVA code</strong></p>
  
  <p>Anyway I got lost regarding how to determine, if annotation is related to the currently processed text. I also do not know, if it is possible to identify exact part of the text.</p>
  
  <pre><code>                PDFParser parser = new PDFParser(new FileInputStream(file));
                  parser.parse();
                  cosDoc = parser.getDocument();
  
                  pdfStripper = new PDFTextStripper()
                  {
                      List&lt;PDAnnotation&gt; la;
                      private boolean closeWithEnd;
                      @Override
                      protected void startPage(PDPage page) throws IOException
                      {
                          la = page.getAnnotations(); // init pages
                          startOfLine = true;
                          super.startPage(page);
                      }
  
                      @Override
                      protected void writeLineSeparator() throws IOException
                      {
                          startOfLine = true;
                          super.writeLineSeparator();
                          if(closeWithEnd) {
                              writeString(" &lt;/b&gt; ");
                          }
                      }
  
                      @Override
                      protected void writeString(String text, List&lt;TextPosition&gt; textPositions) throws IOException
                      {
                          if (startOfLine)
                          {
                              TextPosition firstProsition = textPositions.get(0);
                              PDAnnotation ann;
                              if((ann = isTextAnnotated(firstProsition, text)) != null) {
                                  writeString(" &lt;b id='"+ann.getAnnotationName()+"'&gt; ");
                                  closeWithEnd = true;
                              } else {
                                  closeWithEnd = false;
                              }
                              startOfLine = false;
                          }
                          super.writeString(text+" ", textPositions);
                      }
                      private PDAnnotation isTextAnnotated(TextPosition firstProsition, String text) {
                          for (PDAnnotation ann : la) {
                              System.out.println(text+" ------------- "+getDistance(ann, firstProsition));
                          }
                          return null;
                      }
                      private double getDistance(PDAnnotation ann, TextPosition firstProsition) {
                          TODO - how to get distance
                          return 0.0;
                      }
                      boolean startOfLine = true;
                  };
  
                  pdDoc = new PDDocument(cosDoc);
                  pdfStripper.setStartPage(0);
                  pdfStripper.setEndPage(pdDoc.getNumberOfPages());
                  String parsedText = pdfStripper.getText(pdDoc);
  </code></pre>
  
  <p><strong>Maven dependencies</strong></p>
  
  <pre><code>&lt;dependency&gt;
    &lt;groupId&gt;junit&lt;/groupId&gt;
    &lt;artifactId&gt;junit&lt;/artifactId&gt;
    &lt;version&gt;3.8.1&lt;/version&gt;
    &lt;scope&gt;test&lt;/scope&gt;
  &lt;/dependency&gt;
  
  &lt;!-- http://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox --&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;org.apache.pdfbox&lt;/groupId&gt;
      &lt;artifactId&gt;pdfbox&lt;/artifactId&gt;
      &lt;version&gt;1.8.10&lt;/version&gt;
  &lt;/dependency&gt;
  
  &lt;!-- http://mvnrepository.com/artifact/org.apache.tika/tika-core --&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;org.apache.tika&lt;/groupId&gt;
      &lt;artifactId&gt;tika-core&lt;/artifactId&gt;
      &lt;version&gt;1.13&lt;/version&gt;
  &lt;/dependency&gt;
  
  &lt;!-- http://mvnrepository.com/artifact/commons-io/commons-io --&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;commons-io&lt;/groupId&gt;
      &lt;artifactId&gt;commons-io&lt;/artifactId&gt;
      &lt;version&gt;2.4&lt;/version&gt;
  &lt;/dependency&gt;
  
  
  &lt;!-- http://mvnrepository.com/artifact/log4j/log4j --&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;log4j&lt;/groupId&gt;
      &lt;artifactId&gt;log4j&lt;/artifactId&gt;
      &lt;version&gt;1.2.17&lt;/version&gt;
  &lt;/dependency&gt;
  
  &lt;dependency&gt;
      &lt;groupId&gt;info.debatty&lt;/groupId&gt;
      &lt;artifactId&gt;java-string-similarity&lt;/artifactId&gt;
      &lt;version&gt;RELEASE&lt;/version&gt;
  &lt;/dependency&gt;
  
  &lt;dependency&gt;
    &lt;groupId&gt;org.apache.opennlp&lt;/groupId&gt;
    &lt;artifactId&gt;opennlp-tools&lt;/artifactId&gt;
    &lt;version&gt;1.6.0&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p></p>