private void setCellVMerge(Tc tableCell, String mergeVal) { if (mergeVal != null) { TcPr tableCellProperties = tableCell.getTcPr(); if (tableCellProperties == null) { tableCellProperties = new TcPr(); tableCell.setTcPr(tableCellProperties); } VMerge merge = new VMerge(); if (!"close".equals(mergeVal)) { merge.setVal(mergeVal); } tableCellProperties.setVMerge(merge); } }
/* (non-Javadoc) * @see org.docx4j.model.Model#toJAXB() */ @Override public Object toJAXB() { ObjectFactory factory = Context.getWmlObjectFactory(); Tbl tbl = factory.createTbl(); // <w:tblPr> TblPr tblPr = null; if (tblPr == null) { log.warn("tblPr is null"); tblPr = factory.createTblPr(); // Default to page width TblWidth tblWidth = factory.createTblWidth(); tblWidth.setW(BigInteger.valueOf(DEFAULT_PAGE_WIDTH_TWIPS)); // TODO: shouldn't hard code that. Pass it in? tblWidth.setType("dxa"); // twips tblPr.setTblW(tblWidth); } tbl.setTblPr(tblPr); // <w:tblGrid> // This specifies the number of columns, // and also their width if (tblGrid == null) { log.warn("tblGrid is null"); tblGrid = factory.createTblGrid(); // Default to equal width int width = Math.round(tbl.getTblPr().getTblW().getW().floatValue() / getColCount()); for (int i = 0; i < getColCount(); i++) { TblGridCol tblGridCol = factory.createTblGridCol(); tblGridCol.setW(BigInteger.valueOf(width)); // twips tblGrid.getGridCol().add(tblGridCol); } } tbl.setTblGrid(tblGrid); // <w:tr> // we need a table row, even if every entry is just a vertical span, // so that is easy for (int i = 0; i < cells.size(); i++) { Tr tr = factory.createTr(); tbl.getEGContentRowContent().add(tr); // populate the row for (int j = 0; j < getColCount(); j++) { Tc tc = factory.createTc(); Cell cell = cells.get(i).get(j); if (cell == null) { // easy, nothing to do. // this is just an empty tc tr.getEGContentCellContent().add(tc); } else { if (cell.isDummy()) { // we need to determine whether this is a result of // a vertical merge or a horizontal merge if (j > 0 && (cells.get(i).get(j - 1).isDummy() || cells.get(i).get(j - 1).colspan > 1)) { // Its a horizontal merge, so // just leave it out } else if (i > 0 && (cells.get(i - 1).get(j).isDummy() || cells.get(i - 1).get(j).rowspan > 1)) { // Its a vertical merge TcPr tcPr = factory.createTcPr(); VMerge vm = factory.createTcPrInnerVMerge(); tcPr.setVMerge(vm); tc.setTcPr(tcPr); tr.getEGContentCellContent().add(tc); // Must have an empty paragraph P p = factory.createP(); tc.getEGBlockLevelElts().add(p); } else { log.error("Encountered phantom dummy cell at (" + i + "," + j + ") "); log.debug(debugStr()); } } else { // a real cell TcPr tcPr = factory.createTcPr(); if (cell.colspan > 1) { // add <w:gridSpan> GridSpan gridSpan = factory.createTcPrInnerGridSpan(); gridSpan.setVal(BigInteger.valueOf(cell.colspan)); tcPr.setGridSpan(gridSpan); tc.setTcPr(tcPr); } if (cell.rowspan > 1) { // Its a vertical merge VMerge vm = factory.createTcPrInnerVMerge(); vm.setVal("restart"); tcPr.setVMerge(vm); tc.setTcPr(tcPr); } if (cell.colspan > 1 && cell.rowspan > 1) { log.warn("Both rowspan & colspan set; that will be interesting.."); } tr.getEGContentCellContent().add(tc); // Add the cell content, if we have it. // We won't have compatible content if this model has // been created via XSLT for an outward bound conversion. // But in that case, this method isn't needed // because the developer started with the JAXB model. Node foreign = cell.getContent(); // eg a <td> for (int n = 0; n < foreign.getChildNodes().getLength(); n++) { Object o; try { o = XmlUtils.unmarshal(foreign.getChildNodes().item(n)); tc.getEGBlockLevelElts().add(o); } catch (JAXBException e) { e.printStackTrace(); } } } } } } return tbl; }