Sometimes you want the document’s text and its annotations in one pass, for example to index a reviewed document for search or to give an AI model the whole picture. Set include_annotations to True on TextOptions and pass the options to get_text: the annotations’ text is included in the extracted text, after the text of the page each annotation is on.
Extract text together with annotations
fromgroupdocs.parserimportParserfromgroupdocs.parser.optionsimportTextOptionswithParser("./reviewed.pdf")asparser:# Ask for the annotations' text together with the document textoptions=TextOptions(False)options.include_annotations=Truetext=parser.get_text(options)iftextisNone:print("Text extraction isn't supported for this format")else:print(text)
The following sample file is used in this example: reviewed.pdf
Expected behavior: Prints the document text. The two annotations of the sample’s first page follow the text of that page.
TextOptions(False) keeps the default (accurate) extraction mode; include_annotations is False by default.
Compare text with and without annotations
fromgroupdocs.parserimportParserfromgroupdocs.parser.optionsimportTextOptionswithParser("./reviewed.pdf")asparser:options=TextOptions(False)options.include_annotations=Trueplain=parser.get_text()with_annotations=parser.get_text(options)# Annotation text appears only in the second resultforannotationinparser.get_annotations()or[]:print("%r: %s / %s"%(annotation.value,annotation.valueinplain,annotation.valueinwith_annotations))
The following sample file is used in this example: reviewed.pdf
Expected behavior: For the sample, prints:
'Approve the hero crop before the print run.': False / True
'Aurora Visuals portfolio': False / True
The command line does the same with groupdocs-parser text reviewed.pdf --include-annotations.