tags:

views:

184

answers:

1

I have used PdfPTable to convert table data into a pdf file using com.itextpdf.text.pdf.PdfPTable. Table is displaying, but table data and the header are in same style. To make difference i have to set the header font style to bold. Can anybody help me out in this? I have attached my code here.

Thanks in advance.

import java.awt.Color;
import java.util.ArrayList;
import java.util.List;

import javax.faces.model.ListDataModel;

import com.mypackage.core.filter.domainobject.FilterResultDO;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPTable;

public class PDFGenerator {


 //This method will generate PDF for Filter Result Screen (only DataTable level)
  @SuppressWarnings("unchecked")

 public static PdfPTable generatePDF(PdfPTable table,List<FilterResultDO> filterResultDOList ,List<ColumnHeader> filterResultHeaderList )
 {
  //Initialize the table with number of columns required for the Datatable header
  int numberOfFilterLabelCols = filterResultHeaderList.size();

  //PDF Table Frame
  table =  new PdfPTable(numberOfFilterLabelCols);



     //Getting Filter Detail Table Heading
     for(int i = 0 ; i < numberOfFilterLabelCols; i++)
     {
       ColumnHeader commandHeaderObj =  filterResultHeaderList.get(i);

       table.addCell(commandHeaderObj.getLabel());


     }

     //Getting Filter Detail Data (Rows X Cols)
  FilterResultDO filterResultDOObj = filterResultDOList.get(0);



  List <List> filterResultDataList = filterResultDOObj.getFilterResultLst();
  int numberOfFilterDataRows = filterResultDataList.size();


    //each row iteration
    for(int row = 0; row < numberOfFilterDataRows; row++)
    { 
     List filterResultCols = filterResultDataList.get(row);
     int numberOfFilterDataCols = filterResultCols.size();

     //columns iteration of each row 
     for(int col = 0; col < numberOfFilterDataCols ; col++)
     { 
      String filterColumnsValues = (String) filterResultCols.get(col);

      table.addCell(filterColumnsValues);
     }
    }

  return table;
 }//generatePDF




}
+1  A: 

I suppose that the commandHeaderObj.getLabel() method return a string. you just have to define a font (in the method, or as an instance attribute or a static final class attribute...)

Font tableHeader = 
   FontFactory.getFont(FontFactory.HELVETICA, 10, Font.BOLD);

and then apply it to the cell

for(int i = 0 ; i < numberOfFilterLabelCols; i++)
 {
   ColumnHeader commandHeaderObj =  filterResultHeaderList.get(i);
   Paragraph header = new Paragraph();
   header.setFont(tableHeader);
   header.add(commandHeaderObj.getLabel());
   table.addCell(header);
 }

hope this help

Guillaume

PATRY