Hi,
I am using itext to print a (Grid) table in
HTML format. But I could not justify the contents in
the cells with ALIGN_RIGHT as horizontal alignment in the cells.
the cells with ALIGN_RIGHT as horizontal alignment in the cells.
I am using Table class instead of PdfPTable class as I want the output in HTML format.
Can I use PdfP class
to produce HTML format instead of Table Format.
Please find below the part of code (method being
called) which is being used currently for the purpose.
Could anybody tell me what changes do i need to make in following method to
right justify the content in
cell ?
Thanks
& Regards,
Madhur Taneja
Madhur Taneja
****************************************************************************************************
code currently
being used also sent as an attachment.
****************************************************************************************************
private void createGridLeft_( PdfWriter writer,
Document document,
String title, GridBeanROIF grid, boolean showChildren,
TableProperty_ tableProperty,float pageX,float pageY,
boolean showAccountBg, boolean showLastColumn)
String title, GridBeanROIF grid, boolean showChildren,
TableProperty_ tableProperty,float pageX,float pageY,
boolean showAccountBg, boolean showLastColumn)
{
Table table = null;
int tempIndentaion = 0;
try
{
// - font -
//tableProperty.fontSize_
// int fontsize=8;
int fontsize = tableProperty.fontSize_ ;
Font font = FontFactory.getFont( FONT_NAME,
fontsize );
Table table = null;
int tempIndentaion = 0;
try
{
// - font -
//tableProperty.fontSize_
// int fontsize=8;
int fontsize = tableProperty.fontSize_ ;
Font font = FontFactory.getFont( FONT_NAME,
fontsize );
Font redFont = FontFactory.getFont(
FONT_NAME,
fontsize);
redFont.setColor( Color.red );
fontsize);
redFont.setColor( Color.red );
Font headerFont = FontFactory.getFont(
FONT_NAME,
fontsize, HEADER_FONT_STYLE );
fontsize, HEADER_FONT_STYLE );
Font boldFont = FontFactory.getFont(
FONT_NAME,
fontsize, Font.BOLD );
fontsize, Font.BOLD );
Font redBoldFont = FontFactory.getFont(
FONT_NAME,
fontsize, Font.BOLD );
redBoldFont.setColor( Color.red );
fontsize, Font.BOLD );
redBoldFont.setColor( Color.red );
// - grid -
int rowCount =
grid.getRowCount();
int headerCount =
grid.getHeaderCount();
if( !showLastColumn )
{
headerCount = headerCount -1;
}
if( !showLastColumn )
{
headerCount = headerCount -1;
}
boolean hasChildren =
grid.hasChildren();
if ( rowCount > 0
)
{
// - grid -
float tablewidth=tableProperty.pageSize_.width()- MARGIN_LEFT - MARGIN_RIGHT;
float tableheight=tableProperty.pageSize_.height()- MARGIN_TOP - MARGIN_BOTTOM ;
{
// - grid -
float tablewidth=tableProperty.pageSize_.width()- MARGIN_LEFT - MARGIN_RIGHT;
float tableheight=tableProperty.pageSize_.height()- MARGIN_TOP - MARGIN_BOTTOM ;
table = new Table( headerCount
);
table.setWidths(tableProperty.widths_);
table.setBorderColor( COLOR_BORDER );
table.setBorderWidth( 1 );
table.setPadding( PADDING );
table.setBackgroundColor( Color.white );
table.setWidths(tableProperty.widths_);
table.setBorderColor( COLOR_BORDER );
table.setBorderWidth( 1 );
table.setPadding( PADDING );
table.setBackgroundColor( Color.white );
if ( grid.getGroupCount() > 1
|| grid.getGroupId( 0 ) != null
)
{
for ( int j = 0; j < headerCount; j++ )
{
table.setBorder( Rectangle.NO_BORDER );
table.addCell( "" );
}
{
for ( int j = 0; j < headerCount; j++ )
{
table.setBorder( Rectangle.NO_BORDER );
table.addCell( "" );
}
for( int j=0; j<
grid.getGroupCount(); j++ )
{
{
table.setBorder(
Rectangle.TOP | Rectangle.LEFT | Rectangle.RIGHT );
if (
grid.getGroupId(j)!=null )
{
table.addCell( new Phrase(capitalize( grid.getGroupId(j) ), headerFont ) );
}
else
{
table.addCell("");
}
}
}
{
table.addCell( new Phrase(capitalize( grid.getGroupId(j) ), headerFont ) );
}
else
{
table.addCell("");
}
}
}
for ( int j = 0; j <
headerCount; j++ )
{
String value = grid.getHeaderId( j ).replaceAll("_","");
{
String value = grid.getHeaderId( j ).replaceAll("_","");
table.setBackgroundColor(
COLOR_COLUMN_HEADER_BACKGROUND );
if ( j == 0
)
{
table.setBorder( Rectangle.TOP | Rectangle.RIGHT | Rectangle.LEFT );
}
else
{
table.setBorder(Rectangle.TOP | Rectangle.RIGHT );
}
{
table.setBorder( Rectangle.TOP | Rectangle.RIGHT | Rectangle.LEFT );
}
else
{
table.setBorder(Rectangle.TOP | Rectangle.RIGHT );
}
table.addCell( new
Phrase(capitalize( value ), headerFont ) );
}
}
// - bottom padding
-
table.setBackgroundColor(
Color.white );
for ( int j = 0; j <
headerCount; j++ )
{
if ( j == 0 )
{
table.setBorder(Rectangle.BOTTOM | Rectangle.RIGHT | Rectangle.LEFT );
}
else
{
table.setBorder(Rectangle.BOTTOM | Rectangle.RIGHT );
}
{
if ( j == 0 )
{
table.setBorder(Rectangle.BOTTOM | Rectangle.RIGHT | Rectangle.LEFT );
}
else
{
table.setBorder(Rectangle.BOTTOM | Rectangle.RIGHT );
}
table.addCell( ""
);
}
}
// - data
-
table.setBorderWidth( 1 );
table.setBorderColor( COLOR_BORDER );
table.setBorderWidth( 1 );
table.setBorderColor( COLOR_BORDER );
for ( int i = 0; i < rowCount;
i++ )
{
// - rows -
for ( int j = 0; j < headerCount; j++ )
{
Object value = grid.getValue( i, j );
{
// - rows -
for ( int j = 0; j < headerCount; j++ )
{
Object value = grid.getValue( i, j );
if( value instanceof
String
)
{
value = ((String)value ).replaceAll("_","");
{
value = ((String)value ).replaceAll("_","");
}
CellProperty_
cellProperty = cellProperty_(
value, grid.getMultiplier( j ),
grid.getDecimalCount( j ) );
value, grid.getMultiplier( j ),
grid.getDecimalCount( j ) );
// if this is the
first column.
if ( j == 0 )
{
table.setBorder( Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM );
table.setBackgroundColor( Color.WHITE );
}
else
{
table.setBackgroundColor( getFillBackground( grid.getValue( i, "UI_KEY_DISPLAY_ATTRIBUTE" ) ) );
table.setBorder( Rectangle.RIGHT | Rectangle.BOTTOM );
if ( j == 0 )
{
table.setBorder( Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM );
table.setBackgroundColor( Color.WHITE );
}
else
{
table.setBackgroundColor( getFillBackground( grid.getValue( i, "UI_KEY_DISPLAY_ATTRIBUTE" ) ) );
table.setBorder( Rectangle.RIGHT | Rectangle.BOTTOM );
}
hasChildren =
!grid.isLeaf( i );
if ( j == 0 && isShadedRow( grid.getValue( i, "UI_KEY_DISPLAY_ATTRIBUTE" ) ) )
{
if ( cellProperty.color_ == Color.red )
{
table.addCell(
new Phrase(
spaces_( grid.getIndentation( i ) * CHILD_INDENTATION )
+ cellProperty.value_, redBoldFont ) );
if ( j == 0 && isShadedRow( grid.getValue( i, "UI_KEY_DISPLAY_ATTRIBUTE" ) ) )
{
if ( cellProperty.color_ == Color.red )
{
table.addCell(
new Phrase(
spaces_( grid.getIndentation( i ) * CHILD_INDENTATION )
+ cellProperty.value_, redBoldFont ) );
}
else
{
table.addCell(
new Phrase(
spaces_( grid.getIndentation( i ) * CHILD_INDENTATION )
+ cellProperty.value_, boldFont ) );
else
{
table.addCell(
new Phrase(
spaces_( grid.getIndentation( i ) * CHILD_INDENTATION )
+ cellProperty.value_, boldFont ) );
}
}
else
{
if ( cellProperty.color_ == Color.red )
{
table.addCell(
new Phrase(
spaces_( grid.getIndentation( i ) * CHILD_INDENTATION )
+ cellProperty.value_, redFont ) );
}
else
{
if ( grid.getColumnId(j).equals("UI_KEY_ID_PERCENT" ) )
{
Object value1 = grid.getValue( i, j );
Number a = (Number) value1;
java.text.Format fm = new DecimalFormat ("0.00%");
String percStr = fm.format( a );
table.addCell( new Phrase( spaces_( grid.getIndentation( i ) * CHILD_INDENTATION )
+ percStr, font ) );
}
else {
/*
* Apply required indentation to the "Account Name" field (first field) only
*/
}
else
{
if ( cellProperty.color_ == Color.red )
{
table.addCell(
new Phrase(
spaces_( grid.getIndentation( i ) * CHILD_INDENTATION )
+ cellProperty.value_, redFont ) );
}
else
{
if ( grid.getColumnId(j).equals("UI_KEY_ID_PERCENT" ) )
{
Object value1 = grid.getValue( i, j );
Number a = (Number) value1;
java.text.Format fm = new DecimalFormat ("0.00%");
String percStr = fm.format( a );
table.addCell( new Phrase( spaces_( grid.getIndentation( i ) * CHILD_INDENTATION )
+ percStr, font ) );
}
else {
/*
* Apply required indentation to the "Account Name" field (first field) only
*/
if(j <=
1){
tempIndentaion = grid.getIndentation( i ) * CHILD_INDENTATION ;
}
else
{
tempIndentaion = 0;
}
table.addCell(
new Phrase(
spaces_(tempIndentaion )
+ cellProperty.value_, font ) );
}
tempIndentaion = grid.getIndentation( i ) * CHILD_INDENTATION ;
}
else
{
tempIndentaion = 0;
}
table.addCell(
new Phrase(
spaces_(tempIndentaion )
+ cellProperty.value_, font ) );
}
}
}
}
}
// - children
-
if ( showChildren )
{
int childCount = grid.getChildCount( i );
if ( showChildren )
{
int childCount = grid.getChildCount( i );
for ( int k = 0; k
< childCount; k++
)
{
for ( int j = 0; j < headerCount; j++ )
{
Object value = grid.getChildValue( i, k, j );
{
for ( int j = 0; j < headerCount; j++ )
{
Object value = grid.getChildValue( i, k, j );
CellProperty_ cellProperty =
cellProperty_(
value, grid.getMultiplier( j ),
grid.getDecimalCount( j ) );
value, grid.getMultiplier( j ),
grid.getDecimalCount( j ) );
if ( j +
1 < headerCount
)
{
table.setBorder( Rectangle.RIGHT );
}
else
{
table.setBorder( Rectangle.NO_BORDER );
}
{
table.setBorder( Rectangle.RIGHT );
}
else
{
table.setBorder( Rectangle.NO_BORDER );
}
if (
cellProperty.color_ == Color.red
)
{
table.addCell( new Phrase(
spaces_( CHILD_INDENTATION )
+ cellProperty.value_, redFont ) );
}
else
{
table.addCell( new Phrase(
spaces_( CHILD_INDENTATION )
+ cellProperty.value_, font ) );
}
}
}
}
}
{
table.addCell( new Phrase(
spaces_( CHILD_INDENTATION )
+ cellProperty.value_, redFont ) );
}
else
{
table.addCell( new Phrase(
spaces_( CHILD_INDENTATION )
+ cellProperty.value_, font ) );
}
}
}
}
}
// - total -
if ( grid.hasTotal()
)
{
// - spacing -
{
// - spacing -
table.setBackgroundColor(
Color.white );
for ( int j = 0; j <
headerCount; j++
)
{
if ( j + 1 < headerCount )
{
table.setBorder( Rectangle.NO_BORDER);
}
else
{
table.setBorder( Rectangle.NO_BORDER );
}
{
if ( j + 1 < headerCount )
{
table.setBorder( Rectangle.NO_BORDER);
}
else
{
table.setBorder( Rectangle.NO_BORDER );
}
table.addCell( ""
);
}
}
// - total -
for ( int j = 0; j <
headerCount; j++
)
{
Object value = grid.getTotalValue( j );
{
Object value = grid.getTotalValue( j );
CellProperty_
cellProperty = cellProperty_(
value, grid.getMultiplier( j ),
grid.getDecimalCount( j ) );
value, grid.getMultiplier( j ),
grid.getDecimalCount( j ) );
if ( j == 0
)
{
table.setBorder( Rectangle.NO_BORDER );
}
else
{
table.setBorder( Rectangle.TOP
| Rectangle.LEFT
| Rectangle.RIGHT
| Rectangle.BOTTOM );
}
{
table.setBorder( Rectangle.NO_BORDER );
}
else
{
table.setBorder( Rectangle.TOP
| Rectangle.LEFT
| Rectangle.RIGHT
| Rectangle.BOTTOM );
}
table.addCell(
new Phrase( cellProperty.value_, font ) );
}
}
new Phrase( cellProperty.value_, font ) );
}
}
document.add( table
);
}
}
catch( Exception exception )
{
ReportAppLog.logStackTrace(ReportAppLog.ERROR, exception);
}
}
}
catch( Exception exception )
{
ReportAppLog.logStackTrace(ReportAppLog.ERROR, exception);
}
}
private void createGridLeft_( PdfWriter writer, Document document, String title, GridBeanROIF grid, boolean showChildren, TableProperty_ tableProperty,float pageX,float pageY, boolean showAccountBg, boolean showLastColumn)
{ Table table = null; int tempIndentaion = 0; try { // - font - //tableProperty.fontSize_ // int fontsize=8; int fontsize = tableProperty.fontSize_ ; Font font = FontFactory.getFont( FONT_NAME, fontsize ); Font redFont = FontFactory.getFont( FONT_NAME, fontsize); redFont.setColor( Color.red ); Font headerFont = FontFactory.getFont( FONT_NAME, fontsize, HEADER_FONT_STYLE ); Font boldFont = FontFactory.getFont( FONT_NAME, fontsize, Font.BOLD ); Font redBoldFont = FontFactory.getFont( FONT_NAME, fontsize, Font.BOLD ); redBoldFont.setColor( Color.red ); // - grid - int rowCount = grid.getRowCount(); int headerCount = grid.getHeaderCount(); if( !showLastColumn ) { headerCount = headerCount -1; } boolean hasChildren = grid.hasChildren(); if ( rowCount > 0 ) { // - grid - float tablewidth=tableProperty.pageSize_.width()- MARGIN_LEFT - MARGIN_RIGHT; float tableheight=tableProperty.pageSize_.height()- MARGIN_TOP - MARGIN_BOTTOM ; //madhur edit table = new Table( headerCount ); Cell cell2 = new Cell(); cell2.setHorizontalAlignment(Cell.ALIGN_RIGHT); cell2.setHorizontalAlignment(Element.ALIGN_RIGHT); //madhur edit end table.setWidths(tableProperty.widths_); table.setBorderColor( COLOR_BORDER ); table.setBorderWidth( 1 ); table.setPadding( PADDING ); table.setBackgroundColor( Color.white ); if ( grid.getGroupCount() > 1 || grid.getGroupId( 0 ) != null ) { for ( int j = 0; j < headerCount; j++ ) { table.setBorder( Rectangle.NO_BORDER ); table.addCell( "" ); } for( int j=0; j< grid.getGroupCount(); j++ ) { table.setBorder( Rectangle.TOP | Rectangle.LEFT | Rectangle.RIGHT ); if ( grid.getGroupId(j)!=null ) { table.addCell( new Phrase(capitalize( grid.getGroupId(j) ), headerFont ) ); } else { table.addCell(""); } } } for ( int j = 0; j < headerCount; j++ ) { String value = grid.getHeaderId( j ).replaceAll("_",""); table.setBackgroundColor( COLOR_COLUMN_HEADER_BACKGROUND ); if ( j == 0 ) { table.setBorder( Rectangle.TOP | Rectangle.RIGHT | Rectangle.LEFT ); } else { table.setBorder(Rectangle.TOP | Rectangle.RIGHT ); } table.addCell( new Phrase(capitalize( value ), headerFont ) ); } // - bottom padding - table.setBackgroundColor( Color.white ); for ( int j = 0; j < headerCount; j++ ) { if ( j == 0 ) { table.setBorder(Rectangle.BOTTOM | Rectangle.RIGHT | Rectangle.LEFT ); } else { table.setBorder(Rectangle.BOTTOM | Rectangle.RIGHT ); } table.addCell( "" ); } // - data - table.setBorderWidth( 1 ); table.setBorderColor( COLOR_BORDER ); for ( int i = 0; i < rowCount; i++ ) { // - rows - for ( int j = 0; j < headerCount; j++ ) { Object value = grid.getValue( i, j ); if( value instanceof String ) { value = ((String)value ).replaceAll("_",""); } CellProperty_ cellProperty = cellProperty_( value, grid.getMultiplier( j ), grid.getDecimalCount( j ) ); // if this is the first column. if ( j == 0 ) { table.setBorder( Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM ); table.setBackgroundColor( Color.WHITE ); } else { table.setBackgroundColor( getFillBackground( grid.getValue( i, "UI_KEY_DISPLAY_ATTRIBUTE" ) ) ); table.setBorder( Rectangle.RIGHT | Rectangle.BOTTOM ); } hasChildren = !grid.isLeaf( i ); if ( j == 0 && isShadedRow( grid.getValue( i, "UI_KEY_DISPLAY_ATTRIBUTE" ) ) ) { if ( cellProperty.color_ == Color.red ) { table.addCell( new Phrase( spaces_( grid.getIndentation( i ) * CHILD_INDENTATION ) + cellProperty.value_, redBoldFont ) ); cell2.setHorizontalAlignment(Cell.ALIGN_RIGHT); cell2.setHorizontalAlignment(Element.ALIGN_RIGHT); } else { table.addCell( new Phrase( spaces_( grid.getIndentation( i ) * CHILD_INDENTATION ) + cellProperty.value_, boldFont ) ); cell2.setHorizontalAlignment(Cell.ALIGN_RIGHT); cell2.setHorizontalAlignment(Element.ALIGN_RIGHT); } } else { if ( cellProperty.color_ == Color.red ) { table.addCell( new Phrase( spaces_( grid.getIndentation( i ) * CHILD_INDENTATION ) + cellProperty.value_, redFont ) ); cell2.setHorizontalAlignment(Cell.ALIGN_RIGHT); cell2.setHorizontalAlignment(Element.ALIGN_RIGHT); } else { if ( grid.getColumnId(j).equals("UI_KEY_ID_PERCENT" ) ) { Object value1 = grid.getValue( i, j ); Number a = (Number) value1; java.text.Format fm = new DecimalFormat ("0.00%"); String percStr = fm.format( a ); table.addCell( new Phrase( spaces_( grid.getIndentation( i ) * CHILD_INDENTATION ) + percStr, font ) ); cell2.setHorizontalAlignment(Cell.ALIGN_RIGHT); cell2.setHorizontalAlignment(Element.ALIGN_RIGHT); } else { /* * Apply required indentation to the "Account Name" field (first field) only */ if(j <= 1){ tempIndentaion = grid.getIndentation( i ) * CHILD_INDENTATION ; } else { tempIndentaion = 0; } table.addCell( new Phrase( spaces_(tempIndentaion ) + cellProperty.value_, font ) ); } } } } // - children - if ( showChildren ) { int childCount = grid.getChildCount( i ); for ( int k = 0; k < childCount; k++ ) { for ( int j = 0; j < headerCount; j++ ) { Object value = grid.getChildValue( i, k, j ); CellProperty_ cellProperty = cellProperty_( value, grid.getMultiplier( j ), grid.getDecimalCount( j ) ); if ( j + 1 < headerCount ) { table.setBorder( Rectangle.RIGHT ); } else { table.setBorder( Rectangle.NO_BORDER ); } if ( cellProperty.color_ == Color.red ) { table.addCell( new Phrase( spaces_( CHILD_INDENTATION ) + cellProperty.value_, redFont ) ); } else { table.addCell( new Phrase( spaces_( CHILD_INDENTATION ) + cellProperty.value_, font ) ); } } } } } // - total - if ( grid.hasTotal() ) { // - spacing - table.setBackgroundColor( Color.white ); for ( int j = 0; j < headerCount; j++ ) { if ( j + 1 < headerCount ) { table.setBorder( Rectangle.NO_BORDER); } else { table.setBorder( Rectangle.NO_BORDER ); } table.addCell( "" ); } // - total - for ( int j = 0; j < headerCount; j++ ) { Object value = grid.getTotalValue( j ); CellProperty_ cellProperty = cellProperty_( value, grid.getMultiplier( j ), grid.getDecimalCount( j ) ); if ( j == 0 ) { table.setBorder( Rectangle.NO_BORDER ); } else { table.setBorder( Rectangle.TOP | Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM ); } table.addCell( new Phrase( cellProperty.value_, font ) ); } } document.add( table ); } } catch( Exception exception ) { ReportAppLog.logStackTrace(ReportAppLog.ERROR, exception); } }
------------------------------------------------------------------------- Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________ iText-questions mailing list iText-questions@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/itext-questions