Page 1 of 1

Image and HeaderFooters Handling

PostPosted: Tue Mar 16, 2010 5:52 am
by sirvera
Hi there,

Im new on docx4j.
I been trying to perform a couple of operations with Image and Headers-Footers but Im not able to successed.
The following is what Im trying to do:

1.- Override a text with an Image (jpg)
2.- Override a text what is located in the Header and Footer.

If anyone knows how to do this, I will be glad to get some help. Thanks in advance!

Re: Image and HeaderFooters Handling

PostPosted: Tue Mar 16, 2010 7:57 am
by jason

Re: Image and HeaderFooters Handling

PostPosted: Tue Mar 16, 2010 9:20 pm
by sirvera
Hi,
Thanks for your answer.

Im confuse on how does this work. The example you talk about is create and add an image into the header. What im trying to do is to override a text what is already in the header, I try with unmarshallFromTemplate and Transverse example but it looks like this does not apply for text within the header or footer.

The second issue I have is that from a Word Template I need to process that and insert different JPG in specific places of the document. Every time I try to add an Image goes always to the end of the document, when I try to change the Image using the Transverse example I get an error.

If you have another example or a way to solve please let me know, thanks.

Re: Image and HeaderFooters Handling

PostPosted: Tue Mar 16, 2010 11:01 pm
by jason
Sorry I misread your question.

sirvera wrote: What im trying to do is to override a text what is already in the header, I try with unmarshallFromTemplate and Transverse example but it looks like this does not apply for text within the header or footer.


So you need to get the appropriate header, and then manipulate its contents.

The package has a method getDocumentModel(); you can do something like getDocumentModel().getSections().get(n).getHeaderFooterPolicy().getFirstHeader() to get at the header/footer part you want in section n of the document.

The HeaderPart's jaxbElement is Hdr; your existing text is in List<Object> getEGBlockLevelElts()

From there in its just like manipulating content in the main document part.

sirvera wrote:The second issue I have is that from a Word Template I need to process that and insert different JPG in specific places of the document. Every time I try to add an Image goes always to the end of the document, when I try to change the Image using the Transverse example I get an error.


Well, to insert an image in a specific place in the main document part, you first need to get a reference to that bit of content.

Code: Select all
      MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
      org.docx4j.wml.Document wmlDocumentEl = documentPart.getJaxbElement();
      Body body =  wmlDocumentEl.getBody();
      List <Object> bodyChildren = body.getEGBlockLevelElts();


If the document contained 6 paragraphs, these would be the 6 items in the list bodyChildren.

So if you are inserting an image in the 3rd paragraph,

Code: Select all
P p = bodyChildren.item(2)


That and the AddImage sample should be enough to get you started.

Re: Image and HeaderFooters Handling

PostPosted: Wed Mar 17, 2010 12:35 am
by sirvera
Hi, thanks for the help.

The image is ok, I was able to override the P object on the list and works without problems, thanks for that.

For the header and footer, I was trying to use the code you post but im still not able to get the data from the header or footer, this is the code Im using.

headerPart = wordMLPackage.getDocumentModel().getSections().get(0).getHeaderFooterPolicy().getFirstHeader();

headerPart.getJaxbElement().getEGBlockLevelElts(), here I get the list with just one object (P) but no visible data or no idea how to handled that object. Is this the one I need to look after? if that is the case how can I handled that in order override the data?

As I said before I just need to override some words in every Header and Footer of my document.

Again thanks for your help!

Re: Image and HeaderFooters Handling

PostPosted: Wed Mar 17, 2010 8:24 am
by jason
You're nearly there ... In order to understand what is in the P (paragraph) object, you can use XmlUtils.marshaltoString

A plain vanilla p will contain w:r (run) elements, which in turn contain w:t elements.

You can manipulate the object directly. The runs (R) are in the paragraph's .getParagraphContent(); the text elements are in the run's run.getRunContent(). See OpenMainDocumentAndTraverse sample.

Or you can replace it with another paragraph, created either using the object factory, or perhaps unmarshalString. All this is covered in the Getting Started Guide iirc.