Annotations and Special Text using Java

Extract highlighted text

Iterate through page annotations and read marked text from HighlightAnnotation.

  1. Open the source PDF in a Document instance.
  2. Iterate through the Annotation objects on the target Page.
  3. Check whether each annotation is a HighlightAnnotation before casting it to the typed annotation class.
  4. Read the marked text from each highlight annotation and print it to the console.
public static void extractHighlightedText(Path inputFile) {
    try (Document document = new Document(inputFile.toString())) {
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation instanceof HighlightAnnotation) {
                HighlightAnnotation highlightAnnotation = (HighlightAnnotation) annotation;
                System.out.println(highlightAnnotation.getMarkedText());
            }
        }
    }
}

Extract text from stamp annotations

Read the normal appearance stream from a stamp annotation and pass it through TextAbsorber.

  1. Open the source PDF in a Document instance.
  2. Iterate through the Annotation objects on the target Page.
  3. Filter the annotations to those whose type is Stamp.
  4. Create a TextAbsorber and request the normal appearance entry from the stamp annotation appearance dictionary.
  5. Visit the appearance XForm and print the extracted text.
public static void extractStampText(Path inputFile) {
    try (Document document = new Document(inputFile.toString())) {
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.Stamp) {
                TextAbsorber absorber = new TextAbsorber();
                Object[] xforms = new Object[1];
                if (annotation.getAppearance().tryGetValue("N", xforms) && xforms[0] instanceof XForm) {
                    absorber.visit((XForm) xforms[0]);
                    System.out.println(absorber.getText());
                }
            }
        }
    }
}

Extract superscript and subscript text details

Use TextFragmentAbsorber when you need both the extracted text and the superscript or subscript flags on each fragment.

  1. Open the source PDF in a Document instance.
  2. Create a TextFragmentAbsorber for fragment-level text analysis.
  3. Visit the target Page and collect its TextFragment objects.
  4. Iterate through those fragments and read the text together with the superscript and subscript flags from fragment.getTextState().
  5. Write the extracted details to the output file.
public static void extractSuperSubDetails(Path inputFile, Path outputFile, int pageNumber) throws Exception {
    try (Document document = new Document(inputFile.toString())) {
        TextFragmentAbsorber absorber = new TextFragmentAbsorber();
        document.getPages().get_Item(pageNumber).accept(absorber);
        StringBuilder details = new StringBuilder();
        for (TextFragment fragment : absorber.getTextFragments()) {
            details.append("Text: '").append(fragment.getText())
                    .append("' | Superscript: ").append(fragment.getTextState().isSuperscript())
                    .append(" | Subscript: ").append(fragment.getTextState().isSubscript())
                    .append(System.lineSeparator());
        }
        Files.writeString(outputFile, details.toString());
    }
}