Page 1 of 1

Substituting an image for a text node

PostPosted: Tue Aug 24, 2010 11:23 am
by desau
Hi all,

New to docx4j, new to JAXB.

I want to remove some text and, in that place, add an image.

Looking at the samples and quick start guide, I've managed to open my docx file and find the text node(s) using xpath.

I've got the JAXB text nodes .. I just don't know what to do now.

Should I delete the existing text node and insert a new paragraph node? Should I change the existing text node to be a paragraph node? Not sure how to do either of these.

Thanks for any pointers,
-d

Re: Substituting a paragraph node for a text node

PostPosted: Tue Aug 24, 2010 12:10 pm
by jason
Welcome aboard :-)

If you open a document with an image in it, you'll prob see that your image is a child of w:r
Code: Select all
<w:p>
      <w:r>
        <w:drawing>
        :


so that is what you need to replicate.

If you don't need the existing w:r/w:t, you can delete the w:t and insert instead a w:drawing. You'd delete it from the parent w:r (wml.R)'s runContent:
Code: Select all
    public List<Object> getRunContent()


If you want to keep the w:t, you can add a w:drawing sibling in the w:r.

Or, you can add a new w:p structure.

Have a look at the AddImage sample to see what is involved in adding an image. (In addition to the XML in the document part, you also need to add the image part, and a rel connecting the two. The XML in the image part includes the relId)

Hope this helps.

------------
Did you see the link to the docx4j user survey? Please take the survey, if you haven't already done so. Thanks!

Re: Substituting a paragraph node for a text node

PostPosted: Tue Aug 24, 2010 1:09 pm
by desau
Thanks for the quick reply!

So - this all makes logical sense.

However, I'm having a little trouble working with the tree. So - I've got a JAXBElement. Looking in the Eclipse debugger, I see that the value of that object is the org.docx4j.wml.Text Object.

So far, so good.

So -- I need to go to the parent (the w:r element) and add a w:drawing (and later delete the w:t). However, I don't know how to get to the parent w:r element.

The Text Object has a getParent() method, but that just gives me back the JAXBElement wrapper. From that element, I don't see how to get the parent.

Thanks again!

-d

PS -- yes -- I saw the survey and already filled it out... although I suspect I'd be able to give more useful answers in another week or two when I've got docx4j tamed to my needs :)

Re: Substituting a paragraph node for a text node

PostPosted: Tue Aug 24, 2010 4:28 pm
by jason
desau wrote:The Text Object has a getParent() method, but that just gives me back the JAXBElement wrapper. From that element, I don't see how to get the parent.


From the Getting Started guide:- One annoying thing about JAXB, is that an object – say a table – could be represented as org.docx4j.wml.Tbl (as you would expect). Or it might be wrapped in a javax.xml.bind.JAXBElement, in which case to get the real table, you have to do something like:
Code: Select all
     if ( ((JAXBElement)o).getDeclaredType().getName().equals("org.docx4j.wml.Tbl") )
          org.docx4j.wml.Tbl tbl = (org.docx4j.wml.Tbl)((JAXBElement)o).getValue();


Substitute wml.R where you see wml.Tbl in the above, and you should be right.

desau wrote:PS -- yes -- I saw the survey and already filled it out... although I suspect I'd be able to give more useful answers in another week or two when I've got docx4j tamed to my needs :)


Great, thanks. Please feel free to do it again in a week or two once you have a feel for what you like/dislike etc.

Re: Substituting an image for a text node

PostPosted: Wed Aug 25, 2010 9:04 am
by desau
Perhaps this is an xpath problem.

The type of the JAXBElement parent wrapper isn't a w:r element.. it's the text element. The declared type is "org.docx4j.wml.Text".

My XML looks like this:

Code: Select all
<w:r>
  <w:t>!!TARGET</w:t>
</w:r>


I'm using the getJAXBNodesViaXPath() method to find all text nodes that contain "!!TARGET":

Code: Select all
List<Object> textNodes = documentPart.getJAXBNodesViaXPath("//w:t[contains(text(),'!!TARGET')]", false);

This is working fine -- it's giving me a list of my text nodes which contain that text. However, I just get get to the parent from here.

I've tried various other xpaths:
//w:r/w:t[contains(text(),'!!TARGET')]
//w:t[contains(text(),'!!TARGET')]/parent::w:r
//w:t[contains(text(),'!!TARGET')]/..

None of these give me any objects. :(

Re: Substituting an image for a text node

PostPosted: Wed Aug 25, 2010 9:39 am
by desau
Ah! Yes -- as I suspected, this was an xpath problem

Using this works:

//w:r[w:t[contains(text(),'!!TARGET')]]

I'm moving again -- thanks!