Finik
Finik

Reputation: 35

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).

Using the answer and comments, I got width and height (either through Matrix, or through BufferedImage). The idea was to use the answer here to calculate the DPI, but I am a bit lost.

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.

Upvotes: 3

Views: 3254

Answers (2)

PeteH32
PeteH32

Reputation: 871

I think original answer (answered Aug 28, 2014) was for iText 5. A newer answer for iText 7 is here:

Info from above link is pasted below:

Now you don’t need to calculate the DPI values yourself. There is an ImageData class in iText 7 with built-in methods to solve your problem:

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

Upvotes: 1

Bruno Lowagie
Bruno Lowagie

Reputation: 77528

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.

  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.
  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.

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.

Upvotes: 2

Related Questions