8

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 -> enter image description here

here is the second one 9436 that original tiff image has (1470 x 2057)

enter image description here

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 :(

1

2 Answers 2

32

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.

3
  • Thanks a great solution :) Nov 6, 2016 at 0:51
  • 1
    Thanks for saving me in 2017. Jun 8, 2017 at 13:09
  • If you use margins, you have to substract them from the page width/height otherwise the image will be cut. You can get them from the document (see the validated answer).
    – Carrm
    Oct 25, 2018 at 14:27
18

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

1
  • but the image whatever it comes ,it is getting compressed.how to scale the image properly ,so it should appear proper
    – abh22ishek
    Nov 17, 2015 at 7:12

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.