We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Nested tables

Hi ,


I would like to achieve a specific table structure, The structure is as follows.

There are 3 tables say A, B and C

Table A - 2 rows 2 columns
1st cell has colspan of 2 and rest have 1 colspan.
Table A has 100 100 column width respectively

Table B - 1 row 1 column
colspan is 1
Table B has 100 column width

and Table C - 1 row and 2 columns

the 1st column conatins table A and 2nd contains table B

I used following code to achieve this

private static Table getTable() {
Table table1 = new Table();
table1.setColumnWidths( “100 100” );
Row row = table1.getRows().add();
Cell cell = row.getCells().add();
cell.getParagraphs().add( new TextFragment( “1” ) );
cell.setColSpan( 2 );
row = table1.getRows().add();
cell = row.getCells().add();
cell.getParagraphs().add( new TextFragment( “2” ) );
cell.setColSpan( 1 );
cell = row.getCells().add();
cell.getParagraphs().add( new TextFragment( “3” ) );
cell.setColSpan( 1 );
Table table2 = new Table();
table2.setColumnWidths( “100” );
row = table2.getRows().add();
cell = row.getCells().add();
cell.getParagraphs().add( new TextFragment( “4” ) );
Table table = new Table();
//table.setColumnWidths( “200 100” );
row = table.getRows().add();
cell = row.getCells().add();
cell.getParagraphs().add( table1 );
cell = row.getCells().add();
cell.getParagraphs().add( table2 );
return table;
}

This code insert cell with content 4 above cell with content 3.
Which is not expected.

NOTE - if I uncomment this line from Table C - table.setColumnWidths( “200 100” );
Then the desired behaviour is achieved but in actual scenarios I wont be able to calculate table widths of the childs differently. So please provide a solution with that line commented. If I am doing something wrong please correct me.


I am using Aspose 11 with com.aspose.pdf classes

Hi Saurabh,


Thank for your inquriy. Please note default column width of table is 100, so if we do not define column widths API sets 100 width automatically and shows incorrect results. You need some pre-processing for column width calculation. Please check following code snippet, you can modify it to as per your need.

com.aspose.pdf.Table table1 = new
com.aspose.pdf.Table();<o:p></o:p>

int table1_col1_width=100;

int table1_col2_width=100;

table1.setColumnWidths( table1_col1_width +" "+ table1_col2_width );

Row row = table1.getRows().add();

Cell cell = row.getCells().add();

cell.getParagraphs().add( new TextFragment( "1" ) );

cell.setColSpan( 2 );

row = table1.getRows().add();

cell = row.getCells().add();

cell.getParagraphs().add( new TextFragment( "2" ) );

cell.setColSpan( 1 );

cell = row.getCells().add();

cell.getParagraphs().add( new TextFragment( "3" ) );

cell.setColSpan( 1 );

com.aspose.pdf.Table table2 = new com.aspose.pdf.Table();

int table2_col1_width=100;

table2.setColumnWidths( ""+table2_col1_width );

row = table2.getRows().add();

cell = row.getCells().add();

cell.getParagraphs().add( new TextFragment( "4" ) );

com.aspose.pdf.Table table = new com.aspose.pdf.Table();

int table_col1_width=table1_col1_width+table1_col2_width;

int table_col2_width=table2_col1_width;

table.setColumnWidths( table_col1_width+" "+table_col2_width);

row = table.getRows().add();

cell = row.getCells().add();

cell.getParagraphs().add( table1 );

cell = row.getCells().add();

cell.getParagraphs().add( table2 );


We are sorry for the inconvenience caused.

Best Regards,