The Annotator class can open a document from a file path or any readable binary stream, including password-protected files. The examples below show each loading scenario.
Load from local disk
When the document is on the local disk, pass its path to the Annotator constructor.
fromgroupdocs.annotationimportAnnotatorfromgroupdocs.annotation.modelsimportRectanglefromgroupdocs.annotation.models.annotation_modelsimportAreaAnnotationfromgroupdocs.pydrawingimportColordefload_from_local_disk():# Load a document directly from a local file pathwithAnnotator("./sample.pdf")asannotator:area=AreaAnnotation()area.box=Rectangle(100,100,100,100)area.background_color=Color.yellow.to_argb()area.page_number=0area.message="Loaded from local disk"annotator.add(area)annotator.save("./output.pdf")if__name__=="__main__":load_from_local_disk()
sample.pdf is the sample file used in this example. Click here to download it.
As an alternative to a local file, pass an open binary stream to the document parameter of the Annotator constructor.
fromgroupdocs.annotationimportAnnotatorfromgroupdocs.annotation.modelsimportRectanglefromgroupdocs.annotation.models.annotation_modelsimportAreaAnnotationfromgroupdocs.pydrawingimportColordefload_from_stream():# Load a document from an open binary streamwithopen("./sample.pdf","rb")asstream:withAnnotator(document=stream)asannotator:area=AreaAnnotation()area.box=Rectangle(100,100,100,100)area.background_color=Color.yellow.to_argb()area.page_number=0area.message="Loaded from stream"annotator.add(area)annotator.save("./output.pdf")if__name__=="__main__":load_from_stream()
sample.pdf is the sample file used in this example. Click here to download it.
To open an encrypted document, set the password property of LoadOptions and pass it to the Annotator through the load_options parameter.
fromgroupdocs.annotationimportAnnotatorfromgroupdocs.annotation.optionsimportLoadOptionsfromgroupdocs.annotation.modelsimportRectanglefromgroupdocs.annotation.models.annotation_modelsimportAreaAnnotationfromgroupdocs.pydrawingimportColordefload_password_protected_document():# Provide the password through LoadOptionsload_options=LoadOptions()load_options.password="secret"withAnnotator("./protected.docx",load_options=load_options)asannotator:area=AreaAnnotation()area.box=Rectangle(100,100,100,100)area.background_color=Color.yellow.to_argb()area.page_number=0area.message="Annotated a password-protected document"annotator.add(area)annotator.save("./output.docx")if__name__=="__main__":load_password_protected_document()
protected.docx is the sample file used in this example. Click here to download it.