org.docx4j.wml.Tbl Java Examples
The following examples show how to use
org.docx4j.wml.Tbl.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WmlElementUtils.java From docx4j-template with Apache License 2.0 | 7 votes |
public static void replaceTable(String[] placeholders, List<Map<String, String>> textToAdd, WordprocessingMLPackage template) throws Docx4JException, JAXBException { List<Tbl> tables = getTargetElements(template.getMainDocumentPart(), Tbl.class); // 1. find the table Tbl tempTable = getTable(tables, placeholders[0]); List<Tr> rows = getTargetElements(tempTable, Tr.class); // first row is header, second row is content if (rows.size() == 2) { // this is our template row Tr templateRow = (Tr) rows.get(1); for (Map<String, String> replacements : textToAdd) { // 2 and 3 are done in this method addRowToTable(tempTable, templateRow, replacements); } // 4. remove the template row tempTable.getContent().remove(templateRow); } }
Example #2
Source File: WordprocessingMLPackageRender.java From docx4j-template with Apache License 2.0 | 7 votes |
public void addBorders(Tbl table) { table.setTblPr(new TblPr()); CTBorder border = new CTBorder(); border.setColor("auto"); border.setSz(new BigInteger("4")); border.setSpace(new BigInteger("0")); border.setVal(STBorder.SINGLE); TblBorders borders = new TblBorders(); borders.setBottom(border); borders.setLeft(border); borders.setRight(border); borders.setTop(border); borders.setInsideH(border); borders.setInsideV(border); table.getTblPr().setTblBorders(borders); }
Example #3
Source File: AddingAnInlineImageToTable.java From docx4j-template with Apache License 2.0 | 7 votes |
/** * 首先我们创建包和对象工厂, 因此在类的随处我们都可以使用它们. 然后我们创建一个表格并添加 * 边框. 接下来我们创建一个表格行并在第一个域添加一些文本. * 对于第二个域, 我们用与前面一样的图片创建一个段落并添加进去. 最后把行添加到表格中, 并将 * 表格添加到包中, 然后保存这个包. */ public static void main (String[] args) throws Exception { wordMLPackage = WordprocessingMLPackage.createPackage(); factory = Context.getWmlObjectFactory(); Tbl table = factory.createTbl(); addBorders(table); Tr tr = factory.createTr(); P paragraphOfText = wordMLPackage.getMainDocumentPart().createParagraphOfText("Field 1"); addTableCell(tr, paragraphOfText); File file = new File("src/main/resources/iProfsLogo.png"); P paragraphWithImage = addInlineImageToParagraph(createInlineImage(file)); addTableCell(tr, paragraphWithImage); table.getContent().add(tr); wordMLPackage.getMainDocumentPart().addObject(table); wordMLPackage.save(new java.io.File("src/main/files/HelloWord8.docx")); }
Example #4
Source File: Docx4J_简单例子2.java From docx4j-template with Apache License 2.0 | 6 votes |
public void setTableGridCol(WordprocessingMLPackage wordPackage,ObjectFactory factory,Tbl table,double[] widthPercent) throws Exception{ int width=getWritableWidth(wordPackage); TblGrid tblGrid = factory.createTblGrid(); for (int i = 0; i <widthPercent.length; i++) { TblGridCol gridCol = factory.createTblGridCol(); gridCol.setW(BigInteger.valueOf((long) (width*widthPercent[i]/100))); tblGrid.getGridCol().add(gridCol); } table.setTblGrid(tblGrid); TblPr tblPr=table.getTblPr(); if(tblPr==null){ tblPr=factory.createTblPr(); } TblWidth tblWidth=new TblWidth(); tblWidth.setType("dxa");//这一行是必须的,不自己设置宽度默认是auto tblWidth.setW(new BigInteger(""+width)); tblPr.setTblW(tblWidth); table.setTblPr(tblPr); }
Example #5
Source File: WmlElementUtils.java From docx4j-template with Apache License 2.0 | 6 votes |
/** * @Description: 跨行合并 */ public static void mergeCellsVertically(Tbl tbl, int col, int fromRow, int toRow) { if (col < 0 || fromRow < 0 || toRow < 0) { return; } for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) { Tc tc = getTc(tbl, rowIndex, col); if (tc == null) { break; } TcPr tcPr = getTcPr(tc); VMerge vMerge = tcPr.getVMerge(); if (vMerge == null) { vMerge = new VMerge(); tcPr.setVMerge(vMerge); } if (rowIndex == fromRow) { vMerge.setVal("restart"); } else { vMerge.setVal("continue"); } } }
Example #6
Source File: WmlElementUtils.java From docx4j-template with Apache License 2.0 | 6 votes |
/** * @Description: 跨列合并 */ public static void mergeCellsHorizontal(Tbl tbl, int row, int fromCell, int toCell) { if (row < 0 || fromCell < 0 || toCell < 0) { return; } List<Tr> trList = getTblAllTr(tbl); if (row > trList.size()) { return; } Tr tr = trList.get(row); List<Tc> tcList = getTrAllCell(tr); for (int cellIndex = fromCell, len = Math.min(tcList.size() - 1, toCell); cellIndex <= len; cellIndex++) { Tc tc = tcList.get(cellIndex); TcPr tcPr = getTcPr(tc); HMerge hMerge = tcPr.getHMerge(); if (hMerge == null) { hMerge = new HMerge(); tcPr.setHMerge(hMerge); } if (cellIndex == fromCell) { hMerge.setVal("restart"); } else { hMerge.setVal("continue"); } } }
Example #7
Source File: WmlElementUtils.java From docx4j-template with Apache License 2.0 | 6 votes |
/** * @Description:得到所有表格 */ public static List<Tbl> getAllTbl(WordprocessingMLPackage wordMLPackage) { MainDocumentPart mainDocPart = wordMLPackage.getMainDocumentPart(); List<Object> objList = getAllElementFromObject(mainDocPart, Tbl.class); if (objList == null) { return null; } List<Tbl> tblList = new ArrayList<Tbl>(); for (Object obj : objList) { if (obj instanceof Tbl) { Tbl tbl = (Tbl) obj; tblList.add(tbl); } } return tblList; }
Example #8
Source File: WmlElementUtils.java From docx4j-template with Apache License 2.0 | 6 votes |
/** * @Description:删除指定位置的表格,删除后表格数量减一 */ public static boolean removeTableByIndex(WordprocessingMLPackage wordMLPackage, int index) throws Exception { boolean flag = false; if (index < 0) { return flag; } List<Object> objList = wordMLPackage.getMainDocumentPart().getContent(); if (objList == null) { return flag; } int k = -1; for (int i = 0, len = objList.size(); i < len; i++) { Object obj = XmlUtils.unwrap(objList.get(i)); if (obj instanceof Tbl) { k++; if (k == index) { wordMLPackage.getMainDocumentPart().getContent().remove(i); flag = true; break; } } } return flag; }
Example #9
Source File: WmlElementUtils.java From docx4j-template with Apache License 2.0 | 6 votes |
/** * @Description: 得到表格所有的行 */ public static List<Tr> getTblAllTr(Tbl tbl) { List<Object> objList = getAllElementFromObject(tbl, Tr.class); List<Tr> trList = new ArrayList<Tr>(); if (objList == null) { return trList; } for (Object obj : objList) { if (obj instanceof Tr) { Tr tr = (Tr) obj; trList.add(tr); } } return trList; }
Example #10
Source File: WmlElementUtils.java From docx4j-template with Apache License 2.0 | 6 votes |
/** * @Description: 删除指定行 删除后行数减一 */ public static boolean removeTrByIndex(Tbl tbl, int index) { boolean flag = false; if (index < 0) { return flag; } List<Object> objList = tbl.getContent(); if (objList == null) { return flag; } int k = -1; for (int i = 0, len = objList.size(); i < len; i++) { Object obj = XmlUtils.unwrap(objList.get(i)); if (obj instanceof Tr) { k++; if (k == index) { tbl.getContent().remove(i); flag = true; break; } } } return flag; }
Example #11
Source File: ConditionalDisplayOfParagraphsTest.java From docx-stamper with MIT License | 6 votes |
private void paragraphsInNestedTablesAreRemoved(WordprocessingMLPackage document) { final List<Tbl> tables = new ArrayList<>(); CoordinatesWalker walker = new BaseCoordinatesWalker(document) { @Override protected void onTable(TableCoordinates tableCoordinates) { tables.add(tableCoordinates.getTable()); } }; walker.walk(); Tbl nestedTable = tables.get(1); Tc cell = (Tc) ((JAXBElement) ((Tr) nestedTable.getContent().get(1)).getContent().get(0)).getValue(); P p1 = (P) cell.getContent().get(0); Assert.assertEquals(1, cell.getContent().size()); Assert.assertEquals("This paragraph stays untouched.", new ParagraphWrapper(p1).getText()); }
Example #12
Source File: Docx4j_工具类_S3_Test.java From docx4j-template with Apache License 2.0 | 6 votes |
/** * @Description: 跨行合并 */ public void mergeCellsVertically(Tbl tbl, int col, int fromRow, int toRow) { if (col < 0 || fromRow < 0 || toRow < 0) { return; } for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) { Tc tc = getTc(tbl, rowIndex, col); if (tc == null) { break; } TcPr tcPr = getTcPr(tc); VMerge vMerge = tcPr.getVMerge(); if (vMerge == null) { vMerge = new VMerge(); tcPr.setVMerge(vMerge); } if (rowIndex == fromRow) { vMerge.setVal("restart"); } else { vMerge.setVal("continue"); } } }
Example #13
Source File: Docx4j_工具类_S3_Test.java From docx4j-template with Apache License 2.0 | 6 votes |
/** * @Description:得到所有表格 */ public List<Tbl> getAllTbl(WordprocessingMLPackage wordMLPackage) { MainDocumentPart mainDocPart = wordMLPackage.getMainDocumentPart(); List<Object> objList = getAllElementFromObject(mainDocPart, Tbl.class); if (objList == null) { return null; } List<Tbl> tblList = new ArrayList<Tbl>(); for (Object obj : objList) { if (obj instanceof Tbl) { Tbl tbl = (Tbl) obj; tblList.add(tbl); } } return tblList; }
Example #14
Source File: Docx4j_替换模板.java From docx4j-template with Apache License 2.0 | 6 votes |
/** * @Description: 跨行合并 */ public static void mergeCellsVertically(Tbl tbl, int col, int fromRow, int toRow) { if (col < 0 || fromRow < 0 || toRow < 0) { return; } for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) { Tc tc = getTc(tbl, rowIndex, col); if (tc == null) { break; } TcPr tcPr = getTcPr(tc); VMerge vMerge = tcPr.getVMerge(); if (vMerge == null) { vMerge = factory.createTcPrInnerVMerge(); tcPr.setVMerge(vMerge); } if (rowIndex == fromRow) { vMerge.setVal("restart"); } else { vMerge.setVal("continue"); } } }
Example #15
Source File: Docx4J_简单例子.java From docx4j-template with Apache License 2.0 | 6 votes |
public void addBorders(Tbl table, String borderSize) { table.setTblPr(new TblPr()); CTBorder border = new CTBorder(); border.setColor("auto"); border.setSz(new BigInteger(borderSize)); border.setSpace(new BigInteger("0")); border.setVal(STBorder.SINGLE); TblBorders borders = new TblBorders(); borders.setBottom(border); borders.setLeft(border); borders.setRight(border); borders.setTop(border); borders.setInsideH(border); borders.setInsideV(border); table.getTblPr().setTblBorders(borders); }
Example #16
Source File: Docx4j_合并单元格_S4_Test.java From docx4j-template with Apache License 2.0 | 6 votes |
/** * @Description: 跨行合并 */ public void mergeCellsVertically(Tbl tbl, int col, int fromRow, int toRow) { if (col < 0 || fromRow < 0 || toRow < 0) { return; } for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) { Tc tc = getTc(tbl, rowIndex, col); if (tc == null) { break; } TcPr tcPr = getTcPr(tc); VMerge vMerge = tcPr.getVMerge(); if (vMerge == null) { vMerge = new VMerge(); tcPr.setVMerge(vMerge); } if (rowIndex == fromRow) { vMerge.setVal("restart"); } else { vMerge.setVal("continue"); } } }
Example #17
Source File: TableWithMergedCells.java From docx4j-template with Apache License 2.0 | 6 votes |
/** * 创建一个带边框的表格并添加四个带内容的行, 然后将表格添加到文档并保存 */ public static void main (String[] args) throws Docx4JException { wordMLPackage = WordprocessingMLPackage.createPackage(); factory = Context.getWmlObjectFactory(); Tbl table = factory.createTbl(); addBorders(table); addTableRowWithMergedCells("Heading 1", "Heading 1.1", "Field 1", table); addTableRowWithMergedCells(null, "Heading 1.2", "Field 2", table); addTableRowWithMergedCells("Heading 2", "Heading 2.1", "Field 3", table); addTableRowWithMergedCells(null, "Heading 2.2", "Field 4", table); wordMLPackage.getMainDocumentPart().addObject(table); wordMLPackage.save(new java.io.File( "src/main/files/HelloWord9.docx") ); }
Example #18
Source File: Docx4j_工具类_S3_Test.java From docx4j-template with Apache License 2.0 | 6 votes |
/** * @Description: 得到表格所有的行 */ public List<Tr> getTblAllTr(Tbl tbl) { List<Object> objList = getAllElementFromObject(tbl, Tr.class); List<Tr> trList = new ArrayList<Tr>(); if (objList == null) { return trList; } for (Object obj : objList) { if (obj instanceof Tr) { Tr tr = (Tr) obj; trList.add(tr); } } return trList; }
Example #19
Source File: SettingColumnWidthForTable.java From docx4j-template with Apache License 2.0 | 6 votes |
/** * 本方法为表格添加边框 */ private static void addBorders(Tbl table) { table.setTblPr(new TblPr()); CTBorder border = new CTBorder(); border.setColor("auto"); border.setSz(new BigInteger("4")); border.setSpace(new BigInteger("0")); border.setVal(STBorder.SINGLE); TblBorders borders = new TblBorders(); borders.setBottom(border); borders.setLeft(border); borders.setRight(border); borders.setTop(border); borders.setInsideH(border); borders.setInsideV(border); table.getTblPr().setTblBorders(borders); }
Example #20
Source File: Docx4J_例子2.java From docx4j-template with Apache License 2.0 | 6 votes |
/** * @param tableWidthPercent * 表格占页面宽度百分比 * @param widthPercent * 各列百分比 */ public void setTableGridCol(WordprocessingMLPackage wordPackage, ObjectFactory factory, Tbl table, double tableWidthPercent, double[] widthPercent) throws Exception { int width = getWritableWidth(wordPackage); int tableWidth = (int) (width * tableWidthPercent / 100); TblGrid tblGrid = factory.createTblGrid(); for (int i = 0; i < widthPercent.length; i++) { TblGridCol gridCol = factory.createTblGridCol(); gridCol.setW(BigInteger.valueOf((long) (tableWidth * widthPercent[i] / 100))); tblGrid.getGridCol().add(gridCol); } table.setTblGrid(tblGrid); TblPr tblPr = table.getTblPr(); if (tblPr == null) { tblPr = factory.createTblPr(); } TblWidth tblWidth = new TblWidth(); tblWidth.setType("dxa");// 这一行是必须的,不自己设置宽度默认是auto tblWidth.setW(new BigInteger(tableWidth + "")); tblPr.setTblW(tblWidth); table.setTblPr(tblPr); }
Example #21
Source File: TableWithBorders.java From docx4j-template with Apache License 2.0 | 6 votes |
private static void addBorders(Tbl table) { table.setTblPr(new TblPr()); CTBorder border = new CTBorder(); border.setColor("auto"); border.setSz(new BigInteger("4")); border.setSpace(new BigInteger("0")); border.setVal(STBorder.SINGLE); TblBorders borders = new TblBorders(); borders.setBottom(border); borders.setLeft(border); borders.setRight(border); borders.setTop(border); borders.setInsideH(border); borders.setInsideV(border); table.getTblPr().setTblBorders(borders); }
Example #22
Source File: Docx4J_简单例子2.java From docx4j-template with Apache License 2.0 | 6 votes |
public void addBorders(Tbl table, String borderSize) { table.setTblPr(new TblPr()); CTBorder border = new CTBorder(); border.setColor("auto"); border.setSz(new BigInteger(borderSize)); border.setSpace(new BigInteger("0")); border.setVal(STBorder.SINGLE); TblBorders borders = new TblBorders(); borders.setBottom(border); borders.setLeft(border); borders.setRight(border); borders.setTop(border); borders.setInsideH(border); borders.setInsideV(border); table.getTblPr().setTblBorders(borders); }
Example #23
Source File: WmlElementUtils.java From docx4j-template with Apache License 2.0 | 5 votes |
/** * @Description: 设置单元格Margin */ public void setTableCellMargin(Tbl tbl, String top, String right, String bottom, String left) { TblPr tblPr = getTblPr(tbl); CTTblCellMar cellMar = tblPr.getTblCellMar(); if (cellMar == null) { cellMar = new CTTblCellMar(); tblPr.setTblCellMar(cellMar); } if (StringUtils.isNotBlank(top)) { TblWidth topW = new TblWidth(); topW.setW(new BigInteger(top)); topW.setType("dxa"); cellMar.setTop(topW); } if (StringUtils.isNotBlank(right)) { TblWidth rightW = new TblWidth(); rightW.setW(new BigInteger(right)); rightW.setType("dxa"); cellMar.setRight(rightW); } if (StringUtils.isNotBlank(bottom)) { TblWidth btW = new TblWidth(); btW.setW(new BigInteger(bottom)); btW.setType("dxa"); cellMar.setBottom(btW); } if (StringUtils.isNotBlank(left)) { TblWidth leftW = new TblWidth(); leftW.setW(new BigInteger(left)); leftW.setType("dxa"); cellMar.setLeft(leftW); } }
Example #24
Source File: Docx4j_工具类_S3_Test.java From docx4j-template with Apache License 2.0 | 5 votes |
/** * @Description: 设置表格垂直对齐方式(包括单元格),只对该方法前面产生的单元格起作用 */ public void setTblAllVAlign(Tbl tbl, STVerticalJc vAlignType) { if (vAlignType != null) { List<Tr> trList = getTblAllTr(tbl); for (Tr tr : trList) { List<Tc> tcList = getTrAllCell(tr); for (Tc tc : tcList) { setTcVAlign(tc, vAlignType); } } } }
Example #25
Source File: Docx4j_工具类_S3_Test.java From docx4j-template with Apache License 2.0 | 5 votes |
/** * @Description: 设置表格水平对齐方式(包括单元格),只对该方法前面产生的单元格起作用 */ public void setTblAllJcAlign(Tbl tbl, JcEnumeration jcType) { if (jcType != null) { setTblJcAlign(tbl, jcType); List<Tr> trList = getTblAllTr(tbl); for (Tr tr : trList) { List<Tc> tcList = getTrAllCell(tr); for (Tc tc : tcList) { setTcJcAlign(tc, jcType); } } } }
Example #26
Source File: ExpressionReplacementInTablesTest.java From docx-stamper with MIT License | 5 votes |
private void resolvedExpressionsAreReplacedInFirstLevelTable(WordprocessingMLPackage document) { Tbl table = (Tbl) ((JAXBElement) document.getMainDocumentPart().getContent().get(1)).getValue(); Tr row = (Tr) table.getContent().get(0); Tc cell = (Tc) ((JAXBElement) row.getContent().get(1)).getValue(); P nameParagraph = (P) cell.getContent().get(0); Assert.assertEquals("Bart Simpson", new ParagraphWrapper(nameParagraph).getText()); }
Example #27
Source File: WmlElementUtils.java From docx4j-template with Apache License 2.0 | 5 votes |
/** * @Description: 获取表格内容 */ public static List<String> getTblContentList(Tbl tbl) throws Exception { List<String> resultList = new ArrayList<String>(); List<Tr> trList = getTblAllTr(tbl); for (Tr tr : trList) { StringBuffer sb = new StringBuffer(); List<Tc> tcList = getTrAllCell(tr); for (Tc tc : tcList) { sb.append(getElementContent(tc) + ","); } resultList.add(sb.toString()); } return resultList; }
Example #28
Source File: Docx4j_合并单元格_S4_Test.java From docx4j-template with Apache License 2.0 | 5 votes |
public void mergeCellsHorizontalByGridSpan(Tbl tbl, int row, int fromCell, int toCell) { if (row < 0 || fromCell < 0 || toCell < 0) { return; } List<Tr> trList = getTblAllTr(tbl); if (row > trList.size()) { return; } Tr tr = trList.get(row); List<Tc> tcList = getTrAllCell(tr); for (int cellIndex = Math.min(tcList.size() - 1, toCell); cellIndex >= fromCell; cellIndex--) { Tc tc = tcList.get(cellIndex); TcPr tcPr = getTcPr(tc); if (cellIndex == fromCell) { GridSpan gridSpan = tcPr.getGridSpan(); if (gridSpan == null) { gridSpan = new GridSpan(); tcPr.setGridSpan(gridSpan); } gridSpan.setVal(BigInteger.valueOf(Math.min(tcList.size() - 1, toCell) - fromCell + 1)); } else { tr.getContent().remove(cellIndex); } } }
Example #29
Source File: TableWithBorders.java From docx4j-template with Apache License 2.0 | 5 votes |
private static Tbl createTableWithContent() { Tbl table = factory.createTbl(); Tr tableRow = factory.createTr(); addTableCell(tableRow, "Field 1"); addTableCell(tableRow, "Field 2"); table.getContent().add(tableRow); return table; }
Example #30
Source File: ExpressionReplacementInTablesTest.java From docx-stamper with MIT License | 5 votes |
private void unresolvedExpressionsAreNotReplacedInNestedTable(WordprocessingMLPackage document) { Tbl table = (Tbl) ((JAXBElement) document.getMainDocumentPart().getContent().get(1)).getValue(); Tr row = (Tr) table.getContent().get(2); Tc cell = (Tc) ((JAXBElement) row.getContent().get(0)).getValue(); Tbl nestedTable = (Tbl) ((JAXBElement) cell.getContent().get(1)).getValue(); Tr nestedRow = (Tr) nestedTable.getContent().get(1); Tc nestedCell = (Tc) ((JAXBElement) nestedRow.getContent().get(1)).getValue(); P nameParagraph = (P) nestedCell.getContent().get(0); Assert.assertEquals("${foo}", new ParagraphWrapper(nameParagraph).getText()); }