Loading documents
Contents
[
Hide
]
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.
from groupdocs.annotation import Annotator
from groupdocs.annotation.models import Rectangle
from groupdocs.annotation.models.annotation_models import AreaAnnotation
from groupdocs.pydrawing import Color
def load_from_local_disk():
# Load a document directly from a local file path
with Annotator("./sample.pdf") as annotator:
area = AreaAnnotation()
area.box = Rectangle(100, 100, 100, 100)
area.background_color = Color.yellow.to_argb()
area.page_number = 0
area.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.
Binary file (PDF, 90 KB)
Load from stream
As an alternative to a local file, pass an open binary stream to the document parameter of the Annotator constructor.
from groupdocs.annotation import Annotator
from groupdocs.annotation.models import Rectangle
from groupdocs.annotation.models.annotation_models import AreaAnnotation
from groupdocs.pydrawing import Color
def load_from_stream():
# Load a document from an open binary stream
with open("./sample.pdf", "rb") as stream:
with Annotator(document=stream) as annotator:
area = AreaAnnotation()
area.box = Rectangle(100, 100, 100, 100)
area.background_color = Color.yellow.to_argb()
area.page_number = 0
area.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.
Binary file (PDF, 90 KB)
Load a password-protected file
To open an encrypted document, set the password property of LoadOptions and pass it to the Annotator through the load_options parameter.
from groupdocs.annotation import Annotator
from groupdocs.annotation.options import LoadOptions
from groupdocs.annotation.models import Rectangle
from groupdocs.annotation.models.annotation_models import AreaAnnotation
from groupdocs.pydrawing import Color
def load_password_protected_document():
# Provide the password through LoadOptions
load_options = LoadOptions()
load_options.password = "secret"
with Annotator("./protected.docx", load_options=load_options) as annotator:
area = AreaAnnotation()
area.box = Rectangle(100, 100, 100, 100)
area.background_color = Color.yellow.to_argb()
area.page_number = 0
area.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.
Binary file (DOCX, 10 KB)