Paulo,

 

It is not impossible. The following is my code. I have added a comment "//*** Problem Area ***//" where

I used both ways to get the results I have mentioned in my previous posting. What is the cause of this

outcome?

 

public static synchronized ByteArrayOutputStream getPDF(File file,

    String[] header,

    String[] tableheader,

    String[][] tablecontent,

    Vector visibleRatioNames,

    String[] groupStrings,

    Vector graphs) throws

    DocumentException, FileNotFoundException, IOException

 

{

  Font fontGreyHevNormal_11 =  FontFactory.getFont(FontFactory.HELVETICA_BOLD, 11,

Font.NORMAL,Color.gray);

  Font fontBlackHevNormal_9 = FontFactory.getFont(FontFactory.HELVETICA, 9, Font.NORMAL);

  Font fontRatioHevNorm_7 = FontFactory.getFont(FontFactory.HELVETICA, 7, Font.NORMAL);

  Font fontHevNorm_10 = FontFactory.getFont(FontFactory.HELVETICA_BOLD, 10, Font.NORMAL);

  Font fontHevNorm_8= FontFactory.getFont(FontFactory.HELVETICA_BOLD, 8, Font.NORMAL);

  FontFactory.getFont(FontFactory.HELVETICA, 9, Font.NORMAL);

  Document doc = new Document(PageSize.A4, 70, 70, 70, 70);

  ByteArrayOutputStream bos = new ByteArrayOutputStream();

  PdfWriter docWriter = PdfWriter.getInstance(doc, bos);

  doc.open();

  // Add the document header.

  Table table1 = new Table(1, 1);

  table1.setWidth(100);

  table1.setBorderColor(new Color(0, 0, 0));

  table1.setSpaceInsideCell(2);

  Chunk headerchunk = new Chunk(header[0], fontGreyHevNormal_11);

  Cell headerCell = new Cell(headerchunk);

  headerCell.setVerticalAlignment(Cell.ALIGN_CENTER);

  table1.addCell(headerCell);

  doc.add(table1);

  // Sub headings

  Chunk subheading1 = new Chunk(header[1], fontBlackHevNormal_9);

  Chunk subheading2 = new Chunk(header[2], fontBlackHevNormal_9);

  Chunk subheading3 = new Chunk(header[3], fontBlackHevNormal_9);

  doc.add(new Paragraph(" "));

  doc.add(new Paragraph(subheading1));

  doc.add(new Paragraph(subheading2));

  doc.add(new Paragraph(subheading3));

  // Content table

  Table contentTable = new Table(2 + tableheader.length,

                                 tablecontent.length);

  contentTable.setWidth(100);

  contentTable.setBorderWidth(0.5f);

  contentTable.setBorderColor(Color.gray);

  int[] widths = new int[2 + tableheader.length];

  if(widths.length == 2){ widths[0]=50;widths[1]=50;

  }else if(widths.length == 3){ widths[0]=7;widths[1]=43;widths[2]=50;

  }else if(widths.length == 4){ widths[0]=7;widths[1]=43;widths[2]=25;widths[3]=25;

  }else if(widths.length == 5){ widths[0]=7;widths[1]=33;widths[2]=20;widths[3]=20;widths[4]=20;}

  contentTable.setWidths(widths);

  contentTable.setPadding(2);

  for (int i = 0; i < tableheader.length; i++) {

    Chunk tableheading = new Chunk(tableheader[i], fontRatioHevNorm_7);

    Cell c = new Cell(tableheading);

    c.setVerticalAlignment(Cell.ALIGN_MIDDLE);

    c.setBorderColor(Color.gray);

    contentTable.addCell(c, new Point(0, 2 + i));

  }

  for (int i = 1; i < tablecontent.length; i++) {

    String[] str = tablecontent[i - 1];

    for (int j = 0; j < str.length - 1; j++) {

      if (j == 0 && str[str.length - 1].equalsIgnoreCase("true")) {

        fontRatioHevNorm_7.setColor(new Color(255, 153, 51));

      }

      else {

        fontRatioHevNorm_7.setColor(Color.black);

      }

      Cell c = new Cell(new Chunk(str[j], fontRatioHevNorm_7));

      c.setVerticalAlignment(Cell.ALIGN_CENTER);

      c.setBorderColor(Color.gray);

      contentTable.addCell(c, new Point(i, j));

    }

  }

  doc.add(contentTable);

  doc.newPage();

 

  int visibleRatioSize = visibleRatioNames.size();

  int graphCounter = 0;

  for (int i = 0; i < visibleRatioSize; i++) {

 

    String ratioName = (String) visibleRatioNames.get(i);

    Table ratioNameTable = new Table(1, 1);

    ratioNameTable.setWidth(100);

    ratioNameTable.setBorderColor(new Color(0, 0, 0));

    ratioNameTable.setSpaceInsideCell(2);

    Chunk ratioNamechunk = new Chunk(ratioName, fontHevNorm_10);

    Cell ratioNameCell = new Cell(ratioNamechunk);

    headerCell.setVerticalAlignment(Cell.ALIGN_CENTER);

    ratioNameTable.addCell(ratioNameCell);

    doc.add(ratioNameTable);

    int grpStrLength = groupStrings.length;

    float absX = 70;

    float absY = 100;

 

    for (int grpStr = 0; grpStr < grpStrLength; grpStr++) {

      Chunk grpHeading = new Chunk(groupStrings[grpStr], fontHevNorm_8);

      doc.add(new Paragraph(grpHeading));

      if (graphCounter < graphs.size()) {

 

//**************************** Problem Area ********************************//

        /* Option 1 :

           PDF generation time: 3616 milliseconds

           File size: 4.47 MB

       */

        URL url = "" graphs.get(graphCounter);

        Image img = Image.getInstance(url);

 

        /* Option 2 :

           PDF generation time: 9914 milliseconds

           File size: 372 KB

        */

        // Image img = Image.getInstance((java.awt.Image) graphs.get(graphCounter),null);

 

//*************************************************************************//

 

        img.scalePercent(75, 75);

        img.setAlignment(Image.MIDDLE);

        doc.add(img);

        graphCounter++;

      }

    }

    doc.newPage();

  }

  doc.close();

  FileOutputStream fos = new FileOutputStream(file);

  fos.write(bos.toByteArray());

  fos.close();

  return bos;

}

 

Rochana.

Reply via email to