I am getting desperate now, cause my table won't render the way I want it to. i was looking at this previous post (viewtopic.php?f=6&t=212&p=529&hilit=gridspan&sid=1e302d3cbcd018686d53fbb5af528e81#p529) but it didn't help me too much, since I think that my table itself is build wrong. So any help would be greatly appreciated. I am trying to describe how I did it as detailed as possible:
1. I am creating a table. I use the ObjectFactory for this, since I thought that with merged cells the TblFactory class would be wrong, because it allready creates every cell.
- Code: Select all
TblPr tblPr = objFactory.createTblPr();
table.setTblPr(tblPr);
TblWidth tblW = objFactory.createTblWidth();
tblW.setW( BigInteger.valueOf( 8 * columnWidth ));
tblW.setType( "dxa" );
tblPr.setTblW( tblW );
The columnwidth atribute I get like the following. I take the whole page and devide it by aight (cause this is the count of columns I want to have, so I can merge them into a construct I want to use):
- Code: Select all
List<SectionWrapper> sections = parent.getDocumentModel().getSections();
return sections.get( sections.size() - 1 ).getPageDimensions().getWritableWidthTwips();
The parent is the WordprocessingMLPackage class. I got this from the examples off the cvs.
after that, I thought that I need to craete the gridCol objects, to tell my table where the columns can be. I thought, that I would create 8 gridCols and the last one would be the width of my whole page. So I did this:
- Code: Select all
void applyTableGrid( final Tbl table, final int width) {
TblGrid tblGrid = objFactory.createTblGrid();
for( int i = 1; i < 9; i++) {
TblGridCol col = objFactory.createTblGridCol();
col.setW( BigInteger.valueOf( i * width ) );
tblGrid.getGridCol().add( col );
}
table.setTblGrid( tblGrid );
}
The width argument is the columnwidth from before. Now I have 8 gridCols in my table.
So in a first step, which I will use as example, because I guess if I know how that one works, I can construct the rest by myself, I want to create two cells. Cell1 will have a span of 2 columns, cell2 will have a span of 6 columns, so together they should fill the whole page.
- Code: Select all
int line;
for ( line = 0; line < 4 ; line++ ) {
Tr tr = objFactory.createTr();
Tc tc0 = objFactory.createTc();
tr.getEGContentCellContent().add( tc0 );
addFormattedRun( tc0, TABLE_HEADERS[ line ] );
table.applyGridSpan( tc0, 2 );
Tc tc1 = objFactory.createTc();
tr.getEGContentCellContent().add( tc1 );
table.applyGridSpan( tc1 , 6 );
List<Object> tcContent = tc1.getEGBlockLevelElts();
tcContent.addAll( getContent( line, case ) );
if ( tcContent.size() > 1 ) {
tcContent.remove( 0 );
}
table.getTable().getEGContentRowContent().add( tr );
}
This is creating 4 Tr objects and filling them with content. On every TC object I invoke the applyGridSpan method, which looks like this:
- Code: Select all
void applyGridSpan( final Tc cell, final int span ) {
TcPr tcPr = objFactory.createTcPr();
TblWidth tblWidth = objFactory.createTblWidth();
tblWidth.setType( "dxa" );
tblWidth.setW( BigInteger.valueOf( columnWidth * span ) );
tcPr.setTcW( tblWidth );
if ( span > 1) {
GridSpan gridSpan = objFactory.createTcPrInnerGridSpan();
gridSpan.setVal(BigInteger.valueOf(span));
tcPr.setGridSpan(gridSpan);
}
cell.setTcPr(tcPr);
}
So on my properties, I first set the width of the cell. I calculate it from the span multiplied by the column width which I calculated before. Those values are always alligned with the GridCol Objects I created on the table, so they should fit. After that I apply the GridSpan Object on my cell.
When I use this code, my cells both have the exact same width. They look like a regular 2 column table where each column takes 50 % of the space. I created a word document with an example table and looked at the XML and it seems that mine is pretty much the same (except for the GridCol, but I figured that might be because I calculated mine differently - I am not exactly sure though).
So is there something I am missing here? I am kind of stuck at this point because the rest of my rows in that table have more spans and weirder constructs.
I hope someone can help me with this.
Best regards,
-- artur