Extract Images from PDF coordinates using iText

2019-01-15 12:09发布

问题:

I found some examples for how to extract images from PDF using iText. But what I am looking for is to get the images from PDF by coordinates.

Is it possible? If yes then how it can be done.

回答1:

Along the lines of the iText example ExtractImages you can extract code like this:

PdfReader reader = new PdfReader(resourceStream);
PdfReaderContentParser parser = new PdfReaderContentParser(reader);
ImageRenderListener listener = new ImageRenderListener("testpdf");

for (int i = 1; i <= reader.getNumberOfPages(); i++) {
    parser.processContent(i, listener);
}

The ImageRenderListener is defined like this:

class ImageRenderListener implements RenderListener
{
    final String name;
    int counter = 100000;

    public ImageRenderListener(String name)
    {
        this.name = name;
    }

    public void beginTextBlock() { }
    public void renderText(TextRenderInfo renderInfo) { }
    public void endTextBlock() { }

    public void renderImage(ImageRenderInfo renderInfo)
    {
        try
        {
            PdfImageObject image = renderInfo.getImage();
            if (image == null) return;
            int number = renderInfo.getRef() != null ? renderInfo.getRef().getNumber() : counter++;
            String filename = String.format("%s-%s.%s", name, number, image.getFileType());
            FileOutputStream os = new FileOutputStream(filename);
            os.write(image.getImageAsBytes());
            os.flush();
            os.close();

            PdfDictionary imageDictionary = image.getDictionary();
            PRStream maskStream = (PRStream) imageDictionary.getAsStream(PdfName.SMASK);
            if (maskStream != null)
            {
                PdfImageObject maskImage = new PdfImageObject(maskStream);
                filename = String.format("%s-%s-mask.%s", name, number, maskImage.getFileType());
                os = new FileOutputStream(filename);
                os.write(maskImage.getImageAsBytes());
                os.flush();
                os.close();
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}

As you see the ImageRenderListener method renderImage retrieves an argument ImageRenderInfo. This arguments has methods

  • getStartPoint giving you a vector in User space representing the start point of the xobject and
  • getImageCTM giving you the coordinate transformation matrix active when this image was rendered. Coordinates are in User space.

The latter gives you the information which exact manipulation on a 1x1 user space unit square are used to actually draw the image. As you are aware, an image may be rotated, stretched, skewed, and moved (the former method actually extracts its result from the matrix from the "moved" information).



标签: image pdf itext