Skip to content

Commit

Permalink
Make Page.crop(...) also crop .annots/.hyperlinks
Browse files Browse the repository at this point in the history
  • Loading branch information
jsvine committed Aug 4, 2024
1 parent b16acc3 commit 22494e8
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ All notable changes to this project will be documented in this file. The format
### Fixed

- Fix error on getting `.annots`/`.hyperlinks` from `CroppedPage` (due to missing `.rotation` and `.initial_doctop` attributes) (h/t @Safrone). ([#1171](https://github.com/jsvine/pdfplumber/issues/1171) + [e5737d2](https://github.com/jsvine/pdfplumber/commit/e5737d2))
- Fix problem where `Page.crop(...)` was not cropping `.annots/.hyperlinks` (h/t @Safrone). [#1171](https://github.com/jsvine/pdfplumber/issues/1171)
- Fix calculation of coordinates for `.annots` on `CroppedPage`s. ([0bbb340](https://github.com/jsvine/pdfplumber/commit/0bbb340))
- Dereference structure element attributes (h/t @dhdaines). ([#1169](https://github.com/jsvine/pdfplumber/pull/1169))
- Fix `Page.get_attr(...)` so that it fully resolves references before determining whether the attribute's value is `None` (h/t @zzhangyun + @mkl-public). ([#1176](https://github.com/jsvine/pdfplumber/issues/1176))
Expand Down
6 changes: 5 additions & 1 deletion pdfplumber/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,11 @@ def parse(annot: T_obj) -> T_obj:
return parsed

raw = resolve_all(self.page_obj.annots) or []
return list(map(parse, raw))
parsed = list(map(parse, raw))
if isinstance(self, CroppedPage):
return self._crop_fn(parsed)
else:
return parsed

@property
def hyperlinks(self) -> T_obj_list:
Expand Down
4 changes: 4 additions & 0 deletions tests/test_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ def test_annots_cropped(self):
assert len(cropped.annots) == 13
assert len(cropped.hyperlinks) == 1

h0_bbox = pdfplumber.utils.obj_to_bbox(page.hyperlinks[0])
cropped = page.crop(h0_bbox)
assert len(cropped.annots) == len(cropped.hyperlinks) == 1

def test_annots_rotated(self):
def get_annot(filename, n=0):
path = os.path.join(HERE, "pdfs", filename)
Expand Down

0 comments on commit 22494e8

Please sign in to comment.