简体   繁体   English

使用 iText 获取 PDF 文件中的图像 DPI

[英]Getting Image DPI in PDF files using iText

I 'm trying to get information about scanned images that are saved into PDF files through iText (using Java).我正在尝试获取有关通过 iText(使用 Java)保存到 PDF 文件中的扫描图像的信息。

Using the answer and comments , I got width and height (either through Matrix, or through BufferedImage).使用答案和评论,我得到了宽度和高度(通过 Matrix 或通过 BufferedImage)。 The idea was to use the answer here to calculate the DPI, but I am a bit lost.这个想法是用这里的答案来计算DPI,但我有点迷茫。

Are these values (width and height) in pixels or points?这些值(宽度和高度)是像素还是点? Is there any other way to achieve this?有没有其他方法可以实现这一目标? There are a lot of answers on how to scale and save an image to a PDF file, but I didn't find any on how to read the width/height/scale of an image and be confident about the result.关于如何缩放图像并将其保存到 PDF 文件有很多答案,但我没有找到任何关于如何读取图像的宽度/高度/比例并对结果充满信心的答案。

Let's split this problem into two separate problems.让我们把这个问题分成两个独立的问题。 To calculate the DPI, you need two sets of values: a number of pixels and a distance in inch.要计算 DPI,您需要两组值:像素数和以英寸为单位的距离。

  1. Number of pixels: you obtain the image and the image consists of pixels.像素数:您获得图像并且图像由像素组成。 You can retrieve the width and height of the image in pixels from the image.您可以从图像中检索图像的宽度和高度(以像素为单位)。 Let's say these values are wPx and wPx .假设这些值是wPxwPx
  2. Distance in inch: you obtain the matrix which gives you values expressed in points.以英寸为单位的距离:您将获得一个矩阵,该矩阵为您提供以点表示的值。 As 72 points equal 1 inch, you need to divide these values by 72. Let's say these values are wInch and hInch .由于 72 点等于 1 英寸,因此您需要将这些值除以 72。假设这些值是wInchhInch

Now you can calculate the DPI in the x direction like this: wPx / wInch and the DPI in the y direction like this: hPx / hInch .现在您可以像这样计算 x 方向的 DPI: wPx / wInch和 y 方向的 DPI,如下所示: hPx / hInch

I think original answer (answered Aug 28, 2014) was for iText 5. A newer answer for iText 7 is here:我认为原始答案(2014 年 8 月 28 日回答)适用于 iText 5。 iText 7 的更新答案在这里:

Info from above link is pasted below:上面链接的信息粘贴在下面:

Now you don't need to calculate the DPI values yourself.现在您不需要自己计算 DPI 值。 There is an ImageData class in iText 7 with built-in methods to solve your problem: iText 7 中有一个 ImageData class 内置方法来解决您的问题:

ImageData image = ImageDataFactory.create(IMG);
int x = image.getDpiX();
int y = image.getDpiY();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM