Skip to content

Commit

Permalink
Add support for redacting vector graphics
Browse files Browse the repository at this point in the history
Add support for redacting vector graphics

Also add a quick fix for property Annot.irt_xref

Update test_2548.py
  • Loading branch information
JorjMcKie committed Mar 7, 2024
1 parent f8f4db5 commit 2a4ee0a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
6 changes: 4 additions & 2 deletions src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,8 @@ def info(self):

return res

def irt_xref():
@property
def irt_xref(self):

This comment has been minimized.

Copy link
@okuvshynov

okuvshynov Mar 20, 2024

Thanks a lot for fixing this, I just bumped into irt_xref issue today and was wondering if I'm missing something. What's typical release cadence for pymypdf?

Thanks again!

'''
annotation IRT xref
'''
Expand Down Expand Up @@ -7605,11 +7606,12 @@ def _addWidget(self, field_type, field_name):
JM_add_annot_id(annot, "W")
return Annot(annot)

def _apply_redactions(self, images):
def _apply_redactions(self, images, graphics):
page = self._pdf_page()
opts = mupdf.PdfRedactOptions()
opts.black_boxes = 0 # no black boxes
opts.image_method = images # how to treat images
opts.line_art = graphics # how to treat vector graphics
ASSERT_PDF(page)
success = mupdf.pdf_redact_page(page.doc(), page, opts)
return success
Expand Down
19 changes: 13 additions & 6 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1213,7 +1213,7 @@ def set_metadata(doc: fitz.Document, m: dict) -> None:
info_xref = 0
else:
info_xref = int(temp.replace("0 R", ""))

if m == {} and info_xref == 0: # nothing to do
return

Expand Down Expand Up @@ -4187,13 +4187,20 @@ def commit(self, overlay: bool = True) -> None:
return


def apply_redactions(page: fitz.Page, images: int = 2) -> bool:
def apply_redactions(page: fitz.Page, images: int = 2, graphics: int = 1) -> bool:
"""Apply the redaction annotations of the page.
Args:
page: the PDF page.
images: 0 - ignore images, 1 - remove complete overlapping image,
2 - blank out overlapping image parts.
images:
0 - ignore images
1 - remove all overlapping images
2 - blank out overlapping image parts
3 - remove image unless invisible
graphics:
0 - ignore graphics
1 - remove graphics if contained in rectangle
2 - remove all overlapping graphics
"""

def center_rect(annot_rect, text, font, fsize):
Expand Down Expand Up @@ -4246,7 +4253,7 @@ def center_rect(annot_rect, text, font, fsize):
if redact_annots == []: # any redactions on this page?
return False # no redactions

rc = page._apply_redactions(images) # call MuPDF redaction process step
rc = page._apply_redactions(images, graphics) # call MuPDF
if not rc: # should not happen really
raise ValueError("Error applying redactions.")

Expand Down Expand Up @@ -4504,7 +4511,7 @@ def append_this(pos, text):
pos, text, font=font, fontsize=fontsize, small_caps=small_caps
)
return ret

tolerance = fontsize * 0.2 # extra distance to left border
space_len = textlen(" ")
std_width = rect.width - tolerance
Expand Down
17 changes: 16 additions & 1 deletion tests/test_annots.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def test_stamp():
page = doc.reload_page(page)


def test_redact():
def test_redact1():
doc = fitz.open()
page = doc.new_page()
annot = page.add_redact_annot(r, text="Hello")
Expand All @@ -168,6 +168,21 @@ def test_redact():
assert s == r
page.apply_redactions()


def test_redact2():
"""Test removal of graphics (line art)."""
if not hasattr(fitz, "mupdf"):
print("Not executing 'test_redact2' in classic")
return
doc = fitz.open()
page = doc.new_page()
rect = fitz.Rect(100, 100, 200, 200)
page.draw_rect(rect)
page.add_redact_annot(rect)
page.apply_redactions(graphics=2)
assert page.get_drawings() == []


def test_1645():
'''
Test fix for #1645.
Expand Down

0 comments on commit 2a4ee0a

Please sign in to comment.