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
Supply Agreement
Harbor Lane Provisions and Northwind Coffee Roasters
1. Parties
Buyer: Harbor Lane Provisions, 18 Quayside Road, Port Ellery, represented by Lena Park, Procurement
Lead.
Supplier: Northwind Coffee Roasters, 4 Roastery Lane, Millbrook, represented by Alex Chen.
This agreement takes effect on 12 January 2026 and replaces the agreement of January 2023.
2. Products
The Supplier roasts and delivers the following coffees to the Buyer's Quayside warehouse:
• Harbor Blend
[TRUNCATED]
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
"Check the 2,400 kg against last season's sell-through before we sign.": False / True
'Release date works for the Q4 cash forecast.': False / True
'Ask Northwind for the green coffee index they will use.': False / True
'Dock 2 is reserved for these two mornings.': False / True