Anotações Baseadas em Texto usando Python
Este artigo mostra como trabalhar com anotações baseadas em texto em documentos PDF usando Aspose.PDF for Python via .NET.
O script de exemplo demonstra vários fluxos de trabalho de anotação de texto:
- anotações de texto livre
- anotações de destaque
- anotações sublinhadas
- anotações onduladas
- anotações de tachado
Anotações de Texto Livre
Adicionar Anotações de Texto Livre
Anotações de texto livre permitem que você coloque comentários de texto visíveis diretamente em uma página PDF. Este exemplo adiciona uma anotação de texto livre simples à primeira página.
Abra o PDF de origem
document = ap.Document(infile)
Criar e configurar a anotação de texto livre
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
Adicione a anotação e salve o PDF
document.pages[1].annotations.append(free_text_annotation)
document.save(outfile)
Exemplo 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)
Obter Anotações de Texto Livre
Para inspecionar anotações de texto livre, filtre as anotações da primeira página por FREE_TEXT digite e imprima cada retângulo de anotação.
Carregue o documento e colete anotações de texto livre
document = ap.Document(infile)
free_text_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.FREE_TEXT
]
Imprima os retângulos de anotação
for annotation in free_text_annotations:
print(annotation.rect)
Exemplo 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)
Excluir Anotações FreeText
Este fluxo de trabalho remove todas as anotações de texto livre da primeira página e salva o PDF atualizado.
Localizar e excluir anotações de texto livre
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)
Exemplo 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)
Anotações de Marcação de Texto
Anotações de Destaque
Adicionar realce de texto
As anotações de realce enfatizam partes do documento sem alterar o conteúdo subjacente. Este exemplo adiciona uma anotação de realce à primeira 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)
Obter destaque de texto
Para inspecionar anotações de destaque, filtre as anotações da página por HIGHLIGHT digite e imprima seus retâ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)
Excluir destaque de texto
Este fluxo de trabalho remove todas as anotações de destaque da primeira página e salva o PDF de saída.
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)
Anotações de sublinhado
Adicionar Anotações de Sublinhado de Texto
Anotações de sublinhado marcam o texto com um sublinhado visível. Este exemplo adiciona uma anotação de sublinhado básica e define seus metadados e cor.
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)
Adicionar Anotações de Sublinhado de Texto Aplanar
Se você quiser que o sublinhado se torne parte do conteúdo da página em vez de permanecer como uma anotação interativa, pode achatá‑lo depois de adicioná‑lo.
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)
Adicionar Anotações de Sublinhado de Texto com Quad Points
Os pontos quad permitem que você defina a área exata marcada para a anotação de sublinhado. Isso é útil quando você precisa de mais controle do que um simples retângulo.
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)
Excluir Anotações de Sublinhado de Texto
Este fluxo de trabalho remove todas as anotações de sublinhado da primeira página e salva o documento atualizado.
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)
Excluir anotações de sublinhado de texto por título
Este fluxo de trabalho mostra como excluir seletivamente anotações de sublinhado após verificar seu 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)
Obter Anotações de Sublinhado de Texto
Para inspecionar anotações de sublinhado, filtre as anotações da primeira página por UNDERLINE digite e imprima cada retâ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)
Obter Texto Sublinhado Anotações Texto Marcado
Este fluxo de trabalho converte cada anotação de sublinhado em um UnderlineAnnotation objeto e extrai o 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()}")
Obter fragmentos marcados de anotações de sublinhado de texto
Se precisar de cada fragmento marcado separadamente, pode iterar através da coleção retornada 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}")
Anotações onduladas
Adicionar Anotações Onduladas
Anotações onduladas são frequentemente usadas para marcar áreas de ortografia, gramática ou atenção no texto. Este exemplo adiciona uma anotação ondulada à primeira 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)
Obter Anotações Squiggly
Para inspecionar anotações onduladas, filtre as anotações da página por SQUIGGLY digite e imprima seus retâ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)
Excluir anotações onduladas
Este fluxo de trabalho remove todas as anotações onduladas da primeira página e salva o 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)
Anotações de Tachado
Adicionar Anotações de Texto Tachado
Anotações de tachado marcam texto que deve ser tratado como removido ou riscado. Este exemplo adiciona uma anotação de tachado e define seus metadados e cor.
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)
Obter Anotações de Texto Tachado
Para inspecionar anotações de tachado, filtre as anotações da página pelo STRIKE_OUT digite e imprima seus retâ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)
Excluir anotações de tachado de texto
Este fluxo de trabalho remove todas as anotações de tachado da primeira página e salva o documento atualizado.
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)