以pdf格式获取图像的坐标 [英] Get co-ordinates of image in pdf

查看:1053
本文介绍了以pdf格式获取图像的坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我想找到pdf中的图像坐标。我尝试搜索itext和pdfbox,但我没有成功。使用这些坐标和提取的图像,我想验证提取的图像是否与数据库中存在的图像相同,图像的坐标与数据库中的相同。

In my project, I want to find co-ordinates of image in pdf. I tried searching itext and pdfbox, but I was not succesful. Using these co-ordinates and extracted image, I want to verify whether the extracted image is same as image present in database, and co-ordinates of image are same as present in database.

推荐答案

当你说你尝试过使用iText时,我认为你已经使用了 ExtractImages 示例作为代码的起点。此示例使用帮助程序类 MyImageRenderListener ,它实现 RenderListener interface。

When you say that you've tried with iText, I assume that you've used the ExtractImages example as the starting point for your code. This example uses the helper class MyImageRenderListener, which implements the RenderListener interface.

在该助手类中, renderImage()方法实现为这个:

In that helper class the renderImage() method is implemented like this:

public void renderImage(ImageRenderInfo renderInfo) {
    try {
        String filename;
        FileOutputStream os;
        PdfImageObject image = renderInfo.getImage();
        if (image == null) return;
        filename = String.format(path, renderInfo.getRef().getNumber(), image.getFileType());
        os = new FileOutputStream(filename);
        os.write(image.getImageAsBytes());
        os.flush();
        os.close();
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
}

它使用 ImageRenderInfo 获取 PdfImageObject 实例的对象,它使用该对象创建一个图像文件。

It uses the ImageRenderInfo object to obtain a PdfImageObject instance and it creates an image file using that object.

如果您检查 ImageRenderInfo 类,你会发现你也可以询问有关图像的其他信息。你需要的是 getImageCTM() 方法。此方法返回 Matrix 对象。这个矩阵可以用普通的高中代数来解释。值 I31 I32 为您提供X和Y位置。在大多数情况下, I11 I22 将为您提供宽度和高度(除非图像被旋转)。

If you inspect the ImageRenderInfo class, you'll discover that you can also ask for other info about the image. What you need, is the getImageCTM() method. This method returns a Matrix object. This matrix can be interpreted using ordinary high-school algebra. The values I31 and I32 give you the X and Y position. In most cases I11 and I22 will give you the width and the height (unless the image is rotated).

如果图像被旋转,你将需要咨询你的高中教科书,更具体地说是讨论分析几何学的教科书。

If the image is rotated, you'll have to consult your high-school schoolbooks, more specifically the ones discussing analytic geometry.

这篇关于以pdf格式获取图像的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆