Inserting Image in PDF
Inserting Table in PDF
Inserting List in PDF
Text formatting in PDF
Adding new Pages in PDF
Little Chunk
But before you start this application you must download iTextpdf related jar(s).
Inserting Table in PDF
Inserting List in PDF
Text formatting in PDF
Adding new Pages in PDF
Little Chunk
But before you start this application you must download iTextpdf related jar(s).
package pdf; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; import java.util.Date; import com.itextpdf.text.BaseColor; import com.itextpdf.text.Chunk; import com.itextpdf.text.Document; import com.itextpdf.text.Element; import com.itextpdf.text.Image; import com.itextpdf.text.List; import com.itextpdf.text.ListItem; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; public class PdfGen { public static void main(String[] args) { try { OutputStream file = new FileOutputStream(new File("C:\\custom.pdf")); Document document = new Document(); PdfWriter.getInstance(document, file); //Inserting Image in PDF Image image = Image.getInstance ("src/pdf/custom.png"); image.scaleAbsolute(120f, 60f);//image width,height //Inserting Table in PDF PdfPTable table=new PdfPTable(3); PdfPCell cell = new PdfPCell (new Paragraph ("Ja.com")); cell.setColspan (3); cell.setHorizontalAlignment (Element.ALIGN_CENTER); cell.setPadding (10.0f); cell.setBackgroundColor (new BaseColor (140, 221, 8)); table.addCell(cell); table.addCell("Name"); table.addCell("Address"); table.addCell("Country"); table.addCell("Java"); table.addCell("NC"); table.addCell("United States"); table.setSpacingBefore(30.0f); // Space Before table starts, like margin-top in CSS table.setSpacingAfter(30.0f); // Space After table starts, like margin-Bottom in CSS //Inserting List in PDF List list=new List(true,30); list.add(new ListItem("Java")); list.add(new ListItem("Php")); list.add(new ListItem("Somn...")); //Text formating in PDF Chunk chunk=new Chunk("Welecome To Spring Blog..."); chunk.setUnderline(+1f,-2f);//1st co-ordinate is for line width,2nd is space between Chunk chunk1=new Chunk("Phps.com"); chunk1.setUnderline(+4f,-8f); chunk1.setBackground(new BaseColor (17, 46, 193)); //Now Insert Every Thing Into PDF Document document.open();//PDF document opened........ document.add(image); document.add(Chunk.NEWLINE); document.add(new Paragraph("Dear spring.com")); document.add(new Paragraph("Document Generated On - "+new Date().toString())); document.add(table); document.add(chunk); document.add(chunk1); document.add(Chunk.NEWLINE); document.newPage(); //Opened new page document.add(list); //In the new page we are going to add list document.close(); file.close(); System.out.println("Pdf created successfully.."); } catch (Exception e) { e.printStackTrace(); } } }
I was searching a java library/component which i can use in my java application for creating new pdf file and i came across Aspose.PDF for Java, its not a free library but offers free trial so i registered on the website and tried this component and it works amazing and offers many features like adding new table while creating pdf document etc. You should check it out too.
ReplyDelete