Jenkov.com
Tutorials Books About
Java Itext

1 Java PDF Generation with IText
2 Getting Started with IText PDF API for Java
3 Java IText: Document
4 Java IText: Chunk
5 Java IText: Phrase
6 Java IText: Paragraph
7 Java IText: Chapter + Section
8 Java IText: Anchor (link)
9 Java IText: List
10 Java IText: Table
11 Java IText: Image
12 Java IText: Superscript + Subscript
13 Java IText: Underline + Strikethrough
14 Java IText: Font
15 Java IText: Modifying Existing PDF Documents




Java IText: Image


You can do a lot with images in IText, including scaling, rotating, masking, absolute positioning, borders, alignment etc. I'll only go through the basics here. Get the "IText in Action" book if you want the full story on images. Here is a list of the topics covered in this text:

  1. Creating an Image
  2. Absolute Positioning
  3. Scaling
  4. Rotating

Creating an Image

The com.itextpdf.text.Image is used to add images to IText PDF documents. You can load images either from file or from a URL, like this:

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

import java.io.FileOutputStream;

public class ImageExample {
  public static void main(String[] args) {
    Document document = new Document();

    try {
        PdfWriter.getInstance(document,
                new FileOutputStream("Image.pdf"));
        document.open();

        Image image1 = Image.getInstance("watermark.png");
        document.add(image1);

        
            String imageUrl = "http://jenkov.com/images/" +
            "20081123-20081123-3E1W7902-small-portrait.jpg";

            Image image2 = Image.getInstance(new URL(imageUrl));
        document.add(image2);

        document.close();
    } catch(Exception e){
      e.printStackTrace();
    }
  }
}

Here is what the generated document looks like:

An IText Image example
An IText Image example

Absolute Positioning

You set the absolute position of an image using the setAbsolutePosition() method. Do so before adding the image to the document. This method takes two parameters: X and Y coordinate of the lower left corner of the image. Also keep in mind, that the origin coordinate system in a PDF document is the lower left corner of the document. Not the uppper left corner, like on a screen.

Here is a code example:

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

import java.io.FileOutputStream;
import java.net.URL;

public class Image2Example {
    public static void main(String[] args) {
        Document document = new Document();

        try {
            PdfWriter.getInstance(document,
                    new FileOutputStream("Image2.pdf"));
            document.open();

            String imageUrl = "http://jenkov.com/images/" +
                    "20081123-20081123-3E1W7902-small-portrait.jpg";

            Image image = Image.getInstance(new URL(imageUrl));
            image.setAbsolutePosition(500f, 650f);
            document.add(image);

            document.close();
        } catch(Exception e){
            e.printStackTrace();
        }
    }
}

Here is what the resulting document looks like:

An IText Image at an absolute position
An IText Image at an absolute position

Scaling

You can scale images using one of these Image methods:

scaleAbsolute()
scaleAbsoluteWidth()
scaleAbsoluteHeight()
scalePercentage()
scaleToFit()

Here is a simple example:

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

import java.io.FileOutputStream;
import java.net.URL;

public class Image3Example {
  public static void main(String[] args) {
    Document document = new Document();

    try {
      PdfWriter.getInstance(document,
            new FileOutputStream("Image3.pdf"));
      document.open();

      String imageUrl = "http://jenkov.com/images/" +
              "20081123-20081123-3E1W7902-small-portrait.jpg";

      Image image = Image.getInstance(new URL(imageUrl));
      image.scaleAbsolute(150f, 150f);
      document.add(image);

      Image image2 = Image.getInstance(new URL(imageUrl));
      image2.scalePercent(300f);
      document.add(image2);

      document.close();
    } catch(Exception e){
       e.printStackTrace();
    }
  }
}

Here is what the resulting document looks like:

Two IText Image's scaled
Two IText Image's scaled

Rotating

You can rotate images in IText PDF documents too, using these methods:

setRotationDegrees()
setRotation()

Here is a simple example:

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

import java.io.FileOutputStream;
import java.net.URL;

public class Image4Example {
  public static void main(String[] args) {
    Document document = new Document();

    try {
      PdfWriter.getInstance(document,
            new FileOutputStream("Image4.pdf"));
      document.open();

      String imageUrl = "http://jenkov.com/images/" +
              "20081123-20081123-3E1W7902-small-portrait.jpg";

      Image image = Image.getInstance(new URL(imageUrl));
      image.setRotationDegrees(45f);
      document.add(image);

      document.close();
    } catch(Exception e){
      e.printStackTrace();
    }
  }
}

Here is what the resulting document looks like:

An IText Image rotated
An IText Image rotated


Connect with me: Newsletter - Get all my free tips!