Page 1 of 1

Content controls in header/footer

PostPosted: Thu Jan 13, 2011 5:57 am
by kjetilhp
Hi, I'm trying to use content controls in header and footer using the code from CustomXmlBinding, but the controls placed in the header aren't affected by the binding process... if I place a control bound to the same data inside the main part it works so nothing wrong with the code... does these parts of the document need special handling?

/K

Re: Content controls in header/footer

PostPosted: Thu Jan 13, 2011 9:32 pm
by kjetilhp
looking at the code
Code: Select all
customXmlDataStoragePart.applyBindings(wordMLPackage.getMainDocumentPart());


it seems that you are only binding on the main part, not the header... shouldn't this binder also take HeaderPart/FooterPart?

Re: Content controls in header/footer

PostPosted: Thu Jan 13, 2011 9:33 pm
by jason
With http://dev.plutext.org/trac/docx4j/changeset/1389 you should be able to call BindingHandler.applyBindings passing it a header or footer part.

I haven't made a method to apply it to each header/footer, but that would be straightforward.

Re: Content controls in header/footer

PostPosted: Thu Jan 13, 2011 9:42 pm
by kjetilhp
nice! thx..

btw, have a couple of questions regarding the opendope word add-in and the plutext-server, is it ok to contact you by email?

Re: Content controls in header/footer

PostPosted: Thu Jan 13, 2011 9:46 pm
by jason
sure; anytime. I did just reply to your plutext-server post in the docx4all forum...

Re: Content controls in header/footer

PostPosted: Thu Jan 13, 2011 11:50 pm
by kjetilhp
mail sent :)

did an svn update and tried to apply bindings to header also, doesn't seem to work... only the main document are affected,

I've used the HeaderList example for fetching the header...
Code: Select all
List<SectionWrapper> sectionWrappers = wordMLPackage.getDocumentModel().getSections();
HeaderPart header = null;
      for (SectionWrapper sw : sectionWrappers) {
         HeaderFooterPolicy hfp = sw.getHeaderFooterPolicy();
         if (hfp.getDefaultHeader()!=null) {
                            header = hfp.getDefaultHeader();
                        }
      }
CustomXmlDataStoragePart customXmlDataStoragePart = wordMLPackage.getCustomXmlDataStorageParts().get(itemId_01);
CustomXmlDataStorage customXmlDataStorage = customXmlDataStoragePart.getData();
((CustomXmlDataStorageImpl)customXmlDataStorage).
                setNodeValueAtXPath("/ns0:root[1]/ns0:document[1]/ns0:doknr[1]",
                "1",
               "xmlns:ns0='http://schemas.root'");

((CustomXmlDataStorageImpl)customXmlDataStorage).
                setNodeValueAtXPath("/ns0:root[1]/ns0:document[1]/ns0:doc.name[1]",
                "My Test Dok",
                "xmlns:ns0='http://schemas.root'");


BindingHandler.applyBindings(wordMLPackage.getMainDocumentPart());
BindingHandler.applyBindings(header);

wordMLPackage.save(new java.io.File(dir + outname) );

Re: Content controls in header/footer

PostPosted: Fri Jan 14, 2011 12:30 am
by jason
Works for me.

I just made a test docx. and then, using the same code as above:

Code: Select all
      List<SectionWrapper> sectionWrappers = wordMLPackage.getDocumentModel()
            .getSections();
      HeaderPart header = null;
      for (SectionWrapper sw : sectionWrappers) {
         HeaderFooterPolicy hfp = sw.getHeaderFooterPolicy();
         if (hfp.getDefaultHeader() != null) {
            header = hfp.getDefaultHeader();
         }
      }

      BindingHandler.applyBindings(header);

      System.out.println(XmlUtils.marshaltoString(header.getJaxbElement(),
            true, true));


Maybe you found the wrong header? or need to check again the binding in the header?

cheers .. Jason

Re: Content controls in header/footer

PostPosted: Fri Jan 14, 2011 7:30 pm
by kjetilhp
it works :) thx

another issue with headers and footers, I'm trying to add an image to the header, put there is no addObject on it as it is in the maindocument...

Re: Content controls in header/footer

PostPosted: Sun Jan 16, 2011 8:23 pm
by jason
Have a look at the AddImage sample. The static method createImagePart in BinaryPartAbstractImage allows you to specify the part (eg header) to which the image should be attached.

Re: Content controls in header/footer

PostPosted: Sun Jan 16, 2011 9:35 pm
by kjetilhp
sorry, wrote the post before looking, have been trying to use the samples but I'm not able to add an image to an existing header, new header works great though.. (want to add an image on top of the existing header)

I think I've asked this before but is there any possibilities of using image content controls with docx4j? seems not to be supported at this time...

Re: Content controls in header/footer

PostPosted: Mon Jan 17, 2011 11:34 am
by jason
kjetilhp wrote:sorry, wrote the post before looking, have been trying to use the samples but I'm not able to add an image to an existing header, new header works great though.. (want to add an image on top of the existing header)


There should be nothing to prevent you adding an image to an existing header; if you need help on this, you'll need to post your code though.

kjetilhp wrote:I think I've asked this before but is there any possibilities of using image content controls with docx4j? seems not to be supported at this time...


Just added in http://dev.plutext.org/trac/docx4j/changeset/1390
Please see the other active thread on this; (for this topic, pls post to that thread or start a new one)

Re: Content controls in header/footer

PostPosted: Tue Apr 26, 2011 9:07 pm
by verrnum
jason wrote:Works for me.

I just made a test docx. and then, using the same code as above:

Code: Select all
      List<SectionWrapper> sectionWrappers = wordMLPackage.getDocumentModel()
            .getSections();
      HeaderPart header = null;
      for (SectionWrapper sw : sectionWrappers) {
         HeaderFooterPolicy hfp = sw.getHeaderFooterPolicy();
         if (hfp.getDefaultHeader() != null) {
            header = hfp.getDefaultHeader();
         }
      }

      BindingHandler.applyBindings(header);

      System.out.println(XmlUtils.marshaltoString(header.getJaxbElement(),
            true, true));


cheers .. Jason



Hi,

How to retrieve the docx4j (binary download) version with "BindingHandler.applyBindings" appliable to header / footer ?

I use this one : http://dev.plutext.org/docx4j/docx4j-2. ... -2.6.0.jar and the method is the old one.

Best Regards

Re: Content controls in header/footer

PostPosted: Tue Apr 26, 2011 9:20 pm
by verrnum
Hi,

Additinal question :

Is exists another way to apply binding method in header / footer with upgrading package version ?

Best Regards

Re: Content controls in header/footer

PostPosted: Thu Apr 28, 2011 1:30 am
by jason
I take it these questions aren't of interest to you now now that you are using a recent post 2.6.0 nightly, right?

Re: Content controls in header/footer

PostPosted: Fri May 20, 2011 5:02 am
by tinne
Unfortunately, the whole discussion ignores the fact that Preprocessing is also only being done on the main document body, i.e. conditions and repeats are not possible in headers and footers.

I've started to take care for this, but
  • it's still just a hack and
  • iwriting back patched headers does not work due to a marshaller exception, 'unable to marshal type "org.docx4j.wml.FldChar" as an element because it is missing an @XmlRootElement annotation'

headerfootermods1.zip
After a short try with subclassing I came to patch OpenDoPEHandler and TraversalUtil, find attached the modified versions.
(14.64 KiB) Downloaded 375 times


Anybody got an idea, what I'm missing?

Re: Content controls in header/footer

PostPosted: Fri May 20, 2011 4:30 pm
by jason
Hi Tinne

Thanks for raising this issue, and proposing a patch.

I've implemented your approach, but using slightly different API calls (some of which just added). See principally http://dev.plutext.org/trac/docx4j/changeset/1506

It seems to work OK on the invoice sample, but needs more testing on documents containing components (both in the main document part, and in a header or footer).

cheers .. Jason
ps I also added the annotation to address the FldChar error you were getting.

Re: Content controls in header/footer

PostPosted: Sat May 21, 2011 12:06 am
by tinne
Cheers, great solution.

After marking CTMarkupRange an @XmlRootElement as well, my test document was preprocessed perfectly.

I've tried to steal OpenDoPEHandler.getParts for the calls to BindingHandler.applyBindings as well but of course had to cast the results to JaxbXmlPart<?> to make it work. Maybe enhancing ContentAccessor to getPackage/getElement/setJaxBElement would be a thought, say, after renaming it to DocumentFragmentAccessor or the like.