Page 1 of 1

Table Row replacement

PostPosted: Sat May 14, 2011 5:15 am
by looker
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?

Re: Table Row replacement

PostPosted: Sun May 15, 2011 3:23 am
by tinne
I'm not quite sure, what you want to do about that table row. You can of course construct a Tr node manually and then replace the reference in the Tbl or whatever contains your Tr object which a new Tr you created programmatically, and perhaps substituting just a few cells or one or the other run, before writing the document back to the original or another file.

In my opinion, you want to check the WordprocessingML-Primer for a construct called Structured Document Tags, which provide (above others) XML document data binding, and read on on opendope.org about an docx4j extension for it. Cf. ISO Standard 29500, Part I, chapters M.1 and 17.5.2.

Karsten

Re: Table Row replacement

PostPosted: Mon May 16, 2011 8:09 pm
by looker
Because data binding of the rich text (w:richText) content controls is not supported (viewtopic.php?f=6&t=723) i am thinking of working "directly" with xml.

In this post Franke gave a solution on how to extract bookmark's surrounding tags (tr, tc, p) which i am thinking to extract and then insert in different docx whenever i need.

Re: Table Row replacement

PostPosted: Mon May 16, 2011 9:06 pm
by looker
And do you know how can I construct Tr node manually? i don't see constructors...

Re: Table Row replacement

PostPosted: Mon May 16, 2011 10:14 pm
by tinne
Well, it's plain JAXB-code, cf. eg. http://jaxb.java.net/tutorial/.

For the right way, try (new org.docx4j.wml.ObjectFactory()).createTr();

(Also note, no visible constructors always means there is an implicit default constructor with visibility public.)

Re: Table Row replacement

PostPosted: Tue May 24, 2011 2:16 am
by looker
I've managed to solve my task with doc4j but not sure if I did it in the right way. Could maybe someone point me what might go wrong?

My task is to be able to extract a text in the Tr and insert it into the other document's table preserving all the styles that this text had. So first I've added a bookmark in the table row and in a bookmark in another document where I want to place the extracted row.

Code: Select all

  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));
  }



The main issue I see here is that I completely replace styles and numberings but I think it's safe as document supposed to be almost identical.