Anotaciones y texto especial usando Java
Contents
[
Hide
]
Extraer texto resaltado
Iterar a través de las anotaciones de la página y leer el texto marcado de HighlightAnnotation.
- Abra el PDF de origen en una instancia de
Document. - Itere a través del
Annotationobjetos en el objetivoPage. - Verifique si cada anotación es un
HighlightAnnotationantes de hacer casting a la clase de anotación tipada. - Lea el texto marcado de cada anotación de resaltado y imprímalo en la consola.
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());
}
}
}
}
Extraer texto de anotaciones de sello
Lea el flujo de apariencia normal de una anotación de sello y páselo TextAbsorber.
- Abra el PDF de origen en una instancia de
Document. - Itere a través del
Annotationobjetos en el objetivoPage. - Filtre las anotaciones a aquellas cuyo tipo es
Stamp. - Cree una
TextAbsorbery solicite la entrada de apariencia normal del diccionario de apariencias de la anotación de sello. - Visite la apariencia
XFormy imprima el texto extraído.
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());
}
}
}
}
}
Extraer detalles de texto de superíndice y subíndice
Utilice TextFragmentAbsorber cuando necesites tanto el texto extraído como las banderas de superíndice o subíndice en cada fragmento.
- Abra el PDF de origen en una instancia de
Document. - Cree una
TextFragmentAbsorberpara el análisis de texto a nivel de fragmento. - Visite el objetivo
Pagey recopile suTextFragmentobjetos. - Itere a través de esos fragmentos y lea el texto junto con las banderas de superíndice y subíndice de
fragment.getTextState(). - Escriba los detalles extraídos en el archivo de salida.
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());
}
}