使用 Java 的批注和特殊文本

提取突出显示的文本

遍历页面批注并读取标记的文本 HighlightAnnotation。

  1. 使用 Document 实例打开源 PDF。
  2. 遍历 Annotation 目标上的对象 Page。
  3. 检查每个批注是否为 HighlightAnnotation 在将其转换为特定批注类之前。
  4. 读取每个高亮批注中的标记文本并将其打印到控制台。
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());
            }
        }
    }
}

从印章批注中提取文本

读取印章批注的常规外观流并将其传递 TextAbsorber。

  1. 使用 Document 实例打开源 PDF。
  2. 遍历 Annotation 目标上的对象 Page。
  3. 过滤批注,仅保留其类型为 Stamp。
  4. 创建一个 TextAbsorber 并请求 stamp 批注外观字典中的 normal appearance 条目。
  5. 访问外观 XForm 并打印提取的文本。
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());
                }
            }
        }
    }
}

提取上标和下标文本详细信息

当需要在每个片段上同时获取提取的文本以及上标或下标标记时,使用 TextFragmentAbsorber。

  1. 使用 Document 实例打开源 PDF。
  2. 创建一个 TextFragmentAbsorber 用于片段级文本分析。
  3. 访问目标 Page 并收集它的 TextFragment 对象。
  4. 遍历这些片段并从中读取带有上标和下标标志的文本 fragment.getTextState()。
  5. 将提取的细节写入输出文件。
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());
    }
}