Anotaciones basadas en texto usando Java

Las anotaciones basadas en texto permiten a revisores y desarrolladores agregar notas interactivas, resaltados y marcas a los documentos PDF sin alterar el contenido principal. Esta sección cubre cinco tipos prácticos de anotaciones utilizados en flujos de trabajo de revisión de documentos, escenarios de cumplimiento y ciclos de retroalimentación colaborativa.

Referencia rápida: tipos de anotación

Este artículo cubre los siguientes tipos de anotaciones basadas en texto:

  • Free Text: Cuadros de texto editables para añadir notas y comentarios
  • Highlight: Énfasis visual en pasajes de texto importantes
  • Strikeout: Marcar texto para eliminación o revisión durante la revisión
  • Squiggly: Subrayado ondulado para indicar errores o preocupaciones
  • Underline: Énfasis de subrayado tradicional con precisión opcional de cuatro puntos

Agregar, obtener y eliminar anotaciones de texto libre

Las anotaciones de texto libre actúan como cuadros de texto flotantes que pueden editarse sin afectar la estructura del documento. Utiliza estos ejemplos para añadir cuadros de comentario, inspeccionar sus propiedades o eliminarlos.

Agregar anotaciones de texto libre

  1. Abra el PDF de origen Document.
  2. Cree un FreeTextAnnotation con un rectángulo y configuraciones de apariencia.
  3. Agregue la anotación a la página y guarde el documento.
public static void freeTextAnnotationAdd(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        FreeTextAnnotation freeTextAnnotation = new FreeTextAnnotation(
                document.getPages().get_Item(1),
                new Rectangle(299, 713, 308, 720, true),
                new DefaultAppearance());
        freeTextAnnotation.setTitle("Aspose User");
        freeTextAnnotation.setColor(Color.getLightGreen());

        document.getPages().get_Item(1).getAnnotations().add(freeTextAnnotation);
        document.save(outputFile.toString());
    }
}

Obtener anotaciones de texto libre

  1. Abra el PDF de origen Document.
  2. Itere a través de las anotaciones en la página y filtre por AnnotationType.FreeText.
  3. Recupere las propiedades de la anotación o los límites.
public static void freeTextAnnotationGet(Path inputFile) {
    try (Document document = new Document(inputFile.toString())) {
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.FreeText) {
                System.out.println(annotation.getRect());
            }
        }
    }
}

Eliminar anotaciones de texto libre

  1. Abra el PDF de origen Document.
  2. Encuentre anotaciones de texto libre iterando a través de las anotaciones de la página y filtrando por tipo.
  3. Agregue las anotaciones coincidentes a una lista de eliminación y elimínelas de la página.
  4. Guarde el documento actualizado.
public static void freeTextAnnotationDelete(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        List<Annotation> toDelete = new ArrayList<>();
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.FreeText) {
                toDelete.add(annotation);
            }
        }
        for (Annotation annotation : toDelete) {
            document.getPages().get_Item(1).getAnnotations().delete(annotation);
        }
        document.save(outputFile.toString());
    }
}

Agregar, obtener y eliminar anotaciones de resaltado

Las anotaciones de resaltado marcan pasajes importantes con una superposición semitransparente. Utilice estos ejemplos para crear resaltados para la revisión de documentos, localizar resaltados existentes y limpiar el marcado.

Agregar anotaciones de resaltado

  1. Abra el PDF de origen Document.
  2. Cree un HighlightAnnotation con un rectángulo que define el área resaltada.
  3. Agregue la anotación a la página y guarde el documento.
public static void textHighlightAnnotationAdd(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        HighlightAnnotation highlightAnnotation = new HighlightAnnotation(
                document.getPages().get_Item(1),
                new Rectangle(300, 750, 320, 770, true));

        document.getPages().get_Item(1).getAnnotations().add(highlightAnnotation);
        document.save(outputFile.toString());
    }
}

Obtener anotaciones resaltadas

  1. Abra el PDF de origen Document.
  2. Itere a través de las anotaciones y filtre por AnnotationType.Highlight.
  3. Lea las propiedades de la anotación, como los límites o el color.
public static void textHighlightAnnotationGet(Path inputFile) {
    try (Document document = new Document(inputFile.toString())) {
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.Highlight) {
                System.out.println(annotation.getRect());
            }
        }
    }
}

Eliminar anotaciones de resaltado

  1. Abra el PDF de origen Document.
  2. Recopile anotaciones de resaltado filtrando las anotaciones por tipo.
  3. Elimine cada anotación de la página.
  4. Guarde el documento actualizado.
public static void textHighlightAnnotationDelete(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        List<Annotation> toDelete = new ArrayList<>();
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.Highlight) {
                toDelete.add(annotation);
            }
        }
        for (Annotation annotation : toDelete) {
            document.getPages().get_Item(1).getAnnotations().delete(annotation);
        }
        document.save(outputFile.toString());
    }
}

Agregar, obtener y eliminar anotaciones de tachado

Las anotaciones de tachado cruzan el texto para indicar eliminación, rechazo o revisión. Utilice estos ejemplos para aplicar el marcado de tachado durante la revisión de documentos, encontrar el texto marcado y eliminar las anotaciones de tachado.

Agregar anotaciones de tachado

  1. Abra el PDF de origen Document.
  2. Cree un StrikeOutAnnotation con un rectángulo, título y color.
  3. Agregue la anotación a la página y guarde el documento.
public static void textStrikeoutAnnotationAdd(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        StrikeOutAnnotation strikeoutAnnotation = new StrikeOutAnnotation(
                document.getPages().get_Item(1),
                new Rectangle(299.988, 713.664, 308.708, 720.769, true));
        strikeoutAnnotation.setTitle("Aspose User");
        strikeoutAnnotation.setSubject("Inserted text 1");
        strikeoutAnnotation.setFlags(AnnotationFlags.Print);
        strikeoutAnnotation.setColor(Color.getBlue());

        document.getPages().get_Item(1).getAnnotations().add(strikeoutAnnotation);
        document.save(outputFile.toString());
    }
}

Obtener anotaciones tachadas

  1. Abra el PDF de origen Document.
  2. Itere a través de las anotaciones y filtre por AnnotationType.StrikeOut.
  3. Lea los metadatos o los límites de la anotación.
public static void textStrikeoutAnnotationGet(Path inputFile) {
    try (Document document = new Document(inputFile.toString())) {
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.StrikeOut) {
                System.out.println(annotation.getRect());
            }
        }
    }
}

Eliminar anotaciones de tachado

  1. Abra el PDF de origen Document.
  2. Recopile anotaciones tachadas filtrando por tipo.
  3. Elimine cada anotación de la página.
  4. Guarde el documento actualizado.
public static void textStrikeoutAnnotationDelete(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        List<Annotation> toDelete = new ArrayList<>();
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.StrikeOut) {
                toDelete.add(annotation);
            }
        }
        for (Annotation annotation : toDelete) {
            document.getPages().get_Item(1).getAnnotations().delete(annotation);
        }
        document.save(outputFile.toString());
    }
}

Agregar, obtener y eliminar anotaciones onduladas

Las anotaciones onduladas (subrayados ondulados) resaltan posibles errores, preocupaciones o elementos que requieren atención. Utilice estos ejemplos para marcar texto problemático, inspeccionar anotaciones onduladas y eliminarlas de los documentos.

Agregar anotaciones onduladas

  1. Abra el PDF de origen Document.
  2. Cree un SquigglyAnnotation con un rectángulo y título.
  3. Agregue la anotación a la página y guarde el documento.
public static void textSquigglyAnnotationAdd(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        Page page = document.getPages().get_Item(1);
        SquigglyAnnotation squigglyAnnotation = new SquigglyAnnotation(
                page,
                new Rectangle(67, 317, 261, 459, true));
        squigglyAnnotation.setTitle("John Smith");
        squigglyAnnotation.setColor(Color.getBlue());

        page.getAnnotations().add(squigglyAnnotation);
        document.save(outputFile.toString());
    }
}

Obtener anotaciones onduladas

  1. Abra el PDF de origen Document.
  2. Itere a través de las anotaciones y filtre por AnnotationType.Squiggly.
  3. Lea los límites de la anotación o los metadatos.
public static void textSquigglyAnnotationGet(Path inputFile) {
    try (Document document = new Document(inputFile.toString())) {
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.Squiggly) {
                System.out.println(annotation.getRect());
            }
        }
    }
}

Eliminar anotaciones onduladas

  1. Abra el PDF de origen Document.
  2. Recopile anotaciones onduladas filtrando por tipo.
  3. Elimine cada anotación de la página.
  4. Guarde el documento actualizado.
public static void textSquigglyAnnotationDelete(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        List<Annotation> toDelete = new ArrayList<>();
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.Squiggly) {
                toDelete.add(annotation);
            }
        }
        for (Annotation annotation : toDelete) {
            document.getPages().get_Item(1).getAnnotations().delete(annotation);
        }
        document.save(outputFile.toString());
    }
}

Agregar, obtener y eliminar anotaciones subrayadas

Las anotaciones de subrayado enfatizan pasajes importantes con un subrayado tradicional. Use estos ejemplos para crear subrayados, leer el contenido de texto marcado y eliminar anotaciones de subrayado de las páginas.

Agregar anotaciones de subrayado

  1. Abra el PDF de origen Document.
  2. Cree un UnderlineAnnotation con un rectángulo y color.
  3. Agregue la anotación a la página y guarde el documento.
public static void textUnderlineAnnotationAdd(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        UnderlineAnnotation underlineAnnotation = new UnderlineAnnotation(
                document.getPages().get_Item(1),
                new Rectangle(299.988, 713.664, 308.708, 720.769, true));
        underlineAnnotation.setTitle("Aspose User");
        underlineAnnotation.setSubject("Inserted Underline 1");
        underlineAnnotation.setFlags(AnnotationFlags.Print);
        underlineAnnotation.setColor(Color.getBlue());

        document.getPages().get_Item(1).getAnnotations().add(underlineAnnotation);
        document.save(outputFile.toString());
    }
}

Obtener anotaciones subrayadas

  1. Abra el PDF de origen Document.
  2. Itere a través de las anotaciones y filtre por AnnotationType.Underline.
  3. Lea propiedades o límites de la anotación.
public static void textUnderlineAnnotationGet(Path inputFile) {
    try (Document document = new Document(inputFile.toString())) {
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.Underline) {
                System.out.println(annotation.getRect());
            }
        }
    }
}

Eliminar anotaciones de subrayado

  1. Abra el PDF de origen Document.
  2. Recopile anotaciones subrayadas filtrando por tipo.
  3. Elimine cada anotación de la página.
  4. Guarde el documento actualizado.
public static void textUnderlineAnnotationDelete(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        List<Annotation> toDelete = new ArrayList<>();
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.Underline) {
                toDelete.add(annotation);
            }
        }
        for (Annotation annotation : toDelete) {
            document.getPages().get_Item(1).getAnnotations().delete(annotation);
        }
        document.save(outputFile.toString());
    }
}

Agregar una anotación subrayada con puntos cuádruples

Este ejemplo define el área subrayada explícitamente mediante puntos cuádruples derivados de un rectángulo.

  1. Abra el PDF de origen Document.
  2. Cree un UnderlineAnnotation y calcule sus puntos quad.
  3. Agregue la anotación a la página y guarde el documento.
public static void textUnderlineWithQuadPointsAdd(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        Rectangle rect = new Rectangle(299.988, 713.664, 308.708, 720.769, true);
        UnderlineAnnotation underlineAnnotation = new UnderlineAnnotation(
                document.getPages().get_Item(1), rect);
        underlineAnnotation.setTitle("Aspose User");
        underlineAnnotation.setSubject("Inserted Underline with Quad Points");
        underlineAnnotation.setFlags(AnnotationFlags.Print);
        underlineAnnotation.setColor(Color.getBlue());
        underlineAnnotation.setQuadPoints(new com.aspose.pdf.Point[]{
                new com.aspose.pdf.Point(rect.getLLX(), rect.getLLY()),
                new com.aspose.pdf.Point(rect.getURX(), rect.getLLY()),
                new com.aspose.pdf.Point(rect.getURX(), rect.getURY()),
                new com.aspose.pdf.Point(rect.getLLX(), rect.getURY())
        });

        document.getPages().get_Item(1).getAnnotations().add(underlineAnnotation);
        document.save(outputFile.toString());
    }
}

Obtener texto marcado de anotaciones subrayadas

Recupera el contenido de texto real cubierto por las anotaciones de subrayado. Estos ejemplos muestran dos enfoques: leer el texto marcado completo como una única cadena, o procesar los fragmentos de texto individualmente para un análisis detallado.

  1. Abra el PDF de origen Document.
  2. Itere a través de las anotaciones de subrayado en la página.
  3. Lea cualquiera getMarkedText() o getMarkedTextFragments() y imprima los resultados.
public static void textUnderlineMarkedTextGet(Path inputFile) {
    try (Document document = new Document(inputFile.toString())) {
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.Underline) {
                UnderlineAnnotation ua = (UnderlineAnnotation) annotation;
                System.out.println("Marked text: " + ua.getMarkedText());
            }
        }
    }
}
public static void textUnderlineMarkedFragmentsGet(Path inputFile) {
    try (Document document = new Document(inputFile.toString())) {
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.Underline) {
                UnderlineAnnotation ua = (UnderlineAnnotation) annotation;
                for (TextFragment fragment : ua.getMarkedTextFragments()) {
                    System.out.println("Fragment text: " + fragment.getText());
                }
            }
        }
    }
}

Eliminar anotaciones de subrayado por título

Eliminar anotaciones selectivamente filtrando por propiedades de metadatos como el título. Este enfoque permite una limpieza dirigida de anotaciones por autor o propósito.

  1. Abra el PDF de origen Document.
  2. Filtre anotaciones subrayadas por título.
  3. Elimine las anotaciones coincidentes y guarde el documento actualizado.
public static void textUnderlineByTitleDelete(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        List<UnderlineAnnotation> toDelete = new ArrayList<>();
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.Underline) {
                UnderlineAnnotation ua = (UnderlineAnnotation) annotation;
                if ("Aspose User".equals(ua.getTitle())) {
                    toDelete.add(ua);
                }
            }
        }
        for (UnderlineAnnotation ua : toDelete) {
            document.getPages().get_Item(1).getAnnotations().delete(ua);
        }
        document.save(outputFile.toString());
    }
}

Agregar y aplanar una anotación subrayada

Convierte una anotación de subrayado interactiva en contenido permanente de la página mediante su aplanamiento. Esto evita la edición posterior mientras conserva la apariencia del subrayado en cualquier visor de PDF.

  1. Abra el PDF de origen Document.
  2. Añada un UnderlineAnnotation a la página.
  3. Llame flatten() en la anotación y guarde el archivo de salida.
public static void textUnderlineFlattenAdd(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        UnderlineAnnotation underlineAnnotation = new UnderlineAnnotation(
                document.getPages().get_Item(1),
                new Rectangle(299.988, 713.664, 308.708, 720.769, true));
        underlineAnnotation.setTitle("Aspose User");
        underlineAnnotation.setSubject("Inserted Underline to Flatten");
        underlineAnnotation.setFlags(AnnotationFlags.Print);
        underlineAnnotation.setColor(Color.getBlue());

        document.getPages().get_Item(1).getAnnotations().add(underlineAnnotation);
        underlineAnnotation.flatten();

        document.save(outputFile.toString());
    }
}

Temas de anotación relacionados