Remove pdf image using itext

1

I am adding image in pdf with itext, however in some moments I need to remove this same image .. how do I do this so as not to corrupt the reading of it? I have tried in some ways, as follows:

The first attempt was to clean the streams of the image.

    PdfReader reader = new PdfReader(src);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));

    for(int i = 0; i < reader.getXrefSize(); i++)
    {
        PdfObject obj = reader.getPdfObject(i);
        if(obj == null)
        {
            continue;
        }

        if(obj.isStream())
        {
            PdfStream stream = (PdfStream) obj;

            PdfObject subtype = stream.get(PdfName.SUBTYPE);

            if(PdfName.IMAGE.equals(subtype))
            {
                stream.clear();
            }
        }
    }

    stamper.close();
    reader.close();

This causes error when reading the file with Adobe Reader, this reader understands that the file is corrupted. Other readers like Foxit works perfectly.

The second attempt was to change the properties of the image, ie change the height and width to 1px. But when I change the properties, it's still a "movie" of the same size as the image, which I can not remove.

   PdfReader reader = new PdfReader(src);
   PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));

    for(int i = 0; i < reader.getXrefSize(); i++)
    {
        PdfObject obj = reader.getPdfObject(i);
        if(obj == null)
        {
            continue;
        }

        if(obj.isStream())
        {
            PdfStream stream = (PdfStream) obj;

            PdfObject subtype = stream.get(PdfName.SUBTYPE);

            if(PdfName.IMAGE.equals(subtype))
            {
                stream.put(PdfName.WIDTH, new PdfNumber(1));
                stream.put(PdfName.HEIGHT, new PdfNumber(1));
            }
        }
    }

    stamper.close();
    reader.close();

Would anyone know what I can do to remove the image?

    
asked by anonymous 05.07.2016 / 20:06

0 answers