iText Add Insert Image PDF File Example

In this tutorial, we will present an example that explains how to add images to PDF document using iText and Java. We will also discuss how to align images in PDF files, and go through some code examples that explain inserting images to PDF document. We will also see how to add different image formats to PDF file;JPEG, PNG, GIF, TIFF and BMP. Refer to the step by step instructions below to append a picture to a PDF document;

Step -1
We will create a Document Object and a PDFWriter object to begin with.
            Document Insert_Picture_PDF = new Document();
            PdfWriter.getInstance(Insert_Picture_PDF, new FileOutputStream("AddImage.pdf"));
Step-2
The image file we will be adding to the PDF document is shown below. We will provide a JPG example to start with. Copy this image file and place it in the same directory where your class file will reside.
Insert Image to PDF File Using Itext
iText Add Image to PDF File
Step-3
We now get an instance of the Image to be added inside an Image object. This can be achieved using the code example below
            Image To_be_Added = Image.getInstance("InsertImage.jpg");
Step-4
It is also possible to provide options for the file being inserted. You can specify the border type, alignment, border width, border color etc. It is also possible to scale the image to the required dimensions. We will do some of them for our image using the code below
            To_be_Added.setAlignment(Image.RIGHT | Image.TEXTWRAP);
            To_be_Added.setBorder(Image.BOX);
            To_be_Added.setBorderWidth(15);
Step-5
Once this is done, you can use document.add to insert the image into the PDF document. We will add the image file to the PDF and close the Document object using the code below
            Insert_Picture_PDF.add(To_be_Added);
            Insert_Picture_PDF.close();

The complete code example for this tutorial is provided below
import java.io.FileOutputStream;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.*;
import com.itextpdf.text.*;
public class image {
    public static void main(String[] args) {
        try {
            Document Insert_Picture_PDF = new Document();
            PdfWriter.getInstance(Insert_Picture_PDF, new FileOutputStream("AddImage.pdf"));
            Insert_Picture_PDF.open();
            Image To_be_Added = Image.getInstance("InsertImage.jpg");            
            To_be_Added.setAlignment(Image.RIGHT | Image.TEXTWRAP);
            To_be_Added.setBorder(Image.BOX);
            To_be_Added.setBorderWidth(15);            
            Insert_Picture_PDF.add(To_be_Added);
            Insert_Picture_PDF.close();           
        }
        catch (Exception i1) {
            i1.printStackTrace();
        }
    }
}
Just make sure that your document object is opened using "Open" method. Otherwise, at run time you will get the following exception
com.itextpdf.text.DocumentException: The document is not open yet; you can only
add Meta information.

If you open the resulting PDF document, you will find your image file inserted into it. A screen shot of the output PDF document is shown below
Image Inserted in PDF Document
Image file inside the PDF document
Note that the image has some border (as specified in code) and is right aligned. More iText image tutorials to follow..

1 comment:

  1. and how to insert this image Direct from drawable folder or assets?

    ReplyDelete