Friday, April 3, 2009

How to insert IMAGE in PDF using iText

On assuming that you should have one jpg image in the same folder. In this example I kept Binod_Flex.jpg in the same folder where this java file present.
Demo.java

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

public class Demo {
/**
* @param args
*/
public static void main(String[] args) {
new Demo().createPDF();
}
public void createPDF(){
Document d = new Document (PageSize.A4);
try {
PdfWriter.getInstance(d, new FileOutputStream("sample.pdf"));
d.open ();
d.addCreator("Binod by Demo.java");
d.addAuthor("Binod Suman");
d.addTitle("First PDF By Binod");
Image image = Image.getInstance("Binod_Flex.jpg");
image.scaleAbsolute(300,300);
d.add(image);
d.close ();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
System.out.println("******** PDF Created ***************");
}
}

You can now check that one PDF generated with specify image.

To change PDF page background color, use this

Rectangle pageSize = new Rectangle(400,400);
pageSize.setBackgroundColor(new java.awt.Color(0xDF,0xCC,0xFF));
Document d = new Document (pageSize);
and remaining same.

Insert table in PDF,


PdfPTable table=new PdfPTable(2);
table.addCell("Student Name");
table.addCell("Roll No.");
table.addCell("Binod");
table.addCell("110");
table.addCell("Pramod");
table.addCell("120");
d.add(table);

Insert Header in table in PDF


PdfPTable table=new PdfPTable(2);
PdfPCell cell = new PdfPCell(new Paragraph("Student Details"));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setBackgroundColor(new Color(20,105,160));
cell.setColspan(2);
table.addCell(cell);
table.addCell("Student Name");
table.addCell("Roll No.");
table.addCell("Binod");
table.addCell("110");
table.addCell("Pramod");
table.addCell("120");
d.add(table);

7 comments:

  1. Hi Sir,

    Really nice! I want to create a table over the imgae and insert the data dynamicall inside the tables. Can you plese help me. I am using the same iText2.1.7.jar file.

    Also I have to change the colour in white whatsoever data we are filling in the table.

    ReplyDelete
  2. hii
    i created two tables say t1 and t2.Set headers for both say h1 and h2.
    i want when table2 starts.it should print both h1 and h2.now it is printing h2 only.how to do it?

    ReplyDelete
  3. Hi.
    It's nice.I want to insert an image in an existing pdf file.how can i do that

    ReplyDelete
  4. hi
    Please tell me how to add image and text in same pdf cell?...
    please
    thanks

    ReplyDelete
  5. Hi Binod,
    How to read the image from web content folder

    Thanks.

    ReplyDelete
  6. Sir Please tell me how to add header Image+company title and Footer page no
    header must me fixed on each page only body content changable.
    Sir can u help me to this problem.

    Your's Obey sachin

    ReplyDelete

You can put your comments here (Either feedback or your Question related to blog)