Page 1 of 1

Replace text in Header

PostPosted: Tue Feb 01, 2011 2:35 am
by novato
Sorry for my English is very bad.

I'm learning Java and am trying to use your application (docx4j).

I found a code that replaces text in the maindocument and works perfectly with my template. http://muammeryucel.blogspot.com/2011/01/manipulating-microsoft-docx-files-with.html

I do the same but within the headers. How do I apply the method "getJAXBNodesViaXPath" in a header?

A great job
Thanks for everything Jason

Re: Replace text in Header

PostPosted: Wed Feb 02, 2011 6:45 pm
by jason
The getJAXBNodesViaXPath is currently only set up to work with the main document part, not headers/footers etc. The reason for this is that it has limitations (as noted elsewhere); still, if it is to remain available for use with the main document part, I guess there is no reason not to have it for headers/footers as well. I think I'll probably add it.

In the meantime you have at least 5 options:

1. add it to docx4j yourself, by copying the binder field and unmarshal method etc from MainDocumentPart to Header part

2. manually/programmatically find the things you want to change

3. use TraversalUtil (see Getting Started for details)

4. unmarshallFromTemplate

5. custom xml data binding (recommended option)

hope this helps .. Jason

Re: Replace text in Header

PostPosted: Wed Feb 02, 2011 9:19 pm
by novato
Forgive my inexperience.

Would it be possible for you to do a simple example? I would appreciate it very much.

In this example attached, I need to change the text ${name}, ${inf}. Those of Maindocument I could do it myself.

Would you help me with the headers? If not possible I understand.

Thanks anyway.

P.D. Sorry. My English is Google-Translate ;)

Re: Replace text in Header

PostPosted: Wed Feb 02, 2011 9:49 pm
by jason
I've made a nightly build which supports XPath in the header part. You can get it from http://dev.plutext.org/docx4j/docx4j-ni ... 110202.jar

This is how: http://dev.plutext.org/trac/docx4j/changeset/1412

You'll need to get the header part you want to work on. Earlier posts in this forum explain how to do that. See also the sample HeaderFooterList.

If you'd like me to look at your code, please also include it inline in code tags, so I don't have to download, unzip etc. (The zip is also handy though, when there is a sample docx included).

cheers .. Jason