tumblr counter

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













3 Responses to “Itext Add / Insert Image Into PDF”
  1. freespaceinteriors 12.01.2012 17:40:29
  1. Ronnysoft 01.03.2012 17:41:26
  1. moon 15.07.2013 03:55:40

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