public Element getChild (String path) { return getChild (path, null); }
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");
public static IU read(File file) throws IOException { FileInputStream is = new FileInputStream(file); try { Document iuDocument = parser.parse(new XMLIOSource(is)); Element root = iuDocument.getChild(UNIT); if (root == null) throw new RuntimeException("No iu found."); IU result = new IU(iuDocument, root); if (result.getId() == null) throw new RuntimeException( String.format("The IU defined in %s is missing an id.", file.getAbsolutePath())); if (result.getVersion() == null) throw new RuntimeException( String.format("The IU defined in %s is missing a version.", file.getAbsolutePath())); return result; } finally { IOUtil.close(is); } }
public static IU read(File file) throws IOException { FileInputStream is = new FileInputStream(file); try { Document iuDocument = parser.parse(new XMLIOSource(is)); Element root = iuDocument.getChild(UNIT); if (root == null) throw new RuntimeException("No iu found."); IU result = new IU(iuDocument, root); if (result.getId() == null) throw new RuntimeException( String.format("The IU defined in %s is missing an id.", file.getAbsolutePath())); if (result.getVersion() == null) throw new RuntimeException( String.format("The IU defined in %s is missing a version.", file.getAbsolutePath())); return result; } finally { IOUtil.close(is); } }
return doc == null ? null : doc.getChild (path, ns);
private boolean mergeDoc(Document from, Document to) { Element rootFrom = from.getRootElement(); String appendPath = rootFrom.getAttributeValue("appendPath"); if (appendPath != null) { return appendPath(rootFrom, to, appendPath); } String replacePath = rootFrom.getAttributeValue("replacePath"); if (replacePath != null) { return replacePath(rootFrom, to, replacePath); } Element rootTo; String mergePath = rootFrom.getAttributeValue("mergePath"); if (mergePath != null) { Element child = to.getChild(mergePath); if (child == null) { throw new IllegalStateException("Cannot merge " + name + " to path " + mergePath); } rootTo = child; } else if (rootFrom.getName().equals(to.getRootElement().getName())) { rootTo = to.getRootElement(); } else { return false; } merge(rootFrom, rootTo); return true; }