1

I am trying to remove QR code from each page using iText7 but it is distorting every other text values and components of the page, is there a way to do it in a proper way. Below is the code I am using:

LoopStarts{

        PdfPage page = pdfDoc.getPage(i);
        PdfDictionary resources = page.getResources().getPdfObject();

        // Retrieve all image objects on the page
        PdfDictionary xObjects = resources.getAsDictionary(PdfName.XObject);
        if (xObjects != null) {
            for (PdfName name : xObjects.keySet()) {
                PdfObject obj = xObjects.get(name);
                if (obj instanceof PdfStream) {
                    PdfStream stream = (PdfStream) obj;
                    PdfImageXObject image = new PdfImageXObject(stream);

                    // Get the width and height from the image dictionary
                    float width = image.getWidth();
                    float height = image.getHeight();

                    // Check if the image dimensions match those of a typical QR code
                    if (isLikelyQRCode(width, height)) {
                        // Clear image content to effectively remove it
                        stream.clear();
                    }
                }
            }
        }
    
LoopEnds}


// Method to determine if an image matches the typical size of a QR code
`private static boolean isLikelyQRCode(float width, float height) {
    // QR codes are typically square or nearly square
    return width >= 50f && width <= 200f &&
           height >= 50f && height <= 200f &&
           Math.abs(width - height) < 10; 
}`

I tried 'ImageRenderListener' but couldn't get it as expected so I am open to any better solution.

2
  • While the code you share is likely to change many different kinds of objects, it is not likely to distort text. Can you share an example PDF where text is distorted to analyze what's happening? That being said, for a routine to properly remove QR codes, you first need to know how they technically are included in the PDF: are they part of the page content, or of some embedded form XObject or pattern or font? Or are they in annotations? Are they generated using bitmap images or using vector images? ... Commented Oct 27, 2024 at 13:11
  • Not all QR codes will be based on a raster image either. So only looking into images will not remove all QR codes. A workable approach could be to scan the page with a QR code scanner that provides location information and use this information to remove the actual content, for example with itext pdfsweep or drawing a rectangle over it. Commented Oct 30, 2024 at 8:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.