Repository navigation
Text overlapping on addRedactAnnot and apply_redactions #1062
Copy link
Copy link
Closed
Labels
postponepostpone to a future versionpostpone to a future versionupstream bugbug outside this packagebug outside this package
Description
I am facing a text overlapping issue on applying redactions in a pdf.
I am using python 3.9.2 and pip 20.2.3 versions.
Image before applying redaction

Image After applying redaction

I am using the below code for redaction
# imports
import fitz
import re
import sys
import json
from matplotlib import colors
class Redactor:
# constructor
def __init__(self, path, desc, out, vertexes, color):
self.path = path
self.desc = desc.split(',')
self.out = out
self.vertexes = vertexes
self.color = color
def redaction(self):
# opening the pdf
doc = fitz.open(self.path)
# iterating through pages
for page in doc:
if not page._isWrapped:
page.wrapContents()
text = page.getText('text')
currentpage = page.number
dl = page.getDisplayList()
tp = dl.getTextPage()
# print(self.vertexes[currentpage])
for searchtext in self.desc or []:
# print(searchtext)
areas = tp.search(searchtext, quads=True)
# print(areas)
if (areas is not None):
[page.addRedactAnnot(area, fill=colors.to_rgb(
self.color)) for area in areas]
page.apply_redactions()
# print(areas)
for vertices in self.vertexes[currentpage]["vertices"]:
# print(vertices)
clip = fitz.Quad(((vertices[0]["x"], vertices[0]["y"]), (vertices[1]["x"], vertices[1]["y"]), (
vertices[2]["x"], vertices[2]["y"]), (vertices[3]["x"], vertices[3]["y"])))
page.addRedactAnnot(clip, fill=colors.to_rgb(self.color))
page.apply_redactions()
# saving it to a new pdf
doc.save(self.out, deflate=True, clean=True, linear=True)
print("Successfully redacted")
# driver code for testing
if __name__ == "__main__":
# replace it with name of the pdf file
path = sys.argv[1]
desc = sys.argv[2]
out = sys.argv[3]
color = sys.argv[4]
lines = sys.stdin.readlines()
gcpvalues = json.loads(lines[0])
vertexes = gcpvalues["vertex"]
redactor = Redactor(path, desc, out, vertexes, color)
redactor.redaction()
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
postponepostpone to a future versionpostpone to a future versionupstream bugbug outside this packagebug outside this package
Activity
JorjMcKie commentedon May 27, 2021
Cannot recreate problem.
This could be cause by all sorts of things. Need at least a minimal PDF page for which this happens.
JorjMcKie commentedon May 27, 2021
In the meantime you could try and unconditionally replace
by
page.clean_contents().MallRoy commentedon May 28, 2021
We have tried this already, it is not working.
JorjMcKie commentedon May 28, 2021
Hm, ok.
Then give me an example file for reproducing this please.
JorjMcKie commentedon May 28, 2021
Use my e-mail address if any confidentiality concerns.
MallRoy commentedon May 28, 2021
sure
JorjMcKie commentedon May 28, 2021
Thanks for the file.
This is an upstream (MuPDF) error. If you agree, I will send them the file and a reproducer program.
Your file does not seem to contain confidential information, so that shouldn't be a problem.
Apart from the fact that these people are absolutely trustworthy.
MallRoy commentedon May 28, 2021
Ok. I have removed the sensitive content. So I don't have any concern about that part.
JorjMcKie commentedon May 28, 2021
Good - I will open an issue at their system then and forward the link to you.
MallRoy commentedon May 28, 2021
ok
JorjMcKie commentedon May 28, 2021
Here is the link: https://bugs.ghostscript.com/show_bug.cgi?id=703894
MallRoy commentedon May 28, 2021
Thankyou @JorjMcKie
JorjMcKie commentedon May 28, 2021
You can open an accunt on bugzilla and enter your e-mail address as to be notified when there are status changes of the issue.