Here we are going to try some serious stuff, crating PDF from Java. There is an JAR called IText which is used to created PDF from Java. You can download IText from here. Just place this jar in your class path. There are lots of method and classes using which you can create different kind of PDF, but as a reference example, I created program which depicts most of those aspects including image insert in pdf, table creation, font changing etc. If you got time then go for documentation of IText else class blow will server most of your need. Trust me, people will impress you when you say you know how to create PDF using IText...Just go through the class blow carefully and try some hands on example, but create a folder name "Mohit" in your C drive and place a thumbnail image named logo.PNG. You PDF will be created in same folder.
*************************************************************
package com.xav.poc.util;
import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import com.lowagie.text.BadElementException;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class PDFCreator {
public static void main(String[] args) {
PDFCreator pdfCreater = new PDFCreator();
pdfCreater.createPDF("C://Mohit");
}
public String createPDF(String uploadPath)
{
String fileName = null;
Document document=new Document(PageSize.A4);
Image image = null;
try {
image = Image.getInstance (uploadPath + "//logo.PNG");
fileName = "EmpDataForm.pdf";
PdfWriter.getInstance(document,new FileOutputStream(new
File(uploadPath +"/" + fileName)));
} catch (BadElementException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
document.open();
PdfPTable table=new PdfPTable(new float[]{.90f,1.1f, 1.1f, 1.8f, 2.4f,
1.5f, 1.5f});
table.setWidthPercentage(100.0f);
// Creating Global Font Object
Font smallfont = new Font(Font.TIMES_ROMAN);
smallfont.setStyle(Font.BOLDITALIC);
smallfont.setColor(Color.LIGHT_GRAY);
smallfont.setSize(8.0f);
PdfPCell cell = new PdfPCell (image);
cell.setColspan (6);
cell.setHorizontalAlignment (Element.ALIGN_LEFT);
cell.setBackgroundColor (Color.white);
cell.setPadding (10.0f);
cell.setBorderColor(Color.WHITE);
table.addCell (cell);
Paragraph headingAddress = new Paragraph(new
Chunk("Codinguide\n" + "Noida\n" + "UP, India", smallfont));
cell = new PdfPCell (headingAddress);
cell.setHorizontalAlignment (Element.ALIGN_LEFT);
cell.setVerticalAlignment(Element.ALIGN_BOTTOM);
cell.setBackgroundColor (Color.WHITE);
cell.setPadding (10.0f);
cell.setBorderColor(Color.WHITE);
table.addCell (cell);
//Creating a title cell for table
Font font = new Font(Font.TIMES_ROMAN);
font.setStyle(Font.BOLDITALIC);
font.setColor(Color.white);
font.setSize(20.0f);
Paragraph heading = new Paragraph(new Chunk("Mars Software Solutions", font));
cell = new PdfPCell (heading);
cell.setColspan (7);
cell.setHorizontalAlignment (Element.ALIGN_CENTER);
cell.setBackgroundColor (Color.lightGray);
cell.setPadding (10.0f);
table.addCell (cell);
//Creating a table header cells
cell = new PdfPCell (new Paragraph ("Emp Id"));
cell.setHorizontalAlignment (Element.ALIGN_CENTER);
cell.setBackgroundColor (Color.lightGray);
table.addCell(cell);
cell = new PdfPCell (new Paragraph ("First Name"));
cell.setHorizontalAlignment (Element.ALIGN_CENTER);
cell.setBackgroundColor (Color.lightGray);
table.addCell (cell);
cell = new PdfPCell (new Paragraph ("Last Name"));
cell.setHorizontalAlignment (Element.ALIGN_CENTER);
cell.setBackgroundColor (Color.lightGray);
table.addCell (cell);
cell = new PdfPCell (new Paragraph ("Designation"));
cell.setHorizontalAlignment (Element.ALIGN_CENTER);
cell.setBackgroundColor (Color.lightGray);
table.addCell (cell);
cell = new PdfPCell (new Paragraph ("Email"));
cell.setHorizontalAlignment (Element.ALIGN_CENTER);
cell.setBackgroundColor (Color.lightGray);
table.addCell (cell);
cell = new PdfPCell (new Paragraph ("Contact Number"));
cell.setHorizontalAlignment (Element.ALIGN_CENTER);
cell.setBackgroundColor (Color.lightGray);
table.addCell (cell);
cell = new PdfPCell (new Paragraph ("DOJ"));
cell.setHorizontalAlignment (Element.ALIGN_CENTER);
cell.setBackgroundColor (Color.lightGray);
table.addCell (cell);
PdfPCell cell1 = new PdfPCell(new Paragraph("1"));
cell1.setMinimumHeight(30.0f);
table.addCell(cell1);
table.addCell("Mohit");
table.addCell("Singh");
table.addCell("System Analyst");
table.addCell("mohit.amour@gmail.com");
table.addCell("99999999");
table.addCell("6 June ");
// adding table to document
try {
document.add(table);
} catch (DocumentException e) {
e.printStackTrace();
}
//closing document to generate the PDF
document.close();
return fileName;
}
}
*************************************************************
Thanks,
Mohit Singh
No comments:
Post a Comment