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);
Hi Sir,
ReplyDeleteReally 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.
hii
ReplyDeletei 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?
Hi.
ReplyDeleteIt's nice.I want to insert an image in an existing pdf file.how can i do that
hi
ReplyDeletePlease tell me how to add image and text in same pdf cell?...
please
thanks
Hi Binod,
ReplyDeleteHow to read the image from web content folder
Thanks.
Thanks for the article
ReplyDeleteItext Add / Insert Image Into PDF
Sir Please tell me how to add header Image+company title and Footer page no
ReplyDeleteheader must me fixed on each page only body content changable.
Sir can u help me to this problem.
Your's Obey sachin