skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Saturday, September 15, 2012

Download functionality of Pdf in bench marking module

Posted by Admin at 1:52 AM – 0 comments
 

The pdf created contains module, submodule, maximum score, actual score, weighted score, etc.. Also the module details and sub module details are proerly distinguished .


import java.awt.Color;
import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.struts.actions.DownloadAction.StreamInfo;

import jxl.format.Colour;
import jxl.write.WriteException;


import com.itextpdf.text.Anchor;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.DocListener;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;
import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;

public class BenchMarkingConvertToPdfService extends PdfPageEventHelper {

 private static String FILE = "c:/DownloadPdf.pdf";
 private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18,
   Font.BOLD);
 private static Font redFont = new Font(Font.FontFamily.TIMES_ROMAN, 12,
   Font.NORMAL, BaseColor.RED);
 private static Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 14,
   Font.BOLD);
 private static Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12,
   Font.BOLD);

 private String inputFile;
 private static final String DATE_PATTERN = "dd-MMM-yy HH:mm:ss a";

 private static final String PAGE_NUMBER = "Page %d";

 private static final String ART = "art";

 private static final Font PARENT_MODULE_FONT = new Font(
   Font.FontFamily.TIMES_ROMAN, ErfConstants.CHILD_FONT, Font.NORMAL);
 private static final Font SUB_MODULE_FONT = new Font(
   Font.FontFamily.TIMES_ROMAN, ErfConstants.CHILD_FONT, Font.NORMAL);
 private static final Font DESCRIPTION_FONT = new Font(
   Font.FontFamily.TIMES_ROMAN, ErfConstants.CHILD_FONT);
/* private static final Font DESCRIPTION_FONT1 = new Font(
   Font.FontFamily.TIMES_ROMAN, ErfConstants.CHILD_FONT);*/
 
 private static final Font RELEVANCY_FONT = new Font(
   Font.FontFamily.TIMES_ROMAN, ErfConstants.CHILD_FONT, Font.NORMAL);
 private static final Font COLUMN_HEADING_FONT = new Font(
   Font.FontFamily.TIMES_ROMAN, ErfConstants.CHILD_FONT, Font.NORMAL);
 private static final Font USER_DEF_REQ_FONT = new Font(
   Font.FontFamily.TIMES_ROMAN, ErfConstants.CHILD_FONT, Font.NORMAL,
   new BaseColor(200, 30, 30));

 //private static final String PROJECT_NAME = "Project Name  :  ";
 
 private static final String MODULE     = "Module     :   ";
 
 private static final String SUB_MODULE = "Sub Module   :   ";

 private static final String BREAKLINE = "nn";

 private static final Rectangle RECTANGLE = new Rectangle(36, 54, 559, 788);

 private static BenchMarkingConvertToPdfService benchMarkingconvertToPdfService = null;

 static int pagenumber;

 PdfTemplate total;

 public static synchronized BenchMarkingConvertToPdfService getInstance() {

  pagenumber = 0;

  if (benchMarkingconvertToPdfService == null) {

   benchMarkingconvertToPdfService = new BenchMarkingConvertToPdfService();

  }

  return benchMarkingconvertToPdfService;
 }

 BenchMarkingConvertToPdfService() {

 }

 public void onStartPage(PdfWriter writer, Document document) {
  pagenumber++;
 }
 public byte[] convertToPdfService(List<BenchMarkingPdfBean> benchMarkingPdfBeanList,Map<Long,Integer> reqPerSubModule) 
   throws DocumentException, IOException {
  Document document = new Document(PageSize.A4,
    ErfConstants.DOCUMENT_MARGINLEFT,
    ErfConstants.DOCUMENT_MARGINRIGHT,
    ErfConstants.DOCUMENT_MARGINTOP,
    ErfConstants.DOCUMENT_MARGINBOTTOM);
  ByteOutputStream out = new ByteOutputStream();
  PdfWriter writer = PdfWriter.getInstance(document, out);

  BenchMarkingConvertToPdfService event = BenchMarkingConvertToPdfService.getInstance();
  writer.setBoxSize(ART, RECTANGLE);
  writer.setPageEvent(event);
  

  document.open();  
  
  

  float[] colsWidth = {ErfConstants.FEATURES_COLUMNWIDTH,
    ErfConstants.RELEVANCY_COLUMNWIDTH,
    ErfConstants.EVALDIM_COLUMNWIDTH,
    ErfConstants.WEIGHTAGE_COLUMNWIDTH,
    ErfConstants.MAXSCORE_COLUMNWIDTH, 
    ErfConstants.ACTUALSCORE_COLUMNWIDTH,
    ErfConstants.WEIGHTEDSCORE_COLUMNWIDTH,
    ErfConstants.WEIGHTEDSCORE_COLUMNWIDTH,
    ErfConstants.SCORE0_COLUMNWIDTH,
    ErfConstants.SCORE1_COLUMNWIDTH,
    ErfConstants.SCORE2_COLUMNWIDTH,
    ErfConstants.SCORE3_COLUMNWIDTH};

  PdfPTable table = new PdfPTable(colsWidth);
  table.setWidthPercentage(100f);
  

  float[] cols1Width = {ErfConstants.SCORE0_COLUMNWIDTH,
    ErfConstants.SCORE1_COLUMNWIDTH,
    ErfConstants.SCORE2_COLUMNWIDTH,
    ErfConstants.SCORE3_COLUMNWIDTH};
  
  PdfPTable table1= new PdfPTable(cols1Width);
  table1.setWidthPercentage(30f);
  
  
  PdfPCell c1; 
  PdfPCell c3;
  String submodule="";
 
  PdfPCell cweightedscore = new PdfPCell(new Phrase("weightedscore"));
  cweightedscore.setBackgroundColor(new BaseColor(255, 204, 0));

  Image image = Image
    .getInstance("http://localhost:9080/EcommerceRequirementFramework/images/header.jpg");
  // image.scaleToFit(400F, 6000F);
  image.scalePercent(54, 40);

  document.add(image);
 

  document.add(new Paragraph(BREAKLINE, DESCRIPTION_FONT));

  // document.add(new Paragraph(BREAKLINE,DESCRIPTION_FONT));

  Image checkedImage = Image
    .getInstance("http://localhost:9080/EcommerceRequirementFramework/images/Tick36.jpg");

  checkedImage.scalePercent(20, 20);



 
  for(int i=0;i<benchMarkingPdfBeanList.size();i++){    
  System.out.println("benchMarkingPdfBeanList.get(i).getParentModulespan()"+benchMarkingPdfBeanList.get(i).getParentModulespan());
  
   if( benchMarkingPdfBeanList.get(i).getParentModulespan()!=0)
   { 
    
    document.add(new Paragraph(MODULE + benchMarkingPdfBeanList.get(i).getFunctionalModules(), catFont));
    //document.add(new Phrase("Score Evaluation Criteria",catFont));

   } 
  if(benchMarkingPdfBeanList.get(i).getSubModulespan()!=0)
  {
 
   submodule=benchMarkingPdfBeanList.get(i).getSubModules();
   System.out.println("submodule"+submodule);
   c3 = new PdfPCell(new Phrase("Features"));
   c3.setBackgroundColor(BaseColor.GRAY);
   c3.setHorizontalAlignment(Element.ALIGN_CENTER);

   table.addCell(c3);

   c1 = new PdfPCell(new Phrase("Relevancy"));
   c1.setBackgroundColor(BaseColor.GRAY);
   c1.setHorizontalAlignment(Element.ALIGN_CENTER);
   table.addCell(c1);

   c1 = new PdfPCell(new Phrase("Importance Dimension"));
   c1.setBackgroundColor(BaseColor.GRAY);
   c1.setHorizontalAlignment(Element.ALIGN_CENTER);
   table.addCell(c1);

   c1 = new PdfPCell(new Phrase("Weightage"));
   c1.setBackgroundColor(BaseColor.GRAY);
   c1.setHorizontalAlignment(Element.ALIGN_CENTER);
   table.addCell(c1);

   c1 = new PdfPCell(new Phrase("Maximum Score"));
   c1.setBackgroundColor(BaseColor.GRAY);
   c1.setHorizontalAlignment(Element.ALIGN_CENTER);
   table.addCell(c1);

   c1 = new PdfPCell(new Phrase("Actual Score"));
   c1.setBackgroundColor(BaseColor.GRAY);
   c1.setHorizontalAlignment(Element.ALIGN_CENTER);
   table.addCell(c1);

   c1 = new PdfPCell(new Phrase("Weighted Score"));
   c1.setBackgroundColor(BaseColor.GRAY);
   c1.setHorizontalAlignment(Element.ALIGN_CENTER);
   table.addCell(c1);
   
   c1 = new PdfPCell(new Phrase(""));
   c1.setBackgroundColor(BaseColor.WHITE);
   /*c1.setHorizontalAlignment(Element.ALIGN_CENTER);*/
   c1.setBorder(0);
   table.addCell(c1);
   
   c1 = new PdfPCell(new Phrase("0"));
   c1.setBackgroundColor(BaseColor.GRAY);
   c1.setHorizontalAlignment(Element.ALIGN_CENTER);
   table.addCell(c1);
   
   
   c1 = new PdfPCell(new Phrase("1"));
   c1.setBackgroundColor(BaseColor.GRAY);
   c1.setHorizontalAlignment(Element.ALIGN_CENTER);
   table.addCell(c1);
   
   c1 = new PdfPCell(new Phrase("2"));
   c1.setBackgroundColor(BaseColor.GRAY);
   c1.setHorizontalAlignment(Element.ALIGN_CENTER);
   table.addCell(c1);
   
   c1 = new PdfPCell(new Phrase("3"));
   c1.setBackgroundColor(BaseColor.GRAY);
   c1.setHorizontalAlignment(Element.ALIGN_CENTER);
   table.addCell(c1);
   
   //table.setHeaderRows(1); 
  }
   table.addCell(new Phrase(benchMarkingPdfBeanList.get(i).getFeatures(),DESCRIPTION_FONT));
   table.addCell(new Phrase(benchMarkingPdfBeanList.get(i).getRelevancy(),DESCRIPTION_FONT));
   table.addCell(new Phrase(benchMarkingPdfBeanList.get(i).getImportanceDimension(),DESCRIPTION_FONT));
   if(benchMarkingPdfBeanList.get(i).getWeightage()!=null)
   {
   table.addCell(new Phrase(benchMarkingPdfBeanList.get(i).getWeightage().toString(),DESCRIPTION_FONT));
     
   }
   else
   {
    table.addCell("");            
   }
   if(benchMarkingPdfBeanList.get(i).getMaximumScore()!=null)
   table.addCell(new Phrase(benchMarkingPdfBeanList.get(i).getMaximumScore().toString(),DESCRIPTION_FONT));
   else
   table.addCell("");
   if(benchMarkingPdfBeanList.get(i).getActualScore()!=null)
   table.addCell(new Phrase(benchMarkingPdfBeanList.get(i).getActualScore().toString(),DESCRIPTION_FONT));
   else
    table.addCell("");
   table.addCell(new Phrase(benchMarkingPdfBeanList.get(i).getWeightedScore().toString(),DESCRIPTION_FONT)); 
   if(benchMarkingPdfBeanList.get(i).getScoreCriteria0()!=null)
   {
    c1 = new PdfPCell(new Phrase(""));
    
    c1.setBorder(0);
    //c1.setBorderColor(BaseColor.WHITE);
    table.addCell(c1);
    
    
    
    table.addCell(new Phrase(benchMarkingPdfBeanList.get(i).getScoreCriteria0(),DESCRIPTION_FONT));
    table.addCell(new Phrase(benchMarkingPdfBeanList.get(i).getScoreCriteria1(),DESCRIPTION_FONT));
    table.addCell(new Phrase(benchMarkingPdfBeanList.get(i).getScoreCriteria2(),DESCRIPTION_FONT));
    table.addCell(new Phrase(benchMarkingPdfBeanList.get(i).getScoreCriteria3(),DESCRIPTION_FONT));
  
   }
   else
   {
    c1 = new PdfPCell(new Phrase(""));
    c1.setBorder(0);
    table.addCell(c1);
    c1 = new PdfPCell(new Phrase(""));      
    table.addCell(c1);
    table.addCell(new Phrase("",DESCRIPTION_FONT));
    table.addCell(new Phrase("",DESCRIPTION_FONT));
    table.addCell(new Phrase("",DESCRIPTION_FONT));
  
   }
  
   
  if(  (i<(benchMarkingPdfBeanList.size()-1)&& benchMarkingPdfBeanList.get(i+1).getSubModulespan()!=0) ||(i==(benchMarkingPdfBeanList.size()-1)))
  {
    
   document.add(new Paragraph(SUB_MODULE + submodule +"tttttttttttttttttttttttttttttttttttttttttttt   Score Evaluation Criteria", subFont));   
   document.add(new Paragraph(BREAKLINE, DESCRIPTION_FONT));
   document.add(table);
   document.add(new Paragraph(BREAKLINE, DESCRIPTION_FONT));
   table= new PdfPTable(colsWidth);
   table.setWidthPercentage(100f);

  }  
  }
  document.close();
  
  PdfReader reader = new PdfReader(new ByteArrayInputStream(out.getBytes()));
  int n = reader.getNumberOfPages();

  // Create a stamper that will copy the document to a new file
  PdfStamper stamp = new PdfStamper(reader, out);
  int i = 1;
  PdfContentByte under;

  Image img = Image
    .getInstance("http://localhost:9080/EcommerceRequirementFramework/images/watermark.jpg");



  while (i <= n) {

   img.setAbsolutePosition(40, 100);

   under = stamp.getUnderContent(i);
   under.addImage(img);

   // Text over the existing page
   /*
    * over = stamp.getOverContent(i); over.beginText();
    * over.setFontAndSize(bf, 18); over.showText("page " + i);
    * over.endText();
    */

   i++;
  }
  stamp.close();
  return out.getBytes();
 }

 /**
  * Adds the header and the footer.
  * 
  * @see com.itextpdf.text.pdf.PdfPageEventHelper#onEndPage(com.itextpdf.text.pdf.PdfWriter,
  *      com.itextpdf.text.Document)
  */
 public void onEndPage(PdfWriter writer, Document document) {

  Rectangle rect = writer.getBoxSize(ART);
  ColumnText.showTextAligned(writer.getDirectContent(),
    Element.ALIGN_CENTER, new Phrase(String.format(PAGE_NUMBER,
      pagenumber)), (rect.getLeft() + rect.getRight()) / 2,
    rect.getBottom() - 18, 0);
  ColumnText.showTextAligned(writer.getDirectContent(),
    Element.ALIGN_LEFT, new Phrase(), rect
      .getLeft(), rect.getBottom() - 18, 0);
  ColumnText.showTextAligned(writer.getDirectContent(),
    Element.ALIGN_RIGHT, new Phrase(new SimpleDateFormat(
      DATE_PATTERN).format(new Date())), rect.getRight(),
    rect.getBottom() - 18, 0);
 }

 
 protected class ByteArrayStreamInfo implements StreamInfo {

  protected String contentType;
  protected byte[] bytes;

  public ByteArrayStreamInfo(String contentType, byte[] bytes) {
   this.contentType = contentType;
   this.bytes = bytes;
  }

  public String getContentType() {
   return contentType;
  }

  public InputStream getInputStream() throws IOException {
   return new ByteArrayInputStream(bytes);
  }
 }
}

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