Home  >  iText

Add Image in PDF Using iText in Java

By Arvind Rai, February 06, 2015
In this page we will learn adding image in PDF using iText API. iText provides Image class using which we can add image in PDF. Image class provides different setter and getter methods to handle position, size, rotation and scaling of image. In this page, we will discuss handling of Images with PDF. Image object can be created using absolute path as well as image URL. Image is instantiated as below.
Image img = Image.getInstance(); 

Add Simple Image

To add image in PDF using iText, we need to follow below steps.
1. Create Document instance.
2. Instantiate PdfWriter using document.
3. Instantiate Image by passing image path.
4. Add image instance to document.
AddImage.java
package com.concretepage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
public class AddImage {
   public static void main(String[] args) throws DocumentException, MalformedURLException, IOException {
	Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream("sample1.pdf"));
        document.open();
        Image img = Image.getInstance("arvind-rai.png");
        document.add(new Paragraph("Sample 1: This is simple image demo."));
        document.add(img);
        document.close();
        System.out.println("Done");
   }
} 
Find the output.
Add Image in PDF Using iText in Java



Add Image to Absolute Position

To add image in absolute position, iText provides API as Image.setAbsolutePosition
AddImageAbsolutePosition.java
package com.concretepage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfWriter;
public class AddImageAbsolutePosition {
    public static void main(String[] args) throws DocumentException, MalformedURLException, IOException {
	Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream("sample2.pdf"));
        document.open();
        Image img = Image.getInstance("arvind-rai.png");
        img.setAbsolutePosition(450f, 10f);
        document.add(img);
        document.close();
        System.out.println("Done");
    }
} 
Find the output.
Add Image in PDF Using iText in Java

Rotate Image

To rotate Image, iText provides Image.setRotation API.
RotateImage.java
package com.concretepage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfWriter;
public class RotateImage {
   public static void main(String[] args) throws DocumentException, MalformedURLException, IOException {
	Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream("sample3.pdf"));
        document.open();
        Image img = Image.getInstance("arvind-rai.png");
        img.setRotation(270f);
        document.add(img);
        document.close();
        System.out.println("Done");
  }
} 
Find the output.
Add Image in PDF Using iText in Java

Scale Image

To scale image, iText provides API as Image.scaleAbsolute and Image.scaleToFit. scaleAbsolute accepts absolute width and height. scaleToFit fits an image to given width and height.
ScaleImage.java
package com.concretepage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfWriter;
public class ScaleImage {
   public static void main(String[] args) throws DocumentException, MalformedURLException, IOException {
	Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream("sample4.pdf"));
        document.open();
        Image img1 = Image.getInstance("arvind-rai.png");
        img1.scaleAbsolute(50f, 50f);
        document.add(img1);
        Image img2 = Image.getInstance("arvind-rai.png");
        img2.scaleToFit(100f, 100f);
        document.add(img2);
        document.close();
        System.out.println("Done");
   }
} 
Find the output.
Add Image in PDF Using iText in Java

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us