Insert image with com.lowagie.text.Image

Wrapping text around Image

Embedding Image into Text

iText PDF Image Transformation - Translating, Scaling and rotating images

Insert image with com.lowagie.text.Image


com.lowagie.text.Image supported standard image types such as BMP, EPS, GIF, JPEG/JPG, PNG, TIFF and WMF. com.lowagie.text.Image is an abstract class, you must use Image.getInstance() methods to return an instance of a specific image class implementation.

Adding images into PDF with iText is easy. See the following example:

 

 

package com.geek.tutorial.itext.image;

import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.codec.GifImage;
import com.lowagie.text.pdf.RandomAccessFileOrArray;
import com.lowagie.text.pdf.codec.TiffImage;
import com.lowagie.text.pdf.codec.GifImage;
import java.io.FileOutputStream;

public class SimpleImages {
	
	public SimpleImages() throws Exception {
		Document document = new Document();
		PdfWriter.getInstance(document, 
			new FileOutputStream("SimpleImages.pdf"));
		document.open();
		
		// Code 1
		document.add(new Paragraph("Simple Image"));
		com.lowagie.text.Image image = 
			com.lowagie.text.Image.getInstance("mouse.jpg");
		document.add(image);
		
		// Code 2
		document.add(new Paragraph("\n"+"AWT Image"));
		java.awt.Image awtImg = 
			java.awt.Toolkit.getDefaultToolkit()
				.createImage("square.jpg");
		com.lowagie.text.Image image2 = 
			com.lowagie.text.Image.getInstance(awtImg, null);
		document.add(image2);				
		document.newPage();
		
		// Code 3
		document.add(new Paragraph("Multipages tiff file"));
		RandomAccessFileOrArray ra = 
			new RandomAccessFileOrArray("multipage.tif");
		int pages = TiffImage.getNumberOfPages(ra);
		for(int i = 1; i <= pages; i++){
			document.add(TiffImage.getTiffImage(ra, i));
		}		
		document.newPage();
		
		// Code 4
		document.add(new Paragraph("Animated Gifs"));
		GifImage img = new GifImage("bee.gif");
		int frame_count = img.getFrameCount();
		for(int i = 1; i <= frame_count; i++){
			document.add(img.getImage(i));
		}
		document.close();
	}
	
	public static void main(String[] args) {
		try{
			SimpleImages simpleImages = new SimpleImages();
		}catch(Exception e){
			System.out.println(e);
		}
	}

}
		
		

Run the example above and you will see SimpleImages.pdf generated.

Code 1.
com.lowagie.text.Image support most of the standard image types such as BMP, GIF, JPG, PNG, TIFF, WMF and partially supported EPS. You can not construct the Image instance directly, use the getInstance() method as shown in the example instead.

Code 2.
You can also insert java.awt.Image into the PDF.

Code 3.
Sometimes TIFF image contains multiple pages, you can extract all pages this way. Note that the page count start at 1.

Code 4.
Animated GIF can not display its animation in PDF, but at least you can extract every frames of it and display series of static images. Note that the frames count start at 1.

Important: Note that if you are inserting images of different width and height on a same pages, you may sometimes get unexpected result of the images position and inserted order. The first image to be inserted is not always appear on top of the next image. This is because when the image is too large to be insert into current page's remaining space, iText will try to insert next smaller image that fit the space first. However, you can turn off this default feature by adding the following code:

		PdfWriter.getInstance(document, 
			new FileOutputStream("SimpleImages.pdf"));
		writer.setStrictImageSequence(true);
		document.open();		
		

Now the larger image that did not fit into current page's remaining space will always be inserted as coding instructed, but it will insert into next page instead of current page.

Wrapping text around Image


If you want the text appear to be wrapping around the image instead of display all the image in separated paragraph, you can use Image.TEXTWRAP alignment propreties.


Text wrapping around image.

See this code sample

package com.geek.tutorial.itext.image;

import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.Phrase;
import com.lowagie.text.Image;
import java.io.FileOutputStream;

public class TextWrapping {

	public TextWrapping() throws Exception {
	
		Document document = new Document();
		PdfWriter.getInstance(document, 
			new FileOutputStream("TextWrapping.pdf"));
		document.open();

		for(int i = 0; i < 100; i++)
		{
			document.add(new Phrase("AAAA ")); // Code 1
		}
		
		com.lowagie.text.Image image = 
			com.lowagie.text.Image.getInstance("square.jpg");
		image.setAlignment(Image.RIGHT | Image.TEXTWRAP);//Code 2
		document.add(image);
		
		document.add(new Phrase("\n\n")); // Code 3
		for(int i = 0; i < 100; i++)
		{
			document.add(new Phrase("BBBB "));
		}
		
		document.close();
		
	}
	
	
	public static void main(String[] args) {
		try{
			TextWrapping textWrapping = new TextWrapping();
		}catch(Exception e){
			System.out.println(e);
		}
		
	}

}
		
		

Code 2
This code set the Image properties to RIGHT align and TEXTWRAP. Run this code sample and you will see a PDF generated like in the image above. The Image appear to be RIGHT align and on a new line after the Code 1 added phrases, and the Code 3 phrases wrapping around the image from the left.

Embedding Image into Text


Sometimes when you are writting user manual, you may want to embed image in between phrases to make the instruction easier to understand.


Image embedded inside a phrase.

You can achieve this by inserting the image as a com.lowagie.text.Chunk instance as shown in the following sample near Code 1:

package com.geek.tutorial.itext.image;

import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.Phrase;
import com.lowagie.text.Chunk;
import com.lowagie.text.Image;
import java.io.FileOutputStream;

public class EmbedImage {

	public EmbedImage() throws Exception{
		
		Document document = new Document();
		PdfWriter.getInstance(document, 
			new FileOutputStream("EmbedImage.pdf"));
		document.open();
		
		// Code 1
		document.add(new Phrase("Please press "));
		document.add(new Chunk(
				Image.getInstance("save.gif"),
				0, 0)
			);
		document.add(new Phrase(" to save the file."));
		
		document.close();
	}

	public static void main(String[] args) {
		try{
			EmbedImage embedImage = new EmbedImage();
		}catch(Exception e){
			System.out.println(e);			
		}
	}

}
		
		

iText PDF Image Transformation - Translating, Scaling and rotating images


package com.geek.tutorial.itext.image;

import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Image;
import java.io.FileOutputStream;

public class Transformation {

	public Transformation() throws Exception{
		
		Document document = new Document();
		PdfWriter.getInstance(document, 
			new FileOutputStream("transformation.pdf"));
		document.open();
		
		Image img = Image.getInstance("square.jpg");
		img.setAbsolutePosition(100, 650); // Code 1
		
		img.scaleAbsolute(100, 100); // Code 2
		
		img.setRotationDegrees(40); // Code 3
		
		document.add(img);
		document.close();
		
	}
	
	public static void main(String[] args) {
		try{
			Transformation trans = new Transformation();
		}catch(Exception e){
			System.out.println(e);
		}
		
	}

}
		
		

Code 1.
Perform image translation.

Code 1.
Perform image scaling. Note that you can use percentage scaling too, just call img.scalePercent(); instead.

Code 3.
Perform image rotation.

Your donation will be use for this project's site maintainance and further development of the content. Your support can help us provide higher quality of free tutorials and services to everyone in future Support This Project. Download latest source code for this project.

 

This is the project of
SourceForge.net Logo