Hi,

I am trying to generate a PDF Report using a database query.

I am checking to see if the table fits the page and if not delete the last
row inserted and start a new page (just like in the example Chap0513.java)

This is working fine as long as I do not do the following:

On the first page, I need to add some text above the table (about 3
paragraphs) which I am doing before the table gets printed on the first
page). If I do this, the table is getting messed up and I am unable to split
the table so that cells won't be spanned across multiple pages.

What should I do to fix the above??

Here is my Code:

import java.io.IOException;
import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.*;
import java.sql.*;
public class Practice {
public static void main(String[]args)
 {
   Document doc = new Document(PageSize.A4.rotate());
   HeaderFooter footer = new HeaderFooter(new Phrase("Page "),true);
   footer.setAlignment(footer.ALIGN_RIGHT);
   footer.setBorder(Rectangle.NO_BORDER);

   Connection connection = null;
   try{
   PdfWriter writer = PdfWriter.getInstance(doc,new
FileOutputStream("Practice.pdf"));


   Class.forName("org.gjt.mm.mysql.Driver").newInstance();
   String queryString = "*****"; //masked out for security
   connection = DriverManager.getConnection(*********);//masked out for
security
   Statement stmt = connection.createStatement();
   ResultSet rs = stmt.executeQuery(queryString);
   doc.setFooter(footer);
   doc.open();
   initializePage(doc); //When I call this method and add a few paragraphs
above the table on the first page, the table gets messed up...
// If the above line is skipped then the table is split perfectly.
   int ctr = 1;
   Table aTable = getTable();
   Cell c = null;
   while(rs.next())
    {

      c = new Cell(rs.getString("*******"));
   c.setHorizontalAlignment(c.ALIGN_CENTER);
   aTable.addCell(c);
   c = new Cell(rs.getString("*******"));
   c.setHorizontalAlignment(c.ALIGN_CENTER);
   aTable.addCell(c);
   c = new Cell(rs.getString("*******"));
   c.setHorizontalAlignment(c.ALIGN_CENTER);
   aTable.addCell(c);
   c = new Cell(rs.getString("*******"));
   c.setHorizontalAlignment(c.ALIGN_CENTER);
   aTable.addCell(c);
   c = new Cell(rs.getString("*******"));
   c.setHorizontalAlignment(c.ALIGN_CENTER);
   aTable.addCell(c);
   c = new Cell(rs.getString("*******"));
   c.setHorizontalAlignment(c.ALIGN_CENTER);

   aTable.addCell(c);
   if(!(writer.fitsPage(aTable)))
    {
      aTable.deleteLastRow();
   doc.add(aTable);
   doc.newPage();

      aTable = getTable();

   }

 }
  doc.add(aTable);
   doc.close();

   connection.close();

   }
   catch(Exception ex)
   {try{
   connection.close();
     ex.printStackTrace();
  }
  catch(Exception ex2){}
   }
}

public static Table getTable() throws BadElementException,DocumentException
 {

   Table aTable = new Table(6);
   aTable.setCellsFitPage(true);

   aTable.setWidth(100);
   int headerwidths[] = {26,14,22,15,15,8};
   aTable.setWidths(headerwidths);

  int spacing = 1-1;
   aTable.setSpacing(spacing);
   aTable.setPadding(3);



   Cell c = new Cell("*******");
   c.setBackgroundColor(new java.awt.Color(0xC0, 0xC0, 0xC0));
   c.setHorizontalAlignment(c.ALIGN_CENTER);
   aTable.addCell(c);

   c = new Cell("*******");
   c.setBackgroundColor(new java.awt.Color(0xC0, 0xC0, 0xC0));
   c.setHorizontalAlignment(c.ALIGN_CENTER);
   aTable.addCell(c);

      c = new Cell("*******");
   c.setBackgroundColor(new java.awt.Color(0xC0, 0xC0, 0xC0));
   c.setHorizontalAlignment(c.ALIGN_CENTER);
   aTable.addCell(c);

      c = new Cell("*******");
   c.setBackgroundColor(new java.awt.Color(0xC0, 0xC0, 0xC0));
   c.setHorizontalAlignment(c.ALIGN_CENTER);
   aTable.addCell(c);

      c = new Cell("*******");
   c.setBackgroundColor(new java.awt.Color(0xC0, 0xC0, 0xC0));
   c.setHorizontalAlignment(c.ALIGN_CENTER);
   aTable.addCell(c);

      c = new Cell("*******");
   c.setBackgroundColor(new java.awt.Color(0xC0, 0xC0, 0xC0));
   c.setHorizontalAlignment(c.ALIGN_CENTER);
   aTable.addCell(c);

   return aTable;
 }
 private static void initializePage(Document doc) throws
BadElementException,DocumentException
  {

 Paragraph p = new Paragraph(new Phrase("*******",new
Font(Font.TIMES_ROMAN,14,Font.BOLD|Font.UNDERLINE)));
 p.setAlignment(p.ALIGN_CENTER);
 doc.add(p);
  p = new Paragraph(new Phrase("*******"));
 doc.add(p);
  p = new Paragraph(new Phrase("*******"));
 doc.add(p);
  p = new Paragraph(new Phrase("*******"));
 doc.add(p);
  }

}

Please Advise
 
PS: I have tried PdfPTable and PdfPCell also without any success !

Thanks
Dayal

Reply via email to