iText Image Resize

itext image position
itext dpi
itext pdf to image
image getinstance itext relative path
java resize pdf
itext7 add image to page
itextsharp image position
itextsharp get image position

I have a watermark that I would like to put into my pdf. The watermark is a .bmp image, and is 2290 x 3026. I am having a lot of trouble trying to resize this picture to fit the page, does anyone have any suggestions?

Document document = new Document(); 
PdfWriter.getInstance(document, new FileOutputStream("result.pdf"));; 
document.add(new Paragraph("hello")); 
PdfReader reader = new PdfReader("result.pdf"); 
int number_of_pages = reader.getNumberOfPages(); 
PdfStamper pdfStamper = new PdfStamper(reader, new FileOutputStream("result_watermark.pdf")); 
// Get the PdfContentByte type by pdfStamper. 
Image watermark_image = Image.getInstance("abstract(0307).bmp"); 
int i = 0; 
watermark_image.setAbsolutePosition(0, 0);
watermark_image.scaleToFit(826, 1100);
PdfContentByte add_watermark; 
while (i < number_of_pages) { 
    add_watermark = pdfStamper.getUnderContent(i); 

Here is the output for the getScaled() methods.

826.0 - Width
1091.4742 - Height

I would share the picture of the pdf with you guys but unfortunately I can't.

Should I try using a .jpg instead? I don't really know how well iText handles different image extensions.

You could use another approach : resize the image "manually" (i.e. through an image processing software) instead of programmatically through iText.

Since the final dimension seems hardcoded, you could use an already resized image and save yourself some processing time every time you watermark PDF documents.

How to resize an Image to fit it into a PdfPCell?, By adding the boolean parameter true , you ask iText to scale the image. table. AddCell(img[2]); This will also scale the image, but use the properties of the default cell. We reset all the entries in the image dictionary and we add all the keys that are necessary for a PDF viewer to interpret the image bytes correctly. After changing the PRStream object in the reader, we use PdfStamper to write the altered file to a FileOutputStream. Again we get a look at the way iText works internally.

I do it like that:

//if you would have a chapter indentation
int indentation = 0;
Image image = coolPic;

float scaler = ((document.getPageSize().getWidth() - document.leftMargin()
               - document.rightMargin() - indentation) / image.getWidth()) * 100;


How do I scale an image in PDF document using iText?, There some methods of the com.itextpdf.text.Image class that can be use to scale the image. These methods include the following:  // we wrap the image in a PdfPCell and let iText scale it cell = new PdfPCell(img[1], true); table.AddCell(cell); By adding the boolean parameter true, you ask iText to scale the image. Another option is to use addCell() like this: // we add the image with addCell() table.AddCell(img[2]);


watermark_image.scaleAbsolute(826, 1100);

instead of

watermark_image.scaleToFit(826, 1100);

iText - Scaling an Image, iText - Scaling an Image. Step 1: Creating a PdfWriter object. The PdfWriter class represents the DocWriter for a PDF. Step 2: Creating a PdfDocument object. Step 3: Creating the Document object. Step 4: Creating an Image object. Step 5: Scaling an image. Step 6: Adding image to the document. Step 7: Closing the Example. The following Java program demonstrates how to scale an image with respective to the document size on a PDF document using the iText library. It creates a PDF document with the name autoScale.pdf, adds an image to it, scales it with respect to the page dimensions, saves it in the path C:/itextExamples/.

Just in case if the image height exceeds the document height:

float documentWidth = document.getPageSize().width() - document.leftMargin() - document.rightMargin();
float documentHeight = document.getPageSize().height() - document.topMargin() - document.bottomMargin();
image.scaleToFit(documentWidth, documentHeight);

Java Code Examples com.itextpdf.text.Image.scaleToFit, This page provides Java code examples for com.itextpdf.text.Image.scaleToFit. scaleToFit(500, 500); // scale size document.add(image); document.add(new  The Document object in iText and iTextSharp is an abstraction that takes care of various spacings, paddings and margins for you automatically. Unfortunately for you, this also means that when you call doc.Add() it takes into account existing margins of the document.

you can use

imageInstance.scaleAbsolute(requiredWidth, requiredHeight);

com.itextpdf.text.Image.scaleToFit java code examples, Best Java code snippets using com.itextpdf.text.Image.scaleToFit (Showing top 13 results out of 315) Scale the image to the dimensions of the rectangle. I am using Itextsharp to put some images into PDF. Some of these images are large (3600h by 2500w) and some bit small (1000h by 700w). My PDF page is 842H by 737H. Some of these large images will not fit into potrait so i am flipping images where the width > height. img.RotationDegrees = 90f;

java.awt.Image.scalePercent java code examples, iText Image Resize. //if you would have a chapter indentation int indentation = 0; //whatever Image image = coolPic; float scaler = ((document.getPageSize(). 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)

Java IText: Image, Absolute Positioning; Scaling; Rotating. Creating an Image. The com.itextpdf.text.​Image is used to add images to IText PDF documents  Thumbnailator is an open-source image resizing library for Java with a fluent interface, distributed under the MIT license. I wrote this library because making high-quality thumbnails in Java can be surprisingly difficult, and the resulting code could be pretty messy.

java - iText Image Resize, Se podría utilizar otro enfoque: cambiar el tamaño de la imagen "manualmente" (​es decir, a través de un procesamiento de imágenes software) en lugar de  The following code takes an image of 800 x 600 and forces it to resize, maintaining its aspect ratio, into a rectangle measuring 250 points square. Image jpg = Image .GetInstance(imagepath + "/Sunset.jpg" );

  • you can add screenshot of PDF. See… or…
  • That's not the problem. The watermark I am adding is a company watermark, and I can't just be passing it around.. Well I was told not to anyway.
  • have you tried scaling the image manually, instead of programmatically, and use the manually scaled image in your code? since you seem to hardcode the scaled dimension, that would save you some processing every time you watermark PDF documents.
  • @AlexisPigeon - No I haven't, but I'll try it and get back to you..
  • @AlexisPigeon - The manual resize worked, it's a little blurry, but I can deal. Add your comment as an answer and I'll give you a check mark.
  • Unfortunately that doesn't work for me. The closest I have ever gotten was to do: watermark_image.scalePercent(110 * 72 / 300);
  • can you describe exactly what's your problem!? the scaling doesn't work- ok but what's your output with the scaler you use at the time!?
  • This approach worked for me better than the idea proposed by Alexis because the default scaling has a pretty low resolution, so if you resize the original image to fit the pdf document, you get a pixelated low resolution image at maybe 72 dpi equivalent. But with scaling, you can use a much larger image resized to fit but preserving the high resolution.
  • Worked like a charm. Thanks.
  • Works perfect for me! Thanks a lot!
  • What is the difference between these two methods?