Document doc = new Document(MyDir + "document.docx"); // Retrieve the first table in the document. Table table = (Table)doc.getChild(NodeType.TABLE, 0, true); // Clone the last row in the table. Row clonedRow = (Row)table.getLastRow().deepClone(true); // Remove all content from the cloned row's cells. This makes the row ready for // new content to be inserted into. for (Cell cell: clonedRow.getCells()) { cell.getFirstParagraph().getRuns().clear(); cell.getFirstParagraph().appendChild(new Run(doc,"hello text")); } // Add the row to the end of the table. table.appendChild(clonedRow); doc.save(MyDir + "Table.AddCloneRowToTable Out.doc");
//Load the document Document doc = new Document(MyDir + "in.docx"); //Get the first table in the document Table table = (Table)doc.getChild(NodeType.TABLE, 0, true); //Get the first cell of first row Cell cell = (Cell)table.getRows().get(0).getCells().get(0); //Change the color of text for (Run run : (Iterable<Run>)cell.getChildNodes(NodeType.RUN, true)) { run.getFont().setColor(Color.BLUE); } //Save the document doc.save(MyDir + "Out.docx");
Document doc = new Document(MyDir + "in.docx"); DocumentBuilder builder = new DocumentBuilder(doc); Table table = (Table)doc.getChild(NodeType.TABLE, 0, true); Cell cell = table.getFirstRow().getFirstCell(); builder.moveTo(cell.getFirstParagraph()); FieldBuilder fbuilder = new FieldBuilder(FieldType.FIELD_FORMULA); fbuilder.addArgument("20000").addSwitch("\\# \"#,##0\"").buildAndInsert(builder.getCurrentParagraph()); doc.updateFields(); doc.save(MyDir + "Out.docx");