Skip to content

Text overlapping on addRedactAnnot and apply_redactions #1062

Closed

Description

@MallRoy

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

Image After applying redaction
image

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()

Activity

  1. JorjMcKie commented on May 27, 2021

    @JorjMcKie
    Collaborator

    Cannot recreate problem.
    This could be cause by all sorts of things. Need at least a minimal PDF page for which this happens.

  2. JorjMcKie commented on May 27, 2021

    @JorjMcKie
    Collaborator

    In the meantime you could try and unconditionally replace

                if not page._isWrapped:
                    page.wrapContents()

    by page.clean_contents().

  3. MallRoy commented on May 28, 2021

    @MallRoy
    Author

    In the meantime you could try and unconditionally replace

                if not page._isWrapped:
                    page.wrapContents()

    by page.clean_contents().

    We have tried this already, it is not working.

  4. JorjMcKie commented on May 28, 2021

    @JorjMcKie
    Collaborator

    Hm, ok.
    Then give me an example file for reproducing this please.

  5. JorjMcKie commented on May 28, 2021

    @JorjMcKie
    Collaborator

    Use my e-mail address if any confidentiality concerns.

  6. MallRoy commented on May 28, 2021

    @MallRoy
    Author

    Use my e-mail address if any confidentiality concerns.

    sure

  7. JorjMcKie commented on May 28, 2021

    @JorjMcKie
    Collaborator

    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.

  8. MallRoy commented on May 28, 2021

    @MallRoy
    Author

    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.

    Ok. I have removed the sensitive content. So I don't have any concern about that part.

  9. JorjMcKie commented on May 28, 2021

    @JorjMcKie
    Collaborator

    Good - I will open an issue at their system then and forward the link to you.

  10. MallRoy commented on May 28, 2021

    @MallRoy
    Author

    Good - I will open an issue at their system then and forward the link to you.

    ok

  11. JorjMcKie commented on May 28, 2021

    @JorjMcKie
    Collaborator
  12. MallRoy commented on May 28, 2021

    @MallRoy
    Author
  13. added
    upstream bugbug outside this package
    and removed on May 28, 2021
  14. JorjMcKie commented on May 28, 2021

    @JorjMcKie
    Collaborator

    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.

  15. locked and limited conversation to collaborators on Jul 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

postponepostpone to a future versionupstream bugbug outside this package

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions