skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Thursday, September 20, 2012

To Generate the PDF Report

Posted by Admin at 12:28 PM – 0 comments
 

This code snippet will guide you to how to Generate the PDF report


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Set;
import java.util.ArrayList;
import java.util.Map;
import java.util.Iterator;


import com.lowagie.text.BadElementException;
import com.lowagie.text.Cell;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.Section;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfEncryptor;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.codec.GifImage;
import com.lowagie.text.pdf.RandomAccessFileOrArray;
import com.lowagie.text.pdf.codec.TiffImage;
import com.lowagie.text.pdf.codec.GifImage;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import java.awt.Color;
import java.awt.Graphics;

import com.lowagie.text.Image;


public class Health {
    
 HashMap resultSet=null;
 ServerHealthCheck serverHealthStatus = new ServerHealthCheck();
    public static String dateTime="";
 boolean emailsend=false;
 Date date=new Date();
 private boolean add;
    String filename;
 public static String fileNamev;
    
  Rectangle pageSize = new Rectangle(400,400);
  Document document = new Document (pageSize);
    
 public Health() throws IOException 
 {

  try {
   SimpleDateFormat sdf = new SimpleDateFormat("dd_MMM_yy_HH_mm_ss");

   //System.out.print(sdf);

   dateTime = sdf.format(date).toString();
  // System.out.print(dateTime);
   filename = dateTime + ".pdf";
  // System.out.print(filename);
   PdfWriter writer = PdfWriter.getInstance(document,
     new FileOutputStream("MorningHealthcheck/SUPPORT_ATT/"
       + filename));
  } catch (FileNotFoundException e) {
   System.out.print("Filenot found");
  } catch (DocumentException e) {
   System.out.print("Document not found");
  } catch (Exception e) {
   e.printStackTrace();
  }
  //System.out.println("Started creating PDF File " + filename);
  try {

   document.open();
   document.addTitle("MDIS MorningHealthCheck");
   document.addSubject("PDF Generated by the Automated Health Check");
   document.addKeywords("MorningHealthCheck MDIS");
   document.addCreator("MDISPSS");
   document.addAuthor("Rajashekar D");
   Image image = Image.getInstance("Images/MDIS.jpg");
   image.setAlignment(Image.LEFT);
   image.scaleAbsoluteHeight(20);
   image.scaleAbsoluteWidth(2);
   image.scalePercent(60, 60);
   document.add(image);
  } catch (Exception e1) {
   System.out.print("EXception in Adding the Image");
  }
  try {
   resultSet = serverHealthStatus.connect();
   Set set = resultSet.entrySet();
   Iterator iterator = set.iterator();
   while (iterator.hasNext()) {

    Map.Entry map = (Map.Entry) iterator.next();
    String key = (String) map.getKey();
    document.add(new Paragraph("\n"));
    document.add(new Paragraph("THE BELOW ARE IN " + key,
      FontFactory.getFont(FontFactory.COURIER, 12, Font.BOLD,
        Color.GREEN)));
    HashMap val = (HashMap) map.getValue();
    Set set1 = val.entrySet();
    Iterator iterator1 = set1.iterator();

    while (iterator1.hasNext()) {
     Map.Entry map1 = (Map.Entry) iterator1.next();
     String Key = (String) map1.getKey();
     document.add(new Paragraph("\n"));
     document.add(new Paragraph(Key, FontFactory.getFont(
       FontFactory.COURIER, 10, Font.BOLD, new Color(0, 0,
         255))));
     document.add(new Paragraph("\n"));
     ArrayList value = (ArrayList) map1.getValue();

     Iterator ite = value.iterator();

     while (ite.hasNext()) {
      String line = (String) ite.next();
      Paragraph p = new Paragraph(line, FontFactory.getFont(
        FontFactory.COURIER, 7, Font.NORMAL,
        Color.WHITE));
      PdfPTable table = new PdfPTable(1);
      PdfPCell cell = new PdfPCell(p);
      cell.setHorizontalAlignment(200);
      cell.setBackgroundColor(Color.darkGray);
      cell.setBorder(1);
      cell.setBorderColor(Color.white);
      cell.setColspan(2);
      table.addCell(cell);
      document.add(table);
     }
    }
   }

   emailsend = true;
  } catch (Exception e) {
   System.out.print("null value");
   emailsend = false;
  }finally{
   if(document!=null)
   document.close(); 
  }
  

  try {
   PdfEncryptor.encrypt(new PdfReader(
     "MorningHealthcheck/SUPPORT_ATT/" + filename),
     new FileOutputStream("MorningHealthcheck/SUPPORT_ATT/2"
       + filename), true, "mdis", "",
     PdfWriter.ALLOW_PRINTING | PdfWriter.ALLOW_COPY);

   File tempFile = new File("MorningHealthcheck/SUPPORT_ATT/" + filename);
   tempFile.delete();
   File tempFile2 = new File("MorningHealthcheck/SUPPORT_ATT/2"+ filename);
   tempFile2.renameTo(tempFile);

  // System.out.println("File " + filename+ " has been protected with the password");
   fileNamev = filename;
   emailsend = true;

  } catch (DocumentException e) {
   //System.out.print("Document not found");
   emailsend = false;
  }

  if (emailsend = true) {
   EmailSend send = new EmailSend();
  } else {
   System.out.print("There is an problem and Exceptions in the Document Preparing");
  }
      System.out.print("You are here exit from the sentrion mail handler");
 }      

 public static void main(String args []) throws IOException
 {
  Health a=new Health();
  System.out.print("Your out of the box");
  //System.exit(0);
 } 
}



Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger