private void setCellHMerge(Tc tableCell, int horizontalMergedCells) { if (horizontalMergedCells > 1) { TcPr tableCellProperties = tableCell.getTcPr(); if (tableCellProperties == null) { tableCellProperties = new TcPr(); tableCell.setTcPr(tableCellProperties); } GridSpan gridSpan = new GridSpan(); gridSpan.setVal(new BigInteger(String.valueOf(horizontalMergedCells))); tableCellProperties.setGridSpan(gridSpan); tableCell.setTcPr(tableCellProperties); } }
/** * In this method we create a table cell properties object and a table width object. We set the * given width on the width object and then add it to the properties object. Finally we set the * properties on the table cell. */ private void setCellWidth(Tc tableCell, int width) { TcPr tableCellProperties = new TcPr(); TblWidth tableWidth = new TblWidth(); tableWidth.setW(BigInteger.valueOf(width)); tableCellProperties.setTcW(tableWidth); tableCell.setTcPr(tableCellProperties); }
/** * Fills table cell with given hex color * * @param cell * @param hexColor */ private void fillTableCell(Tc cell, String hexColor) { TcPr cellProps = cell.getTcPr(); if (null == cellProps) cellProps = objectFactory.createTcPr(); CTShd shd = objectFactory.createCTShd(); shd.setFill(hexColor); shd.setThemeFill(STThemeColor.BACKGROUND_2); cellProps.setShd(shd); cell.setTcPr(cellProps); }
private void setCellMargins(Tc tableCell, Map<String, Object> style) { int top = 0, left = 0, bottom = 0, right = 0; if (style.containsKey(StyleFormatConstants.PADDING_LEFT)) { Float val = (Float) style.get(StyleFormatConstants.PADDING_LEFT); left = val.intValue(); } if (style.containsKey(StyleFormatConstants.PADDING_RIGHT)) { Float val = (Float) style.get(StyleFormatConstants.PADDING_RIGHT); right = val.intValue(); } if (style.containsKey(StyleFormatConstants.PADDING_TOP)) { Float val = (Float) style.get(StyleFormatConstants.PADDING_TOP); top = val.intValue(); } if (style.containsKey(StyleFormatConstants.PADDING_BOTTOM)) { Float val = (Float) style.get(StyleFormatConstants.PADDING_BOTTOM); bottom = val.intValue(); } TcPr tableCellProperties = tableCell.getTcPr(); if (tableCellProperties == null) { tableCellProperties = new TcPr(); tableCell.setTcPr(tableCellProperties); } TcMar margins = new TcMar(); if (bottom > 0) { TblWidth bW = new TblWidth(); bW.setType("dxa"); bW.setW(BigInteger.valueOf(pixelsToDxa(bottom))); margins.setBottom(bW); } if (top > 0) { TblWidth tW = new TblWidth(); tW.setType("dxa"); tW.setW(BigInteger.valueOf(pixelsToDxa(top))); margins.setTop(tW); } if (left > 0) { TblWidth lW = new TblWidth(); lW.setType("dxa"); lW.setW(BigInteger.valueOf(pixelsToDxa(left))); margins.setLeft(lW); } if (right > 0) { TblWidth rW = new TblWidth(); rW.setType("dxa"); rW.setW(BigInteger.valueOf(pixelsToDxa(right))); margins.setRight(rW); } tableCellProperties.setTcMar(margins); }
private void setCellNoWrap(Tc tableCell) { TcPr tableCellProperties = tableCell.getTcPr(); if (tableCellProperties == null) { tableCellProperties = new TcPr(); tableCell.setTcPr(tableCellProperties); } BooleanDefaultTrue b = new BooleanDefaultTrue(); b.setVal(true); tableCellProperties.setNoWrap(b); }
private void setCellColor(Tc tableCell, String color) { if (color != null) { TcPr tableCellProperties = tableCell.getTcPr(); if (tableCellProperties == null) { tableCellProperties = new TcPr(); tableCell.setTcPr(tableCellProperties); } CTShd shd = new CTShd(); shd.setFill(color); tableCellProperties.setShd(shd); } }
private void setCellWidth(Tc tableCell, int width) { if (width > 0) { TcPr tableCellProperties = tableCell.getTcPr(); if (tableCellProperties == null) { tableCellProperties = new TcPr(); tableCell.setTcPr(tableCellProperties); } TblWidth tableWidth = new TblWidth(); tableWidth.setType("dxa"); tableWidth.setW(BigInteger.valueOf(width)); tableCellProperties.setTcW(tableWidth); } }
private void setVerticalAlignment(Tc tableCell, STVerticalJc align) { if (align != null) { TcPr tableCellProperties = tableCell.getTcPr(); if (tableCellProperties == null) { tableCellProperties = new TcPr(); tableCell.setTcPr(tableCellProperties); } CTVerticalJc valign = new CTVerticalJc(); valign.setVal(align); tableCellProperties.setVAlign(valign); } }
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); } }
private void setCellBorders(Tc tableCell, Map<String, Object> style) { TcPr tableCellProperties = tableCell.getTcPr(); if (tableCellProperties == null) { tableCellProperties = new TcPr(); tableCell.setTcPr(tableCellProperties); } CTBorder border = new CTBorder(); // border.setColor("auto"); border.setSpace(new BigInteger("0")); border.setVal(STBorder.SINGLE); TcBorders borders = new TcBorders(); if (style.containsKey(StyleFormatConstants.BORDER_LEFT)) { Float val = (Float) style.get(StyleFormatConstants.BORDER_LEFT); border.setSz(BigInteger.valueOf((long) (val / 2))); Color color = (Color) style.get(StyleFormatConstants.BORDER_LEFT_COLOR); border.setColor(ColorUtil.getHexColor(color).substring(1)); borders.setLeft(border); } if (style.containsKey(StyleFormatConstants.BORDER_RIGHT)) { Float val = (Float) style.get(StyleFormatConstants.BORDER_RIGHT); border.setSz(BigInteger.valueOf((long) (val / 2))); Color color = (Color) style.get(StyleFormatConstants.BORDER_RIGHT_COLOR); border.setColor(ColorUtil.getHexColor(color).substring(1)); borders.setRight(border); } if (style.containsKey(StyleFormatConstants.BORDER_TOP)) { Float val = (Float) style.get(StyleFormatConstants.BORDER_TOP); border.setSz(BigInteger.valueOf((long) (val / 2))); Color color = (Color) style.get(StyleFormatConstants.BORDER_TOP_COLOR); border.setColor(ColorUtil.getHexColor(color).substring(1)); borders.setTop(border); } if (style.containsKey(StyleFormatConstants.BORDER_BOTTOM)) { Float val = (Float) style.get(StyleFormatConstants.BORDER_BOTTOM); border.setSz(BigInteger.valueOf((long) (val / 2))); Color color = (Color) style.get(StyleFormatConstants.BORDER_BOTTOM_COLOR); border.setColor(ColorUtil.getHexColor(color).substring(1)); borders.setBottom(border); } tableCellProperties.setTcBorders(borders); }
private void setTextDirection(Tc tableCell, short textRotation) { String dir = null; if (textRotation == 90) { dir = "btLr"; } else if (textRotation == -90) { dir = "tbRl"; } if (dir != null) { TcPr tableCellProperties = tableCell.getTcPr(); if (tableCellProperties == null) { tableCellProperties = new TcPr(); tableCell.setTcPr(tableCellProperties); } TextDirection td = new TextDirection(); td.setVal(dir); tableCellProperties.setTextDirection(td); } }
/* (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; }