Fitting image into PDF using ITEXT - java

Hi im having some problems to add the image into the PDF using the itextPDF...
i have more than 10.000 images scanned from original paper in past years, and different sizes/pixels
when i add image into pdf using
img.scaleAbsolute(823,640)
or
img.scaleToFit(823,640)
doesnt change the result of each image, for example this ones:
First One 654 is the correct one that fit perfectly in the pdf PAGE, the original tiff image has (2048 x 2929)
here image ->
here is the second one 9436 that original tiff image has (1470 x 2057)
look the MARGIN of the pdf in the first and in the second one...
there is a way to make EVERY image like the 654, no matter the original size??
thanks, aprreciate any idea, or i will have to open all the image in paint and edit it :(

I know it's an old question, but I was looking for it myself and I've found:
Image image = ...;
image.scaleToFit(PageSize.A4.getWidth(), PageSize.A4.getHeight());
found it useful enough to share here.

Solved for me:
//if you would have a chapter indentation
int indentation = 0;
//whatever
Image image = coolPic;
float scaler = ((document.getPageSize().getWidth() - document.leftMargin()
- document.rightMargin() - indentation) / image.getWidth()) * 100;
image.scalePercent(scaler);
from #Franz Ebner
iText Image Resize

Related

Reading pixel aspect ratio of TIF image in Java

I am reading in and processing TIF images using ImageIO and JAI. The results are all working perfectly, except that a number of the TIF images do not have square pixels. The aspect ratio of the pixels is being lost during the processing so the resulting image looks stretched.
I found this question which reads out the resolution in C#: Change tiff pixel aspect ratio to square but I cannot find any equivalent in java.
Does anyone know how either to read the horizontal and vertical resolution (not size) of a BufferedImage and/or TIF Image in Java or cause JAI to scale the image as it loads it so that the resulting pixels are square?
After an hour of Googling and trying things I think I have found a solution.
IIOMetadata iiom = ir.getImageMetadata(i);
TIFFDirectory dir = TIFFDirectory.createFromMetadata(iiom);
TIFFField fieldXRes = dir.getTIFFField(BaselineTIFFTagSet.TAG_X_RESOLUTION);
TIFFField fieldYRes = dir.getTIFFField(BaselineTIFFTagSet.TAG_Y_RESOLUTION);
int xRes = fieldXRes.getAsInt(0);
int yRes = fieldYRes.getAsInt(0);
As an alternative, you can also get the same values from the Standard Metadata, if you don't want to rely on the JAI API (or TIFF format specifics at all).
The Dimension element has the child elements HorizontalPixelSize and VerticalPixelSize which should be equivalent to the values you got from the TIFF tags above, as well as a PixelAspectRatio you could use directly.

crop a character from an image using java

How can crop and display a character in an image without using mouse?
The image contain only a one charater and nothing else.
Example a scanned copy of a paper, which contain a character drawn on it.
This will require some image processing, and there is a lot of libraries available for this task. General processing sequence would be:
convert image to b&w image
calculate integral image over it
using integral image determine glyph boundaries
( if nothing of the above make sense to you, read some image processing books first )
You could get Slick2D library and then use the SpriteSheet class to crop it. It works like this:
SpriteSheet s = new SpriteSheet("imagelocation");
Image cropped = s.getSubImage(x,y,width,length);
Worked for me :D

TIFF wrong height

OK, I agree this is a strange question, but bear with me.
I have a TIFF image and I am using Windows. When I right-click and go to Properties, I see Width = 1728 pixels and Height = 1146 pixels. When I read it with Java as a BufferedImage and call getWidth() and getHeight(), I see the same things. When I open it with MS Paint, I see the same size.
But the problem is that this size is not correct because the height is too small and the image looks idiotic. Now the interesting part: when I open it with IrfanView, I see it OK, with Height = 2292 pixels. When I open it with Windows Photo Viewer, it looks OK with Height = 2292 pixels.
So my question is: How did IrfanView and Windows Photo Viewer manage to recognize the correct height, although it was specified wrong in the metadata of the image? And how to do the same in Java? I don't want to show an idiotic image to the user.
P.S The image comes from outside and I can't do anything about the wrong metadata...
I resolved the problem. In fact the image was inappropriate from the beginning. The reason why IrfanView shows it OK, is that it compares the Horizontal and Vertical DPI and if they are not equal, it resizes the image to make them equal.
For example:
HorizontalDPI = 200
VerticalDPI = 100
Width = 800
Height = 600
When IrfanView opens the image it makes the following:
Height = Height * (HorizontalDPI / VerticalDPI) = 600 * 2 = 1200.
I ended up doing the same in my software. Everything is working fine:)
I hope this post will be useful to other people:)

Browsing PDF page content. Problem with color space and DPI

I am writing a program that would validate PDF file. I am using iText java library to get content of a file, but I have some problems with parsing it. I need to get info about color space and DPI of each image. How can I get info about position and dimensions of image in PDF? I tried to browse each XObject of PDF but I stuck, I cannot find any information about width and height of file in PDF.
Are there any other libraries which can help me?
Thank You for all answers and tips.
The image object in the PDF file stores only the Width and the Height of the image in pixels. In order to know the position and size of the image on the page, in PDF points, you have to execute the page content stream, to create a virtual rendering. The image is painted on the page using the 'Do' operator and its position and size are given by the current transformation matrix that is in place when the 'Do' operator is executed.
The DPI for a specific drawing instance is computed as 72*imageSizeInPixels/imageSizeInPoints, imageSizeInPoints being computed as described above.

Java image scaling

I am outputting images to a PDF file using iText. The images always appear larger than they are supposed to. According to the book (iText in Action), this is because iText always displays the images at a resolution of 72 dpi, regardless of what the actual dpi property of the image is. The book suggests using image.getDpiX() to find the dpi of the image, and then using image.scalePercent(72 / actualDpi * 100) to display the image properly. So far, the getDpiX() property of all my images have returned 0 (I've tried 2 gifs and 1 jpg). Is there another way to figure out the actual DPI so that my images scale properly?
com.lowagie.text.Image graphic = com.lowagie.text.Image.getInstance(imgPath);
float actualDpi = graphic.getDpiX();
if (actualDpi > 0)
//Never gets here
graphic.scalePercent(72f / actualDpi * 100);
According to the com.lowagie.text.Image JavaDoc, the method getDpiX gets the dots-per-inch in the X direction. Returns zero if not available.
You're going to have to assume a value when the getDpiX method returns zero. 100 dpi is as good an assumption as any.
if (actualDpi <= 0) actualDpi = 100f;
graphic.scalePercent(72f / actualDpi * 100f);
For GIF, there is no place to store a "DPI" information in the file, so "actualDpi" has no meaning in that case. For JPEG, the "DPI" information can be stored in the file, but is not mandatory, and if not set: "actualDPI" has no meaning as well.
The real answer is: there is no such thing as "actual DPI", either the information is provided (ie. "in this image I want 1 pixel to be rendered with this specific physical width (or height)"), or it's not. Another element is in your sentence: "always appear larger than they are supposed to"; the "supposed to" is the DPI information stored in the image. So if this information is absent, and you feel that when you open the image it seems right on the screen, then you have to calculate the density of your screen (width in number of pixels divided by the width in inches of your screen), and use that as your "actualDPI" variable.
The screen-resolution, retrieved by
java.awt.Toolkit.getDefaultToolkit().getScreenResolution()
did not help, image was still too big. However, one can scale the image to the page width in the following way:
BufferedImage bufferedImage = null; // your image
Image image = Image.getInstance(bufferedImage,null);
image.scalePercent((document.right()-document.left())/((float)image.getWidth())*100);

Categories

Resources