Page 1 of 1

How to replace image with text in table

PostPosted: Mon Aug 29, 2011 2:36 am
by jhkim
Hi all,

I want to replace a embedded image with text(keyword) in table.

I have a docx file.
I read the file and then I want to add a embedded image file in place of keyword.
for instance,
this is docx file.
file.PNG
doxc file
file.PNG (3.94 KiB) Viewed 2802 times

I want to change "today" to "2011-08-28" and "something1" to "some.png" image file. like this,
after.PNG
after replacement
after.PNG (342.75 KiB) Viewed 2802 times


But I don't know how to replace a word.
please help me. If is possible, attach the source cord.

Re: How to replace image with text in table

PostPosted: Mon Aug 29, 2011 10:39 am
by jason
At the highest level, you can choose between 2 approaches.

Approach 1: Do it yourself

On this approach, you search for the keyword, then do the replacement.

You can do the search via TraversalUtil, or via XPath. See Getting Started for more details.

Once you've found the keyword, adding an image is a simple matter (see the AddImage sample).

One problem with this approach is that Word may split your keyword into several elements, making it hard to search for.

Approach 2: Content control binding

On this approach, you use Word to insert a picture content control into your source document, and bind it to a custom xml part. You'll find it easier if you use the OpenDoPE Word Add-In, or the content control toolkit.

At run time, you inject your image into the custom xml part, and then have docx4j resolve the binding (ie place the image into the table). Again, see Gettting Started, and the content control sub-forum, for more on this.

I'd recommend approach 2, since docx4j does most of the work for you, and it supports repeats and conditionals (if you require these).