Read the annotations¶
ozaru.annotations returns the annotations of every page of a PDF as
Annotation objects: the links, the notes and comments, the highlights, and the
widgets of form fields.
import ozaru
for annotation in ozaru.annotations("annotated.pdf"):
if annotation.uri:
print(f"page {annotation.page}: link to {annotation.uri}")
elif annotation.target_page:
print(f"page {annotation.page}: link to page {annotation.target_page}")
elif annotation.contents:
print(f"page {annotation.page}: {annotation.subtype} {annotation.contents!r}")
Output:
page 1: link to https://example.com
page 1: link to page 2
page 1: Highlight 'note one'
page 1: Text 'A sticky note'
page 1: FreeText 'Free text here'
What an annotation tells¶
subtypeis what kind it is, as the PDF names it:Link,Textfor a note,Highlight,FreeText,Square,Widget...rectis where it is, as(x0, y0, x1, y1)in points from the top left corner of the page, the same as the box of aWord. That makes it easy to ask which words a link covers.contents,author,subject,nameandmodifiedare the text of a note and who wrote it, when the PDF has them, andNonewhen it doesn't.coloris its color, as 1, 3 or 4 numbers from 0 to 1, andflagsthe bits of the PDF standard: 2 is hidden and 4 is print.quad_pointsare the four corners of each line a highlight covers.uriis where a link to the web goes, andtarget_pagethe page of the same PDF a link goes to, counted from 1. A link to a named destination is followed to its page.
Drawing them¶
render_page draws the annotations that have an appearance, has_appearance,
and the widgets of form fields, as a viewer does. Pass annotations=False to
draw the page without them. Hidden annotations, and pop-ups, aren't drawn.
Protected and damaged PDFs¶
Pass password for a protected PDF. A page that can't be read has no
annotations, and a PageWarning tells you its number.