Itext Add / Insert Image Into PDF

Itext Add / Insert Image Into PDF Example describes about how to add / insert an image into pdf documents using Java and iText.

iText is a free and open-source tool for manipulating and creating PDF files in Java.

It had been written by Paulo Soares, Bruno Lowagie and others. It allows developers looking to boost web applications with dynamic PDF content  manipulation.

You can also do a lot of effects on images by using IText, which includes rotating, scaling, masking, setting borders, alignment, absolute positioning etc.

You can see the below example, which is demonstrating How to Insert an Image Into PDF

pom.xml

You need to have following dependencies

<dependency>
	<groupId>com.itextpdf</groupId>
	<artifactId>kernel</artifactId>
	<version>7.0.4</version>
</dependency>
<dependency>
	<groupId>com.itextpdf</groupId>
	<artifactId>io</artifactId>
	<version>7.0.4</version>
</dependency>
<dependency>
	<groupId>com.itextpdf</groupId>
	<artifactId>layout</artifactId>
	<version>7.0.4</version>
</dependency>
<dependency>
	<groupId>com.itextpdf</groupId>
	<artifactId>forms</artifactId>
	<version>7.0.4</version>
</dependency>
<dependency>
	<groupId>com.itextpdf</groupId>
	<artifactId>pdfa</artifactId>
	<version>7.0.4</version>
</dependency>
<dependency>
	<groupId>com.itextpdf</groupId>
	<artifactId>pdftest</artifactId>
	<version>7.0.4</version>
</dependency>
<dependency>
	<groupId>org.slf4j</groupId>
	<artifactId>slf4j-log4j12</artifactId>
	<version>1.7.18</version>
</dependency>

Itext Add / Insert Image Example

For add an image to pdf you need to create a Document Object and get a PDFWriter instance, then add image into document

Itext PDF supported standard image types such as GIF, BMP, PNG, JPEG/JPG, WMF and TIFF

Scaling

You can also possible to scale images by using any of the following Image methods:


image.setFixedPosition
setAutoScaleHeight()
setAutoScaleWidth()

Rotating

You can also possible to rotate images in IText PDF documents too, using any of the following methods:

setRotationAngle()

Please see the following example

import com.itextpdf.io.image.ImageData;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;

public class ItextImageExample {
   
public static void main(String[] args) {

 
try {
     
PdfDocument pdfDoc = new PdfDocument(new PdfWriter("image.pdf"));
      Document document =
new Document(pdfDoc);

      ImageData data = ImageDataFactory.create
("logo.png");
      Image image =
new Image(data);
      document.add
(image);

      data = ImageDataFactory.create
("create-pdf-with-itext-java.jpg");
      image =
new Image(data);
     
// set Absolute Position
     
image.setFixedPosition(220f, 550f);
     
// set Scaling
     
image.setAutoScaleHeight(false);
      image.setAutoScaleWidth
(false);
     
// set Rotation
     
image.setRotationAngle(45f);
      document.add
(image);

      document.close
();
 
} catch (Exception e) {
     
e.printStackTrace();
 
}
    }
}
Output
Itext Add / Insert Image Into PDF










4 Responses to "Itext Add / Insert Image Into PDF"
  1. moon 2011-12-10 08:09:57.0
  1. Ronnysoft 2011-12-11 08:09:57.0
  1. freespaceinteriors 2011-12-12 08:09:57.0
  1. CHUNG SLOCUM 2017-11-02 15:04:03.0

Your email address will not be published. Required fields are marked *