GroupDocs.Annotation lets you add many kinds of annotations to PDF, Word, Excel, PowerPoint, image, and other documents. You create an annotation object, set its properties, call add on the Annotator, and save the result.
A few rules apply to every annotation:
Position. Box-style annotations (area, ellipse, point, arrow, distance, polyline, text field, watermark, resources redaction, image) are positioned with a Rectangle(x, y, width, height) assigned to the .box property. Text-markup annotations (highlight, underline, strikeout, squiggly, replacement, text redaction, link) are positioned with a list of corner Point objects assigned to the .points property, in the order top-left, top-right, bottom-left, bottom-right.
Colors. Every color property (background_color, font_color, pen_color, underline_color, squiggly_color) is a packed ARGB integer, not a Color object. Use Color.<name>.to_argb() or Color.from_argb(a, r, g, b).to_argb().
Page index.page_number is 0-based — 0 is the first page.
Add an area annotation
An area annotation draws a filled rectangle over a region of the page.
fromgroupdocs.annotationimportAnnotatorfromgroupdocs.annotation.modelsimportRectangle,PenStylefromgroupdocs.annotation.models.annotation_modelsimportAreaAnnotationfromgroupdocs.pydrawingimportColordefadd_area_annotation():# Load the document to be annotatedwithAnnotator("./sample.pdf")asannotator:# Configure an area (rectangle) annotationarea=AreaAnnotation()area.box=Rectangle(100,100,100,100)# x, y, width, heightarea.background_color=Color.yellow.to_argb()area.pen_color=Color.red.to_argb()area.pen_width=3area.pen_style=PenStyle.SOLIDarea.opacity=0.7area.page_number=0area.message="This is an area annotation"# Add the annotation and save the resultannotator.add(area)annotator.save("./output.pdf")if__name__=="__main__":add_area_annotation()
sample.pdf is the sample file used in this example. Click here to download it.
An ellipse annotation draws a filled oval inside the bounding box.
fromgroupdocs.annotationimportAnnotatorfromgroupdocs.annotation.modelsimportRectangle,PenStylefromgroupdocs.annotation.models.annotation_modelsimportEllipseAnnotationfromgroupdocs.pydrawingimportColordefadd_ellipse_annotation():withAnnotator("./sample.pdf")asannotator:ellipse=EllipseAnnotation()ellipse.box=Rectangle(100,100,200,120)ellipse.background_color=Color.from_argb(255,144,238,144).to_argb()ellipse.pen_color=Color.green.to_argb()ellipse.pen_width=2ellipse.pen_style=PenStyle.SOLIDellipse.opacity=0.7ellipse.page_number=0ellipse.message="This is an ellipse annotation"annotator.add(ellipse)annotator.save("./output.pdf")if__name__=="__main__":add_ellipse_annotation()
sample.pdf is the sample file used in this example. Click here to download it.
A point annotation marks a single location on the page. It is positioned by the origin of its box, so the width and height are 0.
fromgroupdocs.annotationimportAnnotatorfromgroupdocs.annotation.modelsimportRectanglefromgroupdocs.annotation.models.annotation_modelsimportPointAnnotationdefadd_point_annotation():withAnnotator("./sample.pdf")asannotator:point=PointAnnotation()point.box=Rectangle(100,100,0,0)# a point is positioned by its box originpoint.page_number=0point.message="This is a point annotation"annotator.add(point)annotator.save("./output.pdf")if__name__=="__main__":add_point_annotation()
sample.pdf is the sample file used in this example. Click here to download it.
An arrow annotation draws a directed line across the bounding box.
fromgroupdocs.annotationimportAnnotatorfromgroupdocs.annotation.modelsimportRectangle,PenStylefromgroupdocs.annotation.models.annotation_modelsimportArrowAnnotationfromgroupdocs.pydrawingimportColordefadd_arrow_annotation():withAnnotator("./sample.pdf")asannotator:arrow=ArrowAnnotation()arrow.box=Rectangle(100,100,100,100)arrow.pen_color=Color.blue.to_argb()arrow.pen_width=2arrow.pen_style=PenStyle.SOLIDarrow.opacity=0.9arrow.page_number=0arrow.message="This is an arrow annotation"annotator.add(arrow)annotator.save("./output.pdf")if__name__=="__main__":add_arrow_annotation()
sample.pdf is the sample file used in this example. Click here to download it.
A distance annotation measures the span between two points on the page.
fromgroupdocs.annotationimportAnnotatorfromgroupdocs.annotation.modelsimportRectangle,PenStylefromgroupdocs.annotation.models.annotation_modelsimportDistanceAnnotationfromgroupdocs.pydrawingimportColordefadd_distance_annotation():withAnnotator("./sample.pdf")asannotator:distance=DistanceAnnotation()distance.box=Rectangle(100,100,100,100)distance.pen_color=Color.blue.to_argb()distance.pen_width=2distance.pen_style=PenStyle.SOLIDdistance.opacity=0.7distance.page_number=0distance.message="This is a distance annotation"annotator.add(distance)annotator.save("./output.pdf")if__name__=="__main__":add_distance_annotation()
sample.pdf is the sample file used in this example. Click here to download it.
A polyline annotation draws a free-form shape described by an SVG path inside the bounding box.
fromgroupdocs.annotationimportAnnotatorfromgroupdocs.annotation.modelsimportRectangle,PenStylefromgroupdocs.annotation.models.annotation_modelsimportPolylineAnnotationfromgroupdocs.pydrawingimportColordefadd_polyline_annotation():withAnnotator("./sample.pdf")asannotator:polyline=PolylineAnnotation()polyline.box=Rectangle(100,100,200,100)polyline.svg_path="M 0 0 L 50 50 L 100 0 L 150 50"polyline.pen_color=Color.purple.to_argb()polyline.pen_width=2polyline.pen_style=PenStyle.SOLIDpolyline.opacity=0.9polyline.page_number=0polyline.message="This is a polyline annotation"annotator.add(polyline)annotator.save("./output.pdf")if__name__=="__main__":add_polyline_annotation()
sample.pdf is the sample file used in this example. Click here to download it.
Highlight, underline, strikeout, and squiggly are text-markup annotations: they are positioned by the corner Point objects of the text region rather than a box.
fromgroupdocs.annotationimportAnnotatorfromgroupdocs.annotation.modelsimportPointfromgroupdocs.annotation.models.annotation_modelsimportHighlightAnnotationfromgroupdocs.pydrawingimportColordefadd_highlight_annotation():withAnnotator("./sample.pdf")asannotator:highlight=HighlightAnnotation()# Text-markup annotations are positioned by the corner points of the# text region: top-left, top-right, bottom-left, bottom-righthighlight.points=[Point(80,600),Point(300,600),Point(80,575),Point(300,575),]highlight.background_color=Color.yellow.to_argb()highlight.opacity=0.7highlight.page_number=0highlight.message="This is a highlight annotation"annotator.add(highlight)annotator.save("./output.pdf")if__name__=="__main__":add_highlight_annotation()
sample.pdf is the sample file used in this example. Click here to download it.
An underline annotation draws a line beneath the selected text region.
fromgroupdocs.annotationimportAnnotatorfromgroupdocs.annotation.modelsimportPointfromgroupdocs.annotation.models.annotation_modelsimportUnderlineAnnotationfromgroupdocs.pydrawingimportColordefadd_underline_annotation():withAnnotator("./sample.pdf")asannotator:underline=UnderlineAnnotation()underline.points=[Point(80,600),Point(300,600),Point(80,575),Point(300,575),]underline.underline_color=Color.red.to_argb()underline.opacity=0.9underline.page_number=0underline.message="This is an underline annotation"annotator.add(underline)annotator.save("./output.pdf")if__name__=="__main__":add_underline_annotation()
sample.pdf is the sample file used in this example. Click here to download it.
A strikeout annotation draws a line through the selected text region.
fromgroupdocs.annotationimportAnnotatorfromgroupdocs.annotation.modelsimportPointfromgroupdocs.annotation.models.annotation_modelsimportStrikeoutAnnotationfromgroupdocs.pydrawingimportColordefadd_strikeout_annotation():withAnnotator("./sample.pdf")asannotator:strikeout=StrikeoutAnnotation()strikeout.points=[Point(80,600),Point(300,600),Point(80,575),Point(300,575),]strikeout.font_color=Color.red.to_argb()strikeout.opacity=0.9strikeout.page_number=0strikeout.message="This is a strikeout annotation"annotator.add(strikeout)annotator.save("./output.pdf")if__name__=="__main__":add_strikeout_annotation()
sample.pdf is the sample file used in this example. Click here to download it.
A squiggly annotation draws a wavy line under the selected text region.
fromgroupdocs.annotationimportAnnotatorfromgroupdocs.annotation.modelsimportPointfromgroupdocs.annotation.models.annotation_modelsimportSquigglyAnnotationfromgroupdocs.pydrawingimportColordefadd_squiggly_annotation():withAnnotator("./sample.pdf")asannotator:squiggly=SquigglyAnnotation()squiggly.points=[Point(80,600),Point(300,600),Point(80,575),Point(300,575),]squiggly.squiggly_color=Color.red.to_argb()squiggly.opacity=0.9squiggly.page_number=0squiggly.message="This is a squiggly annotation"annotator.add(squiggly)annotator.save("./output.pdf")if__name__=="__main__":add_squiggly_annotation()
sample.pdf is the sample file used in this example. Click here to download it.
A text field annotation places editable text inside a box, with font and alignment control.
fromgroupdocs.annotationimportAnnotatorfromgroupdocs.annotation.modelsimportRectangle,HorizontalAlignmentfromgroupdocs.annotation.models.annotation_modelsimportTextFieldAnnotationfromgroupdocs.pydrawingimportColordefadd_text_field_annotation():withAnnotator("./sample.pdf")asannotator:text_field=TextFieldAnnotation()text_field.box=Rectangle(100,100,150,50)text_field.text="Some text in a field"text_field.font_family="Arial"text_field.font_size=12.0text_field.font_color=Color.black.to_argb()text_field.background_color=Color.yellow.to_argb()text_field.text_horizontal_alignment=HorizontalAlignment.CENTERtext_field.opacity=0.9text_field.page_number=0text_field.message="This is a text field annotation"annotator.add(text_field)annotator.save("./output.pdf")if__name__=="__main__":add_text_field_annotation()
sample.pdf is the sample file used in this example. Click here to download it.
A text replacement annotation marks a text region and provides replacement text.
fromgroupdocs.annotationimportAnnotatorfromgroupdocs.annotation.modelsimportPointfromgroupdocs.annotation.models.annotation_modelsimportReplacementAnnotationfromgroupdocs.pydrawingimportColordefadd_replacement_annotation():withAnnotator("./sample.pdf")asannotator:replacement=ReplacementAnnotation()replacement.points=[Point(80,600),Point(300,600),Point(80,575),Point(300,575),]replacement.text_to_replace="replacement text"replacement.font_color=Color.black.to_argb()replacement.font_size=12.0replacement.opacity=0.9replacement.page_number=0replacement.message="This is a text replacement annotation"annotator.add(replacement)annotator.save("./output.pdf")if__name__=="__main__":add_replacement_annotation()
sample.pdf is the sample file used in this example. Click here to download it.
A text redaction annotation hides a text region behind a solid block.
fromgroupdocs.annotationimportAnnotatorfromgroupdocs.annotation.modelsimportPointfromgroupdocs.annotation.models.annotation_modelsimportTextRedactionAnnotationfromgroupdocs.pydrawingimportColordefadd_text_redaction_annotation():withAnnotator("./sample.pdf")asannotator:redaction=TextRedactionAnnotation()redaction.points=[Point(80,600),Point(300,600),Point(80,575),Point(300,575),]redaction.font_color=Color.black.to_argb()redaction.page_number=0redaction.message="This is a text redaction annotation"annotator.add(redaction)annotator.save("./output.pdf")if__name__=="__main__":add_text_redaction_annotation()
sample.pdf is the sample file used in this example. Click here to download it.
A resources redaction annotation blacks out a rectangular region, removing the underlying page resources.
fromgroupdocs.annotationimportAnnotatorfromgroupdocs.annotation.modelsimportRectanglefromgroupdocs.annotation.models.annotation_modelsimportResourcesRedactionAnnotationdefadd_resources_redaction_annotation():withAnnotator("./sample.pdf")asannotator:redaction=ResourcesRedactionAnnotation()redaction.box=Rectangle(100,100,200,80)redaction.page_number=0redaction.message="This is a resources redaction annotation"annotator.add(redaction)annotator.save("./output.pdf")if__name__=="__main__":add_resources_redaction_annotation()
sample.pdf is the sample file used in this example. Click here to download it.
A watermark annotation places rotated, scalable text over the page.
fromgroupdocs.annotationimportAnnotatorfromgroupdocs.annotation.modelsimportRectangle,HorizontalAlignment,VerticalAlignmentfromgroupdocs.annotation.models.annotation_modelsimportWatermarkAnnotationfromgroupdocs.pydrawingimportColordefadd_watermark_annotation():withAnnotator("./sample.pdf")asannotator:watermark=WatermarkAnnotation()watermark.box=Rectangle(100,100,200,100)watermark.text="Watermark"watermark.font_family="Arial"watermark.font_size=24.0watermark.font_color=Color.red.to_argb()watermark.angle=45.0watermark.auto_scale=Truewatermark.horizontal_alignment=HorizontalAlignment.CENTERwatermark.vertical_alignment=VerticalAlignment.CENTERwatermark.opacity=0.5watermark.page_number=0watermark.message="This is a watermark annotation"annotator.add(watermark)annotator.save("./output.pdf")if__name__=="__main__":add_watermark_annotation()
sample.pdf is the sample file used in this example. Click here to download it.
A link annotation turns a text region into a clickable hyperlink.
fromgroupdocs.annotationimportAnnotatorfromgroupdocs.annotation.modelsimportPointfromgroupdocs.annotation.models.annotation_modelsimportLinkAnnotationfromgroupdocs.pydrawingimportColordefadd_link_annotation():withAnnotator("./sample.pdf")asannotator:link=LinkAnnotation()link.points=[Point(80,600),Point(300,600),Point(80,575),Point(300,575),]link.url="https://www.groupdocs.com"link.background_color=Color.azure.to_argb()link.opacity=0.7link.page_number=0link.message="This is a link annotation"annotator.add(link)annotator.save("./output.pdf")if__name__=="__main__":add_link_annotation()
sample.pdf is the sample file used in this example. Click here to download it.
An image annotation stamps a picture from disk onto the page. Set image_path to a local image file.
fromgroupdocs.annotationimportAnnotatorfromgroupdocs.annotation.modelsimportRectanglefromgroupdocs.annotation.models.annotation_modelsimportImageAnnotationdefadd_image_annotation():withAnnotator("./sample.pdf")asannotator:image=ImageAnnotation()image.box=Rectangle(100,100,100,100)image.image_path="./stamp.png"image.opacity=0.9image.angle=0.0image.page_number=0image.message="This is an image annotation"annotator.add(image)annotator.save("./output.pdf")if__name__=="__main__":add_image_annotation()
sample.pdf is the document used in this example. Click here to download it.
stamp.png is the image stamped onto the page. Click here to download it.