Hello,
For example I've got an org.docx4j.wml.Tr object is it possible to replace it with a different Tr object created based on a string, say, loaded from data base?
protected java.io.File findXMLPPart(java.io.File file) throws Exception
{
//load the document where we want to add the extracted Row
String inputfilepath2 = "c:/Temp/docx/resulted.docx";
WordprocessingMLPackage wordMLPackage2 = WordprocessingMLPackage.load(new java.io.File(inputfilepath2));
MainDocumentPart documentPart2 = wordMLPackage2.getMainDocumentPart();
//check if we have any styles part in the document
StyleDefinitionsPart styleDefReturned = documentPart2.getStyleDefinitionsPart();
if (styleDefReturned == null)
{
//create one if we don't have
styleDefReturned = new StyleDefinitionsPart();
styleDefReturned.unmarshalDefaultStyles();
documentPart2.addTargetPart(styleDefReturned);
}
//check if we have numbering (bullets) part
NumberingDefinitionsPart numberDefReturned = documentPart2.getNumberingDefinitionsPart();
if (numberDefReturned == null)
{
numberDefReturned = new NumberingDefinitionsPart();
documentPart2.addTargetPart(numberDefReturned);
}
//load document from which want to extract the Row
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(file);
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
StyleDefinitionsPart styleDef = documentPart.getStyleDefinitionsPart();
NumberingDefinitionsPart numberDef = documentPart.getNumberingDefinitionsPart();
//replace styles in the document where we want to add extracted Row
styleDefReturned.setJaxbElement(styleDef.getJaxbElement());
//replace numbering in the document where we want to add extracted Row
numberDefReturned.setJaxbElement(numberDef.getJaxbElement());
org.docx4j.wml.Tr tr = findBookmarkedRow("start", documentPart);
org.docx4j.wml.Tr trMoved = XmlUtils.deepCopy(tr);
//find a bookMark where we want to place the extracted Row
org.docx4j.wml.Tbl table = findBookmarkedTable("putt", documentPart2);
table.getContent().set(1, trMoved);
java.io.File temp = java.io.File.createTempFile("tmp_", file.getName());
temp.deleteOnExit();
wordMLPackage2.save(temp);
return temp;
}
private org.docx4j.wml.Tbl findBookmarkedTable(String name, MainDocumentPart documentPart) throws JAXBException
{
final String xpath = "//w:bookmarkStart[@w:name='" + name + "']/../../../..";
List<Object> objects = documentPart.getJAXBNodesViaXPath(xpath, false);
return (org.docx4j.wml.Tbl) XmlUtils.unwrap(objects.get(0));
}
Users browsing this forum: No registered users and 54 guests