Разметка аннотаций с помощью Python
Эта статья показывает, как работать с разметкой аннотаций в PDF‑документах с использованием Aspose.PDF for Python via .NET.
Пример скрипта демонстрирует три распространённых рабочего процесса аннотаций:
- текстовые аннотации для комментариев в виде заметок
- аннотации курсора для маркеров вставки
- заменить аннотации для разметки замены текста
Текстовые аннотации
Добавление текстовых аннотаций
В этом примере создаётся текстовая аннотация на первой странице и связывается с всплывающим окном. Текстовые аннотации полезны для комментариев в стиле стикеров в процессах рецензирования.
Открытие исходного PDF
document = ap.Document(infile)
Создание и настроить текстовую аннотацию
Определите прямоугольник аннотации и задайте его заголовок, тему, содержимое, флаги отображения, цвет и значок.
text_annotation = ap.annotations.TextAnnotation(
document.pages[1],
ap.Rectangle(299.988, 613.664, 428.708, 680.769, True),
)
text_annotation.title = "Aspose User"
text_annotation.subject = "Sticky Note"
text_annotation.contents = (
"This is a text annotation added by Aspose.PDF for Python via .NET"
)
text_annotation.flags = ap.annotations.AnnotationFlags.PRINT
text_annotation.color = ap.Color.blue
text_annotation.icon = ap.annotations.TextIcon.HELP
Создание всплывающую аннотацию
Создайте всплывающее окно и подключите его к текстовой аннотации.
popup = ap.annotations.PopupAnnotation(
document.pages[1],
ap.Rectangle(428.708, 613.664, 528.708, 713.664, True),
)
popup.open = True
text_annotation.popup = popup
Добавление аннотацию и сохранить PDF
document.pages[1].annotations.add(text_annotation, consider_rotation=False)
document.save(outfile)
Полный пример
def text_annotation_add(infile, outfile):
document = ap.Document(infile)
text_annotation = ap.annotations.TextAnnotation(
document.pages[1],
ap.Rectangle(299.988, 613.664, 428.708, 680.769, True),
)
text_annotation.title = "Aspose User"
text_annotation.subject = "Sticky Note"
text_annotation.contents = (
"This is a text annotation added by Aspose.PDF for Python via .NET"
)
text_annotation.flags = ap.annotations.AnnotationFlags.PRINT
text_annotation.color = ap.Color.blue
text_annotation.icon = ap.annotations.TextIcon.HELP
popup = ap.annotations.PopupAnnotation(
document.pages[1],
ap.Rectangle(428.708, 613.664, 528.708, 713.664, True),
)
popup.open = True
text_annotation.popup = popup
document.pages[1].annotations.add(text_annotation, consider_rotation=False)
document.save(outfile)
Получение текстовые аннотации
Чтобы проверить существующие текстовые аннотации, отфильтруйте коллекцию аннотаций на первой странице и оставьте только элементы, тип которых является TEXT.
Открытие документ и собрать текстовые аннотации
document = ap.Document(infile)
text_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.TEXT
]
Вывести прямоугольники аннотаций
for annotation in text_annotations:
print(annotation.rect)
Полный пример
def text_annotation_get(infile, outfile):
document = ap.Document(infile)
text_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.TEXT
]
for annotation in text_annotations:
print(annotation.rect)
Удаление текстовые аннотации
Этот рабочий процесс удаляет все текстовые аннотации с первой страницы и сохраняет изменённый PDF.
Найти текстовые аннотации, которые нужно удалить
document = ap.Document(infile)
text_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.TEXT
]
Удаление аннотации и сохранить файл
for annotation in text_annotations:
document.pages[1].annotations.delete(annotation)
document.save(outfile)
Полный пример
def text_annotation_delete(infile, outfile):
document = ap.Document(infile)
text_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.TEXT
]
for annotation in text_annotations:
document.pages[1].annotations.delete(annotation)
document.save(outfile)
Каретные аннотации
Добавление аннотации каретки
Caret annotations используются для обозначения точек вставки в сценариях обзора. В этом примере добавляется caret annotation с прикреплённым всплывающим примечанием.
Открытие документ и получить целевую страницу
document = ap.Document(infile)
page = document.pages[1]
Создание и настроить аннотацию caret
caret_annotation = ap.annotations.CaretAnnotation(
page, ap.Rectangle(299.988, 713.664, 308.708, 720.769, True)
)
caret_annotation.title = "Aspose User"
caret_annotation.subject = "Inserted text 1"
caret_annotation.flags = ap.annotations.AnnotationFlags.PRINT
caret_annotation.color = ap.Color.blue
Прикрепить всплывающее окно и сохранить документ
caret_annotation.popup = ap.annotations.PopupAnnotation(
page, ap.Rectangle(310, 713, 410, 730, True)
)
page.annotations.append(caret_annotation)
document.save(outfile)
Полный пример
def caret_annotations_add(infile, outfile):
document = ap.Document(infile)
page = document.pages[1]
caret_annotation = ap.annotations.CaretAnnotation(
page, ap.Rectangle(299.988, 713.664, 308.708, 720.769, True)
)
caret_annotation.title = "Aspose User"
caret_annotation.subject = "Inserted text 1"
caret_annotation.flags = ap.annotations.AnnotationFlags.PRINT
caret_annotation.color = ap.Color.blue
caret_annotation.popup = ap.annotations.PopupAnnotation(
page, ap.Rectangle(310, 713, 410, 730, True)
)
page.annotations.append(caret_annotation)
document.save(outfile)
Получение аннотации каретки
Чтобы просмотреть аннотации caret, пройдитесь по аннотациям страницы и отфильтруйте по CARET тип аннотации.
Загрузить документ и страницу
document = ap.Document(infile)
page = document.pages[1]
Выводить прямоугольники аннотации caret
for annot in page.annotations:
if annot.annotation_type == ap.annotations.AnnotationType.CARET:
print(annot.rect)
Полный пример
def caret_annotations_get(infile, outfile):
document = ap.Document(infile)
page = document.pages[1]
for annot in page.annotations:
if annot.annotation_type == ap.annotations.AnnotationType.CARET:
print(annot.rect)
Удаление аннотации каретки
Этот рабочий процесс сначала собирает аннотации caret, удаляет их по одной, а затем сохраняет обновлённый файл.
Загрузить документ и собрать аннотации карет
document = ap.Document(infile)
page = document.pages[1]
caret_annotations = [
annot
for annot in page.annotations
if annot.annotation_type == ap.annotations.AnnotationType.CARET
]
Удаление аннотации и сохранить документ
for annot in caret_annotations:
page.annotations.delete(annot)
document.save(outfile)
Полный пример
def caret_annotations_delete(infile, outfile):
document = ap.Document(infile)
page = document.pages[1]
caret_annotations = [
annot
for annot in page.annotations
if annot.annotation_type == ap.annotations.AnnotationType.CARET
]
for annot in caret_annotations:
page.annotations.delete(annot)
document.save(outfile)
Замена аннотации
Добавление замену аннотаций
Replace annotations комбинируют аннотацию caret и сгруппированную аннотацию strikeout. Этот шаблон отмечает текст, который следует заменить, и связывает заметку о замене с зачеркиваемым содержимым.
Открытие документ и получить страницу
document = ap.Document(infile)
page = document.pages[1]
Создание аннотацию caret для заменяемого текста
caret_annotation = ap.annotations.CaretAnnotation(
page, ap.Rectangle(361.246, 727.908, 370.081, 735.107, True)
)
caret_annotation.flags = ap.annotations.AnnotationFlags.PRINT
caret_annotation.subject = "Inserted text 2"
caret_annotation.title = "Aspose User"
caret_annotation.color = ap.Color.blue
caret_annotation.popup = ap.annotations.PopupAnnotation(
page, ap.Rectangle(310, 713, 410, 730, True)
)
Создание группированную аннотацию зачеркивания
Определите область зачеркивания, назначьте квадропункты и свяжите её с аннотацией каретки через in_reply_to и reply_type.
strikeout_annotation = ap.annotations.StrikeOutAnnotation(
page, ap.Rectangle(318.407, 727.826, 368.916, 740.098, True)
)
strikeout_annotation.color = ap.Color.blue
strikeout_annotation.quad_points = [
ap.Point(321.66, 739.416),
ap.Point(365.664, 739.416),
ap.Point(321.66, 728.508),
ap.Point(365.664, 728.508),
]
strikeout_annotation.subject = "Cross-out"
strikeout_annotation.in_reply_to = caret_annotation
strikeout_annotation.reply_type = ap.annotations.ReplyType.GROUP
Добавление обе аннотации и сохранить PDF
page.annotations.append(caret_annotation)
page.annotations.append(strikeout_annotation)
document.save(outfile)
Полный пример
def replace_annotations_add(infile, outfile):
document = ap.Document(infile)
page = document.pages[1]
caret_annotation = ap.annotations.CaretAnnotation(
page, ap.Rectangle(361.246, 727.908, 370.081, 735.107, True)
)
caret_annotation.flags = ap.annotations.AnnotationFlags.PRINT
caret_annotation.subject = "Inserted text 2"
caret_annotation.title = "Aspose User"
caret_annotation.color = ap.Color.blue
caret_annotation.popup = ap.annotations.PopupAnnotation(
page, ap.Rectangle(310, 713, 410, 730, True)
)
strikeout_annotation = ap.annotations.StrikeOutAnnotation(
page, ap.Rectangle(318.407, 727.826, 368.916, 740.098, True)
)
strikeout_annotation.color = ap.Color.blue
strikeout_annotation.quad_points = [
ap.Point(321.66, 739.416),
ap.Point(365.664, 739.416),
ap.Point(321.66, 728.508),
ap.Point(365.664, 728.508),
]
strikeout_annotation.subject = "Cross-out"
strikeout_annotation.in_reply_to = caret_annotation
strikeout_annotation.reply_type = ap.annotations.ReplyType.GROUP
page.annotations.append(caret_annotation)
page.annotations.append(strikeout_annotation)
document.save(outfile)
Получение замену аннотаций
Чтобы определить заменяющие аннотации, найдите аннотации-зачёркивания, сгруппированные как ответы на другую аннотацию. Пример приводит каждую аннотацию-зачёркивание перед проверкой её полей отношений.
Загрузить документ и пройтись по аннотациям
document = ap.Document(infile)
page = document.pages[1]
Фильтровать сгруппированные аннотации перечёркивания
for annot in page.annotations:
if annot.annotation_type == ap.annotations.AnnotationType.STRIKE_OUT:
sa = cast(ap.annotations.StrikeOutAnnotation, annot)
if (
sa.in_reply_to is not None
and sa.reply_type == ap.annotations.ReplyType.GROUP
):
print(f"Replace annotation rect: {sa.rect}")
Полный пример
def replace_annotations_get(infile, outfile):
document = ap.Document(infile)
page = document.pages[1]
for annot in page.annotations:
if annot.annotation_type == ap.annotations.AnnotationType.STRIKE_OUT:
sa = cast(ap.annotations.StrikeOutAnnotation, annot)
if (
sa.in_reply_to is not None
and sa.reply_type == ap.annotations.ReplyType.GROUP
):
print(f"Replace annotation rect: {sa.rect}")
Удаление замену аннотаций
Этот рабочий процесс собирает аннотации зачёркивания, используемые для замены разметки, удаляет их со страницы и сохраняет результирующий PDF.
Загрузить документ и собрать заменяющие аннотации
document = ap.Document(infile)
page = document.pages[1]
replace_annotations = [
cast(ap.annotations.StrikeOutAnnotation, annot)
for annot in page.annotations
if annot.annotation_type == ap.annotations.AnnotationType.STRIKE_OUT
]
Удаление аннотации и сохранить документ
for annot in replace_annotations:
page.annotations.delete(annot)
document.save(outfile)
Полный пример
def replace_annotations_delete(infile, outfile):
document = ap.Document(infile)
page = document.pages[1]
replace_annotations = [
cast(ap.annotations.StrikeOutAnnotation, annot)
for annot in page.annotations
if annot.annotation_type == ap.annotations.AnnotationType.STRIKE_OUT
]
for annot in replace_annotations:
page.annotations.delete(annot)
document.save(outfile)