Hi,
I try to export a large JTable into a PDF with Cell Colors, CheckBox
compoenents in Cells as below:
1) I don't get the proper pagination
2) I don;t get the Cell Colors
public static void export( JTable table )
{
Document document = new Document( PageSize.A4.rotate() );
try
{
PdfWriter writer = PdfWriter.getInstance( document, new
FileOutputStream( FILE_NAME ) );
document.open();
//document.addTitle("TIA Strategy Grid");
PdfContentByte cb = writer.getDirectContent();
PdfPTable datatable = getPdfPTable( table );
datatable.setTotalWidth(1200 );
datatable.writeSelectedRows(0,datatable.getRows().size(), 0, 500,
cb );
}
catch( DocumentException de )
{
m_logger.error( de.getMessage(), de );
}
catch( IOException ioe )
{
m_logger.error( ioe.getMessage(), ioe );
}
document.close();
try{
Runtime rt=Runtime.getRuntime();
rt.exec("cmd.exe /c " + FILE_NAME);
}catch (Exception ex) {
JOptionPane.showMessageDialog( null, "Error " + ex );
}
}
private static PdfPTable getPdfPTable(JTable table ) throws
DocumentException
{
PdfPTable datatable = new PdfPTable( table.getColumnCount() - 1 );
int headerwidths[] = new int[table.getColumnCount() - 1];
for( int i = 1; i < table.getColumnCount(); i++ )
{
TableColumn col = table.getColumnModel().getColumn( i );
headerwidths[i - 1] = col.getPreferredWidth();
}
datatable.setWidths( headerwidths );
datatable.setWidthPercentage( 80 );
datatable.getDefaultCell().setBorderWidth( 2 );
datatable.getDefaultCell().setPadding(5);
datatable.getDefaultCell().setHorizontalAlignment( Element.ALIGN_CENTER
);
PdfPCell cell;
for( int i = 1; i < table.getColumnCount(); i++ )
{
cell = new PdfPCell(new Phrase(table.getColumnName( i )));
cell.setGrayFill(0.5f);
cell.setHorizontalAlignment( Element.ALIGN_CENTER );
datatable.addCell( cell );
}
datatable.setHeaderRows(1);
datatable.getDefaultCell().setBorderWidth(1);
for( int i = 0; i < table.getRowCount(); i++ )
{
for( int j = 1; j < table.getColumnCount(); j++ )
{
datatable.addCell( table.getValueAt( i, j ) != null ?
table.getValueAt( i, j ).toString() : "null" );
}
}
datatable.setSplitLate( false );
return datatable;
}
Chandrajeet Padhy.
____________________________________________________________________________________
Never miss a thing. Make Yahoo your home page.
http://www.yahoo.com/r/hs
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/