By default save() writes every annotation back into the document. With SaveOptions you can persist only certain annotation types or limit the output to a range of pages. Pass the configured SaveOptions to save() through the save_options parameter.
Save specific annotation types
Set annotation_types to an AnnotationType value to write only annotations of that type, even if the document contains others.
fromgroupdocs.annotationimportAnnotatorfromgroupdocs.annotation.modelsimportRectanglefromgroupdocs.annotation.models.annotation_modelsimportAreaAnnotation,EllipseAnnotationfromgroupdocs.annotation.optionsimportSaveOptions,AnnotationTypefromgroupdocs.pydrawingimportColordefsave_specific_annotation_types():withAnnotator("./sample.pdf")asannotator:area=AreaAnnotation()area.box=Rectangle(100,100,100,100)area.background_color=Color.yellow.to_argb()area.page_number=0area.message="Area"ellipse=EllipseAnnotation()ellipse.box=Rectangle(100,250,150,80)ellipse.background_color=Color.from_argb(255,144,238,144).to_argb()ellipse.page_number=0ellipse.message="Ellipse"annotator.add(area)annotator.add(ellipse)# Persist only the area annotationssave_options=SaveOptions()save_options.annotation_types=AnnotationType.AREAannotator.save("./output.pdf",save_options=save_options)if__name__=="__main__":save_specific_annotation_types()
sample.pdf is the sample file used in this example. Click here to download it.
Set first_page and last_page to write only a range of pages. These values are 1-based — page 1 is the first page.
fromgroupdocs.annotationimportAnnotatorfromgroupdocs.annotation.modelsimportRectanglefromgroupdocs.annotation.models.annotation_modelsimportAreaAnnotationfromgroupdocs.annotation.optionsimportSaveOptionsfromgroupdocs.pydrawingimportColordefsave_specific_pages():withAnnotator("./multipage_sample.pdf")asannotator:area=AreaAnnotation()area.box=Rectangle(100,100,100,100)area.background_color=Color.yellow.to_argb()area.page_number=0area.message="Page 1 annotation"annotator.add(area)# first_page / last_page are 1-based for SaveOptionssave_options=SaveOptions()save_options.first_page=1save_options.last_page=2annotator.save("./output.pdf",save_options=save_options)if__name__=="__main__":save_specific_pages()
multipage_sample.pdf is the sample file used in this example. Click here to download it.