简体   繁体   中英

itext filling table

I need to fill a table with in the first column a number and in the second column certain data from a array.

So it will look like :

1 | Data1

2 | Data2

3 | Data3

What is the best way to add this data.

Usualy you do something like :

 Table table = new Table(3); 
 table.setWidth(100);           
 table.setDefaultVerticalAlignment(Element.ALIGN_TOP);           
 table.setCellsFitPage(true);            
 table.setPadding(2);            
 table.setSpacing(0);              
 for (int i = 1; i <= 6; i++) {            
 Cell cell = new Cell("Data " + i);            
 table.addCell(cell); }

Then you get something like :

Data 1 | Data 2 | Data 3

Data 4 | Data 5 | Data 6

Is there a specific way to fill selected cells or do I see it wrong.

Table table = new Table( 2 ); // 2 is the number of columns

table.setWidth( 100 );
table.setDefaultVerticalAlignment( Element.ALIGN_TOP ) ;
table.setCellsFitPage( true );
table.setPadding( 2 );
table.setSpacing( 0 );

for ( int i = 1 ; i <= 3 ; i++ )
{
     Cell leftCell = new Cell( i );
     Cell rightCell = new Cell( "Data " + i );

     table.addCell( leftCell );
     table.addCell( rightCell );
}

As a sidenote : the Table class can be found in rather old versions of iText that are not supported anymore. Most recent versions make use of PdfPTable.

I warmly recommend you to upgrade to a more recent version in order to get all the latest features, bug fixes and security fixes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM