Anotaciones basadas en texto usando Python
Este artículo muestra cómo trabajar con anotaciones basadas en texto en documentos PDF usando Aspose.PDF for Python via .NET.
El script de ejemplo muestra varios flujos de trabajo de anotación de texto:
- anotaciones de texto libre
- anotaciones de resaltado
- anotaciones de subrayado
- anotaciones onduladas
- anotaciones de tachado
Anotaciones de texto libre
Agregar anotaciones FreeText
Las anotaciones de texto libre le permiten colocar comentarios de texto visibles directamente en una página PDF. Este ejemplo agrega una anotación de texto libre simple a la primera página.
Abra el PDF de origen
document = ap.Document(infile)
Crear y configurar la anotación de texto libre
free_text_annotation = ap.annotations.FreeTextAnnotation(
document.pages[1],
ap.Rectangle(299, 713, 308, 720, True),
ap.annotations.DefaultAppearance(),
)
free_text_annotation.title = "Aspose User"
free_text_annotation.color = ap.Color.light_green
Añadir la anotación y guardar el PDF
document.pages[1].annotations.append(free_text_annotation)
document.save(outfile)
Ejemplo completo
def free_text_annotation_add(infile, outfile):
document = ap.Document(infile)
free_text_annotation = ap.annotations.FreeTextAnnotation(
document.pages[1],
ap.Rectangle(299, 713, 308, 720, True),
ap.annotations.DefaultAppearance(),
)
free_text_annotation.title = "Aspose User"
free_text_annotation.color = ap.Color.light_green
document.pages[1].annotations.append(free_text_annotation)
document.save(outfile)
Obtener anotaciones de texto libre
Para inspeccionar anotaciones de texto libre, filtre las anotaciones de la primera página por el FREE_TEXT escribe e imprime cada rectángulo de anotación.
Cargue el documento y recopile anotaciones de texto libre
document = ap.Document(infile)
free_text_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.FREE_TEXT
]
Imprimir los rectángulos de anotación
for annotation in free_text_annotations:
print(annotation.rect)
Ejemplo completo
def free_text_annotation_get(infile, outfile):
document = ap.Document(infile)
free_text_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.FREE_TEXT
]
for annotation in free_text_annotations:
print(annotation.rect)
Eliminar anotaciones de texto libre
Este flujo de trabajo elimina todas las anotaciones de texto libre de la primera página y guarda el PDF actualizado.
Buscar y eliminar anotaciones de texto libre
document = ap.Document(infile)
free_text_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.FREE_TEXT
]
for annotation in free_text_annotations:
document.pages[1].annotations.delete(annotation)
document.save(outfile)
Ejemplo completo
def free_text_annotation_delete(infile, outfile):
document = ap.Document(infile)
free_text_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.FREE_TEXT
]
for annotation in free_text_annotations:
document.pages[1].annotations.delete(annotation)
document.save(outfile)
Anotaciones de marcado de texto
Resaltar anotaciones
Agregar resaltado de texto
Las anotaciones de resaltado enfatizan partes del documento sin cambiar el contenido subyacente. Este ejemplo agrega una anotación de resaltado a la primera página.
document = ap.Document(infile)
highlight_annotation = ap.annotations.HighlightAnnotation(
document.pages[1],
ap.Rectangle(300, 750, 320, 770, True),
)
document.pages[1].annotations.append(highlight_annotation)
document.save(outfile)
def text_highlight_annotation_add(infile, outfile):
document = ap.Document(infile)
highlight_annotation = ap.annotations.HighlightAnnotation(
document.pages[1],
ap.Rectangle(300, 750, 320, 770, True),
)
document.pages[1].annotations.append(highlight_annotation)
document.save(outfile)
Obtener resaltado de texto
Para inspeccionar las anotaciones de resaltado, filtre las anotaciones de la página por el HIGHLIGHT escriba e imprima sus rectángulos.
document = ap.Document(infile)
highlight_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.HIGHLIGHT
]
for annotation in highlight_annotations:
print(annotation.rect)
def text_highlight_annotation_get(infile, outfile):
document = ap.Document(infile)
highlight_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.HIGHLIGHT
]
for annotation in highlight_annotations:
print(annotation.rect)
Eliminar resaltado de texto
Este flujo de trabajo elimina todas las anotaciones de resaltado de la primera página y guarda el PDF de salida.
document = ap.Document(infile)
highlight_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.HIGHLIGHT
]
for annotation in highlight_annotations:
document.pages[1].annotations.delete(annotation)
document.save(outfile)
def text_highlight_annotation_delete(infile, outfile):
document = ap.Document(infile)
highlight_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.HIGHLIGHT
]
for annotation in highlight_annotations:
document.pages[1].annotations.delete(annotation)
document.save(outfile)
Anotaciones de subrayado
Agregar anotaciones de subrayado de texto
Las anotaciones de subrayado marcan el texto con un subrayado visible. Este ejemplo agrega una anotación de subrayado básica y establece sus metadatos y color.
document = ap.Document(infile)
underline_annotation = ap.annotations.UnderlineAnnotation(
document.pages[1],
ap.Rectangle(299.988, 713.664, 308.708, 720.769, True),
)
underline_annotation.title = "Aspose User"
underline_annotation.subject = "Inserted Underline 1"
underline_annotation.flags = ap.annotations.AnnotationFlags.PRINT
underline_annotation.color = ap.Color.blue
document.pages[1].annotations.append(underline_annotation)
document.save(outfile)
def text_underline_annotation_add(infile, outfile):
document = ap.Document(infile)
underline_annotation = ap.annotations.UnderlineAnnotation(
document.pages[1],
ap.Rectangle(299.988, 713.664, 308.708, 720.769, True),
)
underline_annotation.title = "Aspose User"
underline_annotation.subject = "Inserted Underline 1"
underline_annotation.flags = ap.annotations.AnnotationFlags.PRINT
underline_annotation.color = ap.Color.blue
document.pages[1].annotations.append(underline_annotation)
document.save(outfile)
Agregar Texto Subrayado Anotaciones Aplanar
Si deseas que el subrayado se convierta en parte del contenido de la página en lugar de permanecer como una anotación interactiva, puedes aplanarlo después de añadirlo.
document = ap.Document(infile)
underline_annotation = ap.annotations.UnderlineAnnotation(
document.pages[1],
ap.Rectangle(299.988, 713.664, 308.708, 720.769, True),
)
underline_annotation.title = "Aspose User"
underline_annotation.subject = "Inserted Underline to Flatten"
underline_annotation.flags = ap.annotations.AnnotationFlags.PRINT
underline_annotation.color = ap.Color.blue
document.pages[1].annotations.append(underline_annotation)
underline_annotation.flatten()
document.save(outfile)
def text_underline_flatten_add(infile, outfile):
document = ap.Document(infile)
underline_annotation = ap.annotations.UnderlineAnnotation(
document.pages[1],
ap.Rectangle(299.988, 713.664, 308.708, 720.769, True),
)
underline_annotation.title = "Aspose User"
underline_annotation.subject = "Inserted Underline to Flatten"
underline_annotation.flags = ap.annotations.AnnotationFlags.PRINT
underline_annotation.color = ap.Color.blue
document.pages[1].annotations.append(underline_annotation)
underline_annotation.flatten()
document.save(outfile)
Agregar anotaciones de subrayado de texto con puntos cuádruples
Los puntos quad le permiten definir el área exacta marcada para la anotación de subrayado. Esto es útil cuando necesita más control que un rectángulo simple.
document = ap.Document(infile)
rect = ap.Rectangle(299.988, 713.664, 308.708, 720.769, True)
underline_annotation = ap.annotations.UnderlineAnnotation(document.pages[1], rect)
underline_annotation.title = "Aspose User"
underline_annotation.subject = "Inserted Underline with Quad Points"
underline_annotation.flags = ap.annotations.AnnotationFlags.PRINT
underline_annotation.color = ap.Color.blue
underline_annotation.quad_points = [
ap.Point(rect.llx, rect.lly),
ap.Point(rect.urx, rect.lly),
ap.Point(rect.urx, rect.ury),
ap.Point(rect.llx, rect.ury),
]
document.pages[1].annotations.append(underline_annotation)
document.save(outfile)
def text_underline_with_quad_points_add(infile, outfile):
document = ap.Document(infile)
rect = ap.Rectangle(299.988, 713.664, 308.708, 720.769, True)
underline_annotation = ap.annotations.UnderlineAnnotation(document.pages[1], rect)
underline_annotation.title = "Aspose User"
underline_annotation.subject = "Inserted Underline with Quad Points"
underline_annotation.flags = ap.annotations.AnnotationFlags.PRINT
underline_annotation.color = ap.Color.blue
underline_annotation.quad_points = [
ap.Point(rect.llx, rect.lly),
ap.Point(rect.urx, rect.lly),
ap.Point(rect.urx, rect.ury),
ap.Point(rect.llx, rect.ury),
]
document.pages[1].annotations.append(underline_annotation)
document.save(outfile)
Eliminar anotaciones de subrayado de texto
Este flujo de trabajo elimina todas las anotaciones de subrayado de la primera página y guarda el documento actualizado.
document = ap.Document(infile)
underline_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.UNDERLINE
]
for annotation in underline_annotations:
document.pages[1].annotations.delete(annotation)
document.save(outfile)
def text_underline_annotation_delete(infile, outfile):
document = ap.Document(infile)
underline_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.UNDERLINE
]
for annotation in underline_annotations:
document.pages[1].annotations.delete(annotation)
document.save(outfile)
Eliminar anotaciones de subrayado de texto por título
Este flujo de trabajo muestra cómo eliminar selectivamente anotaciones de subrayado después de verificar su título.
document = ap.Document(infile)
underline_annotations = [
cast(ap.annotations.UnderlineAnnotation, annotation)
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.UNDERLINE
]
for annotation in underline_annotations:
if annotation.title.startswith("a"):
document.pages[1].annotations.delete(annotation)
document.save(outfile)
def text_underline_by_title_delete(infile, outfile):
document = ap.Document(infile)
underline_annotations = [
cast(ap.annotations.UnderlineAnnotation, annotation)
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.UNDERLINE
]
for annotation in underline_annotations:
if annotation.title.startswith("a"):
document.pages[1].annotations.delete(annotation)
document.save(outfile)
Obtener anotaciones de subrayado de texto
Para inspeccionar anotaciones subrayadas, filtre las anotaciones de la primera página por el UNDERLINE escribe e imprime cada rectángulo.
document = ap.Document(infile)
underline_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.UNDERLINE
]
for annotation in underline_annotations:
print(annotation.rect)
def text_underline_annotation_get(infile, outfile):
document = ap.Document(infile)
underline_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.UNDERLINE
]
for annotation in underline_annotations:
print(annotation.rect)
Obtener texto subrayado de anotaciones de texto marcado
Este flujo de trabajo convierte cada anotación de subrayado en una UnderlineAnnotation objeto y extrae el texto marcado.
document = ap.Document(infile)
underline_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.UNDERLINE
]
for annotation in underline_annotations:
ua = cast(ap.annotations.UnderlineAnnotation, annotation)
print(f"Marked text: {ua.get_marked_text()}")
def text_underline_marked_text_get(infile, outfile):
document = ap.Document(infile)
underline_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.UNDERLINE
]
for annotation in underline_annotations:
ua = cast(ap.annotations.UnderlineAnnotation, annotation)
print(f"Marked text: {ua.get_marked_text()}")
Obtener Fragmentos Marcados de Anotaciones de Subrayado de Texto
Si necesita cada fragmento marcado por separado, puede iterar a través de la colección devuelta por get_marked_text_fragments().
document = ap.Document(infile)
underline_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.UNDERLINE
]
for annotation in underline_annotations:
ua = cast(ap.annotations.UnderlineAnnotation, annotation)
for fragment in ua.get_marked_text_fragments():
print(f"Fragment text: {fragment.text}")
def text_underline_marked_fragments_get(infile, outfile):
document = ap.Document(infile)
underline_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.UNDERLINE
]
for annotation in underline_annotations:
ua = cast(ap.annotations.UnderlineAnnotation, annotation)
for fragment in ua.get_marked_text_fragments():
print(f"Fragment text: {fragment.text}")
Anotaciones onduladas
Agregar anotaciones onduladas
Las anotaciones onduladas se utilizan a menudo para marcar áreas de ortografía, gramática o atención en el texto. Este ejemplo añade una anotación ondulada a la primera página.
document = ap.Document(infile)
page = document.pages[1]
squiggly_annotation = ap.annotations.SquigglyAnnotation(
page,
ap.Rectangle(67, 317, 261, 459, True),
)
squiggly_annotation.title = "John Smith"
squiggly_annotation.color = ap.Color.blue
page.annotations.append(squiggly_annotation)
document.save(outfile)
def text_squiggly_annotation_add(infile, outfile):
document = ap.Document(infile)
page = document.pages[1]
squiggly_annotation = ap.annotations.SquigglyAnnotation(
page,
ap.Rectangle(67, 317, 261, 459, True),
)
squiggly_annotation.title = "John Smith"
squiggly_annotation.color = ap.Color.blue
page.annotations.append(squiggly_annotation)
document.save(outfile)
Obtener anotaciones onduladas
Para inspeccionar anotaciones onduladas, filtre las anotaciones de la página por la SQUIGGLY escriba e imprima sus rectángulos.
document = ap.Document(infile)
squiggly_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.SQUIGGLY
]
for annotation in squiggly_annotations:
print(annotation.rect)
def text_squiggly_annotation_get(infile, outfile):
document = ap.Document(infile)
squiggly_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.SQUIGGLY
]
for annotation in squiggly_annotations:
print(annotation.rect)
Eliminar anotaciones onduladas
Este flujo de trabajo elimina todas las anotaciones onduladas de la primera página y guarda el resultado.
document = ap.Document(infile)
squiggly_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.SQUIGGLY
]
for annotation in squiggly_annotations:
document.pages[1].annotations.delete(annotation)
document.save(outfile)
def text_squiggly_annotation_delete(infile, outfile):
document = ap.Document(infile)
squiggly_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.SQUIGGLY
]
for annotation in squiggly_annotations:
document.pages[1].annotations.delete(annotation)
document.save(outfile)
Anotaciones de tachado
Agregar anotaciones de tachado de texto
Las anotaciones de tachado marcan el texto que debe ser tratado como eliminado o tachado. Este ejemplo agrega una anotación de tachado y establece sus metadatos y color.
document = ap.Document(infile)
strikeout_annotation = ap.annotations.StrikeOutAnnotation(
document.pages[1],
ap.Rectangle(299.988, 713.664, 308.708, 720.769, True),
)
strikeout_annotation.title = "Aspose User"
strikeout_annotation.subject = "Inserted text 1"
strikeout_annotation.flags = ap.annotations.AnnotationFlags.PRINT
strikeout_annotation.color = ap.Color.blue
document.pages[1].annotations.append(strikeout_annotation)
document.save(outfile)
def text_strikeout_annotation_add(infile, outfile):
document = ap.Document(infile)
strikeout_annotation = ap.annotations.StrikeOutAnnotation(
document.pages[1],
ap.Rectangle(299.988, 713.664, 308.708, 720.769, True),
)
strikeout_annotation.title = "Aspose User"
strikeout_annotation.subject = "Inserted text 1"
strikeout_annotation.flags = ap.annotations.AnnotationFlags.PRINT
strikeout_annotation.color = ap.Color.blue
document.pages[1].annotations.append(strikeout_annotation)
document.save(outfile)
Obtener anotaciones de tachado de texto
Para inspeccionar anotaciones de tachado, filtre las anotaciones de la página por el STRIKE_OUT escriba e imprima sus rectángulos.
document = ap.Document(infile)
strikeout_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.STRIKE_OUT
]
for annotation in strikeout_annotations:
print(annotation.rect)
def text_strikeout_annotation_get(infile, outfile):
document = ap.Document(infile)
strikeout_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.STRIKE_OUT
]
for annotation in strikeout_annotations:
print(annotation.rect)
Eliminar anotaciones de tachado de texto
Este flujo de trabajo elimina todas las anotaciones de tachado de la primera página y guarda el documento actualizado.
document = ap.Document(infile)
strikeout_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.STRIKE_OUT
]
for annotation in strikeout_annotations:
document.pages[1].annotations.delete(annotation)
document.save(outfile)
def text_strikeout_annotation_delete(infile, outfile):
document = ap.Document(infile)
strikeout_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.STRIKE_OUT
]
for annotation in strikeout_annotations:
document.pages[1].annotations.delete(annotation)
document.save(outfile)