title: Extracting info from Content tree and Tags tree in PDF with PDFBox
body: <p>I have a PDF created from word that contains a good amount of info regarding its structure in the Content Tree and Tags Tree that can be seen in Adobe Acrobat (see pics below)
  <a href="https://i.stack.imgur.com/CTJrl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CTJrl.png" alt="View of the Content Tree or Table"></a>
  <a href="https://i.stack.imgur.com/f0xZX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/f0xZX.png" alt="View of the Tags Tree or Table"></a></p>
  
  <p>Before you suggest it, no, I don't have access to the original word document so I'm forced to deal with PDF parsing.</p>
  
  <p>I've managed to do some progress with Java PDFBox which allows me to locate the annotations on each page and extract info (the annotations are part of the content tree). 
  Digging in the PDPage raw documentation <a href="https://pdfbox.apache.org/docs/2.0.8/javadocs/" rel="nofollow noreferrer">PDFBox PDPage documentation</a> I have found a couple methods that seem promising such as getContents() and getCOSDictionary(). Now from what I understand dealing with COSObjects is painful and not robust and getContents yields and InputStream of bytes that is difficult to deal with.</p>
  
  <p>Has someone managed to obtain the info from the mentioned tables in a clean and easy way? If a different package/language has been used this works too :)</p>
  
  <pre><code>package acs.pdf2isosts.TestPDFBox;
  
  import java.io.BufferedReader;
  import java.io.File;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.util.Iterator;
  import java.util.List;
  
  import org.apache.pdfbox.pdmodel.PDDocument;
  import org.apache.pdfbox.pdmodel.PDPage;
  import org.apache.pdfbox.pdmodel.common.PDStream;
  import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation;
  
  import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink;
  
  public class ReadPdf  {
      public static void main( String[] args ) throws IOException{
  
          PDDocument document = PDDocument.load(new File(
                  "/sample.pdf")); 
          document.getClass();
  
          if (!document.isEncrypted()) {
  
              Iterator&lt;PDPage&gt; it = document.getPages().iterator();
              while (it.hasNext()) {
                  PDPage pg = it.next();
                  //System.out.println(pg.toString());
                  List&lt;PDAnnotation&gt; annotations = pg.getAnnotations();
                  for (PDAnnotation annotation : annotations)
                  {
                      if (annotation instanceof PDAnnotationLink)
                      {
                          PDAnnotationLink annotationLink = (PDAnnotationLink)annotation;
                          System.out.println(annotationLink.getAction().getCOSObject());
                      }
                  BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(pg.getContents()));
                  String line;
                  while( (line = bufferedReader.readLine()) != null )
                  { 
                      System.out.println(line.getClass());
                      System.out.printf("%s\n", line);
                  }
  
  
              }
  
          }
  
          document.close();
  
  
      }
          }
  }
  </code></pre>