Add Polygon Annotation

Contents
[ ]

Polygon annotations are used to highlight irregular areas or shapes in a PDF, providing visual emphasis or marking specific regions. Using PdfContentEditor, you can create polygons by specifying the vertices coordinates, border style, page number, and annotation rectangle.

  1. Create the PdfContentEditor object.
  2. Bind the input PDF.
  3. Configure Polygon properties.
  4. Add the Polygon annotation.
  5. Save the updated Document.
import aspose.pdf as ap
import aspose.pdf.facades as pdf_facades
import aspose.pydrawing as apd
import sys
from os import path

sys.path.append(path.join(path.dirname(__file__), ".."))

from config import set_license, initialize_data_dir


def add_polygon_annotation(infile, outfile):
    # Create PdfContentEditor object
    content_editor = pdf_facades.PdfContentEditor()
    # Bind input PDF file
    content_editor.bind_pdf(infile)

    line_info = pdf_facades.LineInfo()
    line_info.border_style = 0  # 0 - Solid
    line_info.vertice_coordinate = [100, 200, 150, 260, 220, 220, 200, 160]
    content_editor.create_polygon(
        line_info,
        1,
        apd.Rectangle(90, 150, 150, 120),
        "This is polygon annotation",
    )

    # Save output PDF file
    content_editor.save(outfile)