MCPcopy Create free account
hub / github.com/pymupdf/PyMuPDF / apply_redactions

Function apply_redactions

src_classic/utils.py:4050–4139  ·  view source on GitHub ↗

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.

(page: Page, images: int = 2)

Source from the content-addressed store, hash-verified

4048
4049
4050def apply_redactions(page: Page, images: int = 2) -> bool:
4051 """Apply the redaction annotations of the page.
4052
4053 Args:
4054 page: the PDF page.
4055 images: 0 - ignore images, 1 - remove complete overlapping image,
4056 2 - blank out overlapping image parts.
4057 """
4058
4059 def center_rect(annot_rect, text, font, fsize):
4060 """Calculate minimal sub-rectangle for the overlay text.
4061
4062 Notes:
4063 Because 'insert_textbox' supports no vertical text centering,
4064 we calculate an approximate number of lines here and return a
4065 sub-rect with smaller height, which should still be sufficient.
4066 Args:
4067 annot_rect: the annotation rectangle
4068 text: the text to insert.
4069 font: the fontname. Must be one of the CJK or Base-14 set, else
4070 the rectangle is returned unchanged.
4071 fsize: the fontsize
4072 Returns:
4073 A rectangle to use instead of the annot rectangle.
4074 """
4075 if not text:
4076 return annot_rect
4077 try:
4078 text_width = get_text_length(text, font, fsize)
4079 except ValueError: # unsupported font
4080 return annot_rect
4081 line_height = fsize * 1.2
4082 limit = annot_rect.width
4083 h = math.ceil(text_width / limit) * line_height # estimate rect height
4084 if h >= annot_rect.height:
4085 return annot_rect
4086 r = annot_rect
4087 y = (annot_rect.tl.y + annot_rect.bl.y - h) * 0.5
4088 r.y0 = y
4089 return r
4090
4091 CheckParent(page)
4092 doc = page.parent
4093 if doc.is_encrypted or doc.is_closed:
4094 raise ValueError("document closed or encrypted")
4095 if not doc.is_pdf:
4096 raise ValueError("is no PDF")
4097
4098 redact_annots = [] # storage of annot values
4099 for annot in page.annots(types=(PDF_ANNOT_REDACT,)): # loop redactions
4100 redact_annots.append(annot._get_redact_values()) # save annot values
4101
4102 if redact_annots == []: # any redactions on this page?
4103 return False # no redactions
4104
4105 rc = page._apply_redactions(images) # call MuPDF redaction process step
4106 if not rc: # should not happen really
4107 raise ValueError("Error applying redactions.")

Callers

nothing calls this directly

Calls 12

CheckParentFunction · 0.85
center_rectFunction · 0.85
annotsMethod · 0.80
_get_redact_valuesMethod · 0.80
_apply_redactionsMethod · 0.80
new_shapeMethod · 0.80
appendMethod · 0.45
draw_rectMethod · 0.45
finishMethod · 0.45
getMethod · 0.45
insert_textboxMethod · 0.45
commitMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…