How do I rotate image in iText PDF document?

You can rotate images in the iText pdf document using the setRotation(final float r) or the setRotationDegrees(final float deg) methods of the com.itextpdf.text.Image class. This method sets the rotation in radian and in degree respectively. Let’s see an example below:

package org.kodejava.itextpdf;

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

import java.io.FileOutputStream;
import java.io.IOException;

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

            // Rotate image in radian using the setRotation method.
            String filename = "kodejava-itextpdf/src/main/resources/java.png";
            Image image = Image.getInstance(filename);
            image.setRotation(90f);
            doc.add(image);

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

            // Rotate image in degree using the setRotationDegree method
            String url = "https://kodejava.org/wp-content/uploads/2017/01/kodejava.png";
            image = Image.getInstance(url);
            image.setRotationDegrees(90);
            doc.add(image);
        } catch (DocumentException | IOException e) {
            e.printStackTrace();
        } finally {
            doc.close();
        }
    }
}

Maven Dependencies

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13.3</version>
</dependency>

Maven Central

How do I add an image into a PDF document in iText?

The following example demonstrates how to add an image into a PDF document using the iText library. An image is created using the com.itextpdf.text.Image class. To create an instance of image, we can use the Image.getInstance() method. In the example below, we create an image from an image file name a URL that points to an image resource.

package org.kodejava.itextpdf;

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

import java.io.FileOutputStream;
import java.io.IOException;

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

            // Creating image by file name
            String filename = "kodejava-itextpdf/src/main/resources/java.png";
            Image image = Image.getInstance(filename);
            doc.add(image);

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

            // Creating image from a URL
            String url = "https://kodejava.org/wp-content/uploads/2017/01/kodejava.png";
            image = Image.getInstance(url);
            doc.add(image);
        } catch (DocumentException | IOException e) {
            e.printStackTrace();
        } finally {
            doc.close();
        }
    }
}

Maven Dependencies

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13.3</version>
</dependency>

Maven Central

How do I set an image as the content of a cell in iText?

This example demonstrate how to add an image into a cell in iText. One way to set image into cell’s content is by creating an instance of com.itextpdf.text.pdf.PdfPCell and pass an image object (com.itextpdf.text.Image) in the constructor parameter. The constructor also accept a boolean value whether the image will be fitted into the cell or not.

package org.kodejava.itextpdf;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Objects;

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

            // Creates a table
            PdfPTable table = new PdfPTable(3);
            PdfPCell cell1 = new PdfPCell(new Phrase("Cell 1"));

            // Creates an image and set it as cell's content
            URL resource = TableImageDemo.class.getResource("/java.png");
            Image image = Image.getInstance(Objects.requireNonNull(resource));
            PdfPCell cell2 = new PdfPCell(image, false);
            cell2.setPadding(10);

            PdfPCell cell3 = new PdfPCell(new Phrase("Cell 3"));

            // Add cells to table
            table.addCell(cell1);
            table.addCell(cell2);
            table.addCell(cell3);

            doc.add(table);
        } catch (DocumentException | IOException e) {
            e.printStackTrace();
        } finally {
            doc.close();
        }
    }
}

Maven Dependencies

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13.3</version>
</dependency>

Maven Central

How do I create nested Table in iText?

This example show you how to create nested table in iText. To create a nested table you can add a com.itextpdf.text.pdf.PdfPTable object into a cell using the com.itextpdf.text.pdf.PdfPCell‘s addElement() method.

package org.kodejava.itextpdf;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

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

            PdfPTable table = new PdfPTable(3);
            PdfPCell cell1 = new PdfPCell(new Phrase("Cell 1"));
            PdfPCell cell2 = new PdfPCell(new Phrase("Cell 2"));
            PdfPCell cell3 = new PdfPCell(new Phrase("Cell 3"));

            PdfPTable nestedTable = new PdfPTable(2);
            nestedTable.addCell(new PdfPCell(new Phrase("Nested 1")));
            nestedTable.addCell(new PdfPCell(new Phrase("Nested 2")));
            cell3.addElement(nestedTable);

            table.addCell(cell1);
            table.addCell(cell2);
            table.addCell(cell3);
            doc.add(table);
        } catch (DocumentException | FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            doc.close();
        }
    }
}

Maven Dependencies

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13.3</version>
</dependency>

Maven Central

How do I rotate Cell contents in iText?

You can set the rotation of the cell contents using the com.itextpdf.text.pdf.PdfPCell‘s setRotation() method. The valid rotation options are 0, 90, 180 and 270.

package org.kodejava.itextpdf;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

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

            PdfPTable table = new PdfPTable(3);
            PdfPCell cell1 = new PdfPCell(new Phrase("Cell 1 - 90"));
            // 90 degree rotation
            cell1.setRotation(90);
            table.addCell(cell1);

            PdfPCell cell2 = new PdfPCell(new Phrase("Cell 2 - 180"));
            // 180 degree rotation
            cell2.setRotation(180);
            table.addCell(cell2);

            PdfPCell cell3 = new PdfPCell(new Phrase("Cell 3 - 270"));
            // 270 degree rotation
            cell3.setRotation(270);
            table.addCell(cell3);
            table.completeRow();

            doc.add(table);
        } catch (DocumentException | FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            doc.close();
        }
    }
}

Maven Dependencies

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13.3</version>
</dependency>

Maven Central