Skip to content

Commit

Permalink
DOC: Correct example (#2955)
Browse files Browse the repository at this point in the history
* DOC: Correct example

Variable used is 'annot' and not 'annotation'; update to reflect this.

* DOC: Rename annot to annotations

For readability and consistency of the examples.
  • Loading branch information
j-t-1 authored Nov 19, 2024
1 parent 92bb037 commit 54b1d48
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions docs/user/reading-pdf-annotations.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@ reader = PdfReader("annotated.pdf")

for page in reader.pages:
if "/Annots" in page:
for annot in page["/Annots"]:
obj = annot.get_object()
annotation = {"subtype": obj["/Subtype"], "location": obj["/Rect"]}
print(annotation)
for annotation in page["/Annots"]:
obj = annotation.get_object()
print({"subtype": obj["/Subtype"], "location": obj["/Rect"]})
```

Examples of reading three of the most common annotations:
Expand All @@ -57,10 +56,10 @@ reader = PdfReader("example.pdf")

for page in reader.pages:
if "/Annots" in page:
for annot in page["/Annots"]:
subtype = annot.get_object()["/Subtype"]
for annotation in page["/Annots"]:
subtype = annotation.get_object()["/Subtype"]
if subtype == "/Text":
print(annot.get_object()["/Contents"])
print(annotation.get_object()["/Contents"])
```

## Highlights
Expand All @@ -72,10 +71,10 @@ reader = PdfReader("example.pdf")

for page in reader.pages:
if "/Annots" in page:
for annot in page["/Annots"]:
subtype = annot.get_object()["/Subtype"]
for annotation in page["/Annots"]:
subtype = annotation.get_object()["/Subtype"]
if subtype == "/Highlight":
coords = annot.get_object()["/QuadPoints"]
coords = annotation.get_object()["/QuadPoints"]
x1, y1, x2, y2, x3, y3, x4, y4 = coords
```

Expand All @@ -90,8 +89,8 @@ attachments = {}
for page in reader.pages:
if "/Annots" in page:
for annotation in page["/Annots"]:
subtype = annot.get_object()["/Subtype"]
subtype = annotation.get_object()["/Subtype"]
if subtype == "/FileAttachment":
fileobj = annot.get_object()["/FS"]
fileobj = annotation.get_object()["/FS"]
attachments[fileobj["/F"]] = fileobj["/EF"]["/F"].get_data()
```

0 comments on commit 54b1d48

Please sign in to comment.