Image Quality using Itext
Asked Answered
D

2

20

Is possible to generate a PDF document using Itext which contains imgages with a resolution of 150 dpi.

(Seems to be that Itext is reducing the quality of images to 72 dpi.)

Thanks

Declan answered 23/11, 2011 at 8:53 Comment(0)
D
53

See this thread, specifically this post which explains that PDFs don't have a concept of DPI. This thread is a good read, too.

Essentially (and this isn't technically accurate but close enough) by default, when you place an image using iText assume for every 72 pixels that 1 inch of printed space will be used. If you want to change this rule then you need to change the image's matrix. You can either do this directly (which is hard) or use the scaling methods (much easier) such as scaleAbsolute(), scaleAbsoluteWidth(), scaleAbsoluteHeight(), scalePercentage() and scaleToFit().

Changing the matrix does not change the image in any way, it only (essentially) changes the definition of how many pixels to cram into an inch, which when printed you would call DPI. (You're actually changing the relationship between image pixels and the document's user units but you don't really need to know that.) So by default, a 200x200 pixel image placed by iText should print out at about 2 3/4". If you call scaleAbsolute(100,100) it will cram those 200 pixels into 100 "user units" giving you an effective DPI of 144 and print at about 1 3/8".

All of this can really make your head hurt, especially if you're used to imaging programs such as Adobe Photoshop that allow you to set a "resolution" for some image types. Unfortunately this is more of a "printing hint" and doesn't really change the file that much. A 100x100 pixel image at 72DPI is the exact same as 100x100 pixel at 300DPI, except when printed. iText completely ignores this printing hint, either because that's all it is or because not all image formats support it. (Not directly related, but one common myth is that "Save for Web" makes images 72DPI when reality it completely strips the resolution information from the file.)

Ignoring what's actually happening, to get the image to be what you think of as 150 DPI, take the source image's width and height in pixels and multiple those by 0.48 (72 divided by 150) and pass those new numbers to scaleAbsolute().

There is a method on the Image class called setDPI but as far as I can tell it doesn't actually have any effect on the placement of the image and is more of an informational thing.

Drennan answered 23/11, 2011 at 16:13 Comment(4)
Thanks a lot for your comment. Now I got images with superb quality at my PDF. I wonder why I didn't find the thread you mentioned when I googled it.Declan
Thanks @Chris ,how can i get good quality at pdf? Luixv can you tell me how can i get superb images in my pdf?Sard
Just make a quality image and add it. iText will not alter your image. If this doesn't help, post a new question that explains your problem, what you've tried, what didn't work, etc.Drennan
Great answer!!.Apparatus
T
0

If the image quality is poor in PDF. you can do following:

  1. Create larger size image with high resolution.
  2. Scale down the image while exporting in PDF file.

This solved my problem.

Theaterintheround answered 12/7, 2012 at 10:24 Comment(1)
This solution may be viable, but is too ambiguous--can you provide a small code fragment? (one "scaling" function may keep the original image's resolution while another one resamples and changes the resolution).Monogamist

© 2022 - 2024 — McMap. All rights reserved.