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);
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);
0 comments:
Post a Comment