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: scaleAbsolute(), scaleAbsoluteWidth(), scaleAbsoluteHeight(), scalePercent() and scaleToFit().

package org.kodejava.example.itextpdf;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfWriter;


public class ImageScalingDemo {
    public static void main(String[] args) {
        Document doc = new Document();
        try {
            PdfWriter.getInstance(doc, new FileOutputStream("ImageScaling.pdf"));

            // Scale the image to an absolute width and an absolute
            // height
            String filename = "other-sample/src/main/resources/java.gif";
            Image image = Image.getInstance(filename);
            image.scaleAbsolute(200f, 200f);

            // The following line to prevent the "Server returned
            // HTTP response code: 403" error.
            System.setProperty("http.agent", "Chrome");

            // Scale the image to a certain percentage
            String url = "";
            image = Image.getInstance(url);

            // Scales the image so that it fits a certain width and
            // height
            image = Image.getInstance(url);
            image.scaleToFit(100f, 200f);
        } catch (DocumentException | IOException e) {
        } finally {

Maven Dependencies

<!-- -->

Wayan Saryada

Founder at Kode Java Org
I am a programmer, a runner, a recreational diver, currently live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. You can support my works by donating here. Thank you ?


  1. If I need to scale the image to 794 px (width) and 559 px (height), what should I need to use, just like 100f, 200f?


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.