Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Annotate PDF documents using a REST API on the cloud in Python
Annotate PDF documents using Python
1. Programmatically upload PDF file on the cloud
2. Add Annotations to the uploaded PDF File programmatically using Python
3. Download the annotated file from the cloud
# initialize api
api = groupdocs_annotation_cloud.AnnotateApi.from_config(configuration)
# define annotation
a1 = groupdocs_annotation_cloud.AnnotationInfo()
a1.box = groupdocs_annotation_cloud.Rectangle()
a1.box.x = 300
a1.box.y = 300
a1.box.width = 200
a1.box.height = 100
a1.page_number = 0
a1.pen_color = 1201033
a1.pen_style = "Solid"
a1.pen_width = 1
a1.opacity = 0.7
a1.type = "Image"
a1.text = "This is Image annotation"
a1.creator_name = "Anonym A."
a1.image_path = "JohnSmith.png"
# input file path
file_info = groupdocs_annotation_cloud.FileInfo()
file_info.file_path = "sample.pdf"
# define annotation options
options = groupdocs_annotation_cloud.AnnotateOptions()
options.file_info = file_info
options.annotations = [a1]
options.output_path = "output_img.pdf"
# create annotation request
request = groupdocs_annotation_cloud.AnnotateRequest(options)
result = api.annotate(request)
print("AddImageAnnotation: Image Annotation added: " + result['href'])
# initialize api
api = groupdocs_annotation_cloud.AnnotateApi.from_config(configuration)
# provide annotation information
a1 = groupdocs_annotation_cloud.AnnotationInfo()
p1 = groupdocs_annotation_cloud.Point()
p1.x = 80
p1.y = 710
p2 = groupdocs_annotation_cloud.Point()
p2.x = 240
p2.y = 710
p3 = groupdocs_annotation_cloud.Point()
p3.x = 80
p3.y = 650
p4 = groupdocs_annotation_cloud.Point()
p4.x = 240
p4.y = 650
a1.points = [p1, p2, p3, p4]
a1.page_number = 0
a1.type = "Link"
a1.text = "This is Link annotation"
a1.creator_name = "Anonym A."
a1.url = "https://www.groupdocs.com/"
# input file path
file_info = groupdocs_annotation_cloud.FileInfo()
file_info.file_path = "sample.pdf"
# define annotation options
options = groupdocs_annotation_cloud.AnnotateOptions()
options.file_info = file_info
options.annotations = [a1]
options.output_path = "output.pdf"
# create annotation request
request = groupdocs_annotation_cloud.AnnotateRequest(options)
result = api.annotate(request)
print("AddLinkAnnotation: Link Annotation added: " + result['href'])
# initialize api
api = groupdocs_annotation_cloud.AnnotateApi.from_config(configuration)
# distance annotation
a1 = groupdocs_annotation_cloud.AnnotationInfo()
a1.annotation_position = groupdocs_annotation_cloud.Point()
a1.annotation_position.x = 1
a1.annotation_position.y = 1
a1.box = groupdocs_annotation_cloud.Rectangle()
a1.box.x = 100
a1.box.y = 100
a1.box.width = 200
a1.box.height = 200
a1.page_number = 0
a1.pen_color = 1201033
a1.pen_style = "Solid"
a1.pen_width = 3
a1.opacity = 1
a1.type = "Distance"
a1.text = "This is distance annotation"
a1.creator_name = "Anonym A."
# area annotation
a2 = groupdocs_annotation_cloud.AnnotationInfo()
a2.annotation_position = groupdocs_annotation_cloud.Point()
a2.annotation_position.x = 1
a2.annotation_position.y = 1
a2.box = groupdocs_annotation_cloud.Rectangle()
a2.box.x = 100
a2.box.y = 400
a2.box.width = 200
a2.box.height = 100
a2.page_number = 0
a2.pen_color = 1201033
a2.pen_style = "Solid"
a2.pen_width = 3
a2.opacity = 1
a2.type = "Area"
a2.text = "This is area annotation"
a2.creator_name = "Anonym A."
# arrow annotation
a3 = groupdocs_annotation_cloud.AnnotationInfo()
a3.annotation_position = groupdocs_annotation_cloud.Point()
a3.annotation_position.x = 1
a3.annotation_position.y = 1
a3.box = groupdocs_annotation_cloud.Rectangle()
a3.box.x = 100
a3.box.y = 250
a3.box.width = 100
a3.box.height = 50
a3.page_number = 0
a3.pen_color = 1201033
a3.pen_style = "Solid"
a3.pen_width = 1
a3.opacity = 0.7
a3.type = "Arrow"
a3.text = "This is arrow annotation"
a3.creator_name = "Anonym A."
# input file
file_info = groupdocs_annotation_cloud.FileInfo()
file_info.file_path = "sample.pdf"
# define annotation options
options = groupdocs_annotation_cloud.AnnotateOptions()
options.file_info = file_info
options.annotations = [a1, a2, a3]
options.output_path = "output.pdf"
# create annotation request
request = groupdocs_annotation_cloud.AnnotateRequest(options)
result = api.annotate(request)
print("AddMultipleAnnotations: Multiple Annotations added: " + result['href'])
# initialize api
api = groupdocs_annotation_cloud.AnnotateApi.from_config(configuration)
# provide annotation information
a1 = groupdocs_annotation_cloud.AnnotationInfo()
a1.annotation_position = groupdocs_annotation_cloud.Point()
a1.annotation_position.x = 1
a1.annotation_position.y = 1
a1.box = groupdocs_annotation_cloud.Rectangle()
a1.box.x = 380
a1.box.y = 300
a1.box.width = 100
a1.box.height = 50
a1.page_number = 0
a1.font_color = 1201033
a1.font_size = 12
a1.opacity = 0.7
a1.type = "TextField"
a1.text = "Text field text"
a1.creator_name = "Anonym A."
# input file path
file_info = groupdocs_annotation_cloud.FileInfo()
file_info.file_path = "sample.pdf"
# define annotation options
options = groupdocs_annotation_cloud.AnnotateOptions()
options.file_info = file_info
options.annotations = [a1]
options.output_path = "output.pdf"
# create annotation request
request = groupdocs_annotation_cloud.AnnotateRequest(options)
result = api.annotate(request)
print("AddTextFieldAnnotation: Text Field Annotation added: " + result['href'])
client_id = "659fe7da-715b-4744-a0f7-cf469a392b73"
client_secret = "b377c36cfa28fa69960ebac6b6e36421"
configuration = groupdocs_annotation_cloud.Configuration(client_id, client_secret)
configuration.api_base_url = "https://api.groupdocs.cloud"
my_storage = ""
# api initialization
file_api = groupdocs_annotation_cloud.FileApi.from_config(configuration)
my_storage = ""
# create download request
request = groupdocs_annotation_cloud.DownloadFileRequest("output.pdf", my_storage)
response = file_api.download_file(request)
# Move downloaded file to your working directory
shutil.move(response, "C:\\Files\\")
# create instance of the api
file_api = groupdocs_annotation_cloud.FileApi.from_config(configuration)
my_storage = ""
# upload sample file
request = groupdocs_annotation_cloud.UploadFileRequest("sample.pdf", "C:\\Files\\sample.pdf", my_storage)
response = file_api.upload_file(request)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment