Choose Your Language

Sunday 14 April 2013

Generate PDF using Servlets

GeneratePDF.java

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.*;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class GeneratePDF extends HttpServlet {
    Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18,Font.BOLD);
    Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12,Font.BOLD);    
    Font redFont = new Font(Font.FontFamily.TIMES_ROMAN, 12,Font.NORMAL, BaseColor.RED);
    Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 16,Font.BOLD);
protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String file="E:\\AravindSankaran.pdf";
       try {
            Document document = new Document();
            PdfWriter.getInstance(document,new FileOutputStream(file) );

            document.open();
                        addMetaData(document);
                        addTitlePage(document);
                        addContent(document);
                        document.getPageNumber();
            document.close();
          
        } catch (Exception e) {

            e.printStackTrace();
        }

    }
public void addMetaData(Document document){
    document.addTitle("My Profile");
    document.addSubject("using i text");
    document.addKeywords("java j2ee soa bpel");
    document.addAuthor("aravind");
    document.addCreator("aravind sankarran");
}
public void addTitlePage(Document document){
    
    Paragraph preface=new Paragraph();
    addEmptyLine(preface, 1);
   
    preface.add(new Paragraph("Title of the document",catFont));
    addEmptyLine(preface, 1);
    preface.add(new Paragraph("Report generated by"+System.getProperty("user.name")+""+new   Date(),smallBold));
   

    addEmptyLine(preface, 3);
    preface.add(new Paragraph("This document describes something which is very important ",
        smallBold));
   
    addEmptyLine(preface, 8);

    preface.add(new Paragraph("This document is a preliminary version and not subject to your license agreement or any other agreement with vogella.com ;-).",
        redFont));
        try {
            document.add(preface);
            document.getPageNumber();
            document.newPage();
        } catch (DocumentException ex) {
            Logger.getLogger(GeneratePDF.class.getName()).log(Level.SEVERE, null, ex);
        }
}
public  void addContent(Document document) throws DocumentException{
    Anchor anchor = new Anchor("First Chapter", catFont);
    anchor.setName("First Chapter");
    Chapter catPart = new Chapter(new Paragraph(anchor), 1);

    Paragraph subPara = new Paragraph("Subcategory 1", subFont);
    Section subCatPart = catPart.addSection(subPara);
    subCatPart.add(new Paragraph("Hello"));

    subPara = new Paragraph("Subcategory 2", subFont);
    subCatPart = catPart.addSection(subPara);
    subCatPart.add(new Paragraph("Paragraph 1"));
    subCatPart.add(new Paragraph("Paragraph 2"));
    subCatPart.add(new Paragraph("Paragraph 3"));

    createList(subCatPart);
    Paragraph paragraph = new Paragraph();
    addEmptyLine(paragraph, 5);
    subCatPart.add(paragraph);

    createTable(subCatPart);

    document.add(catPart);

    anchor = new Anchor("Second Chapter", catFont);
    anchor.setName("Second Chapter");

    catPart = new Chapter(new Paragraph(anchor), 1);

    subPara = new Paragraph("Subcategory", subFont);
    subCatPart = catPart.addSection(subPara);
    subCatPart.add(new Paragraph("This is a very important message"));

    document.add(catPart);
}
public void createTable(Section subCatPart)
      throws BadElementException {
    PdfPTable table = new PdfPTable(3);

    PdfPCell c1 = new PdfPCell(new Phrase("Table Header 1"));
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(c1);

    c1 = new PdfPCell(new Phrase("Table Header 2"));
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(c1);

    c1 = new PdfPCell(new Phrase("Table Header 3"));
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);

    table.addCell(c1);
    table.setHeaderRows(1);
    table.addCell("1.0");
    table.addCell("1.1");
    table.addCell("1.2");
    table.addCell("2.1");
    table.addCell("2.2");
    table.addCell("2.3");
    subCatPart.add(table);
}
public void createList(Section subCatPart) {
    List list = new List(true, false, 10);
    list.add(new ListItem("First point"));
    list.add(new ListItem("Second point"));
    list.add(new ListItem("Third point"));
    subCatPart.add(list);
  }
public void addEmptyLine(Paragraph paragraph,int number){
    for(int i=0;i<number;i++){
        paragraph.add(new Paragraph(" "));
       
    }
}
}
jar file needed:
itextpdf-5.2.1.jar

No comments:

Post a Comment