tumblr counter

Itext Add / Insert Image Into PDF

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

Required Libraries

You need to download

  1. JDK 6
  2. itext-5.1.3.jar in classpath

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:

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

Rotating

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

setRotationDegrees()
setRotationDegrees()

Please see the following example

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

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

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

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

      Image image = Image.getInstance
("http://www.javatips.net/static/images/logo.png");
      document.add
(image);

      image = Image.getInstance
(new URL("http://www.javatips.net/static/images/create-pdf-with-itext-java.jpg"));
     
// set Absolute Position
     
image.setAbsolutePosition(220f, 550f);
     
// set Scaling
      // image.scalePercent(100f);
      // set Rotation
      // image.setRotationDegrees(45f);
     
document.add(image);

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






How To Insert Image In PDF Using IText Adding An Image To A PDF document Using IText

You might also like following tutorials :

Java Tutorial Apache CXF Tutorial Hibernate Tutorial Spring Tutorial Struts Tutorial
3 Comments on "Itext Add / Insert Image Into PDF"
12.01.2012 17:40:29 freespaceinteriors
thanks for the information...
01.03.2012 17:41:26 Ronnysoft
eps is not a recognized imageformat. What to do?
15.07.2013 03:55:40 moon
how do i insert eps files in my pdf documnet?

please tell me~