Page 1 of 1

UnmarshallFromTemplate cannot replace Header's Text

PostPosted: Sun May 08, 2011 2:26 am
by yugikhoi
Hi,

I am new to this forum and very impressive with all docx4j work. I tried to replace text in MSWord document by UnmarshallFromTemplate.java (887), everything is just perfect. However, when I add a header and put a variable there, it is not working. I think it got problems with getting Relatioship Part or something like that but don't know how to fix (I keep exactly sample code and your docx file). Can anyone have exp. help me with your coding? Thanks a lot.

Yugi

Re: UnmarshallFromTemplate cannot replace Header's Text

PostPosted: Mon May 09, 2011 10:49 am
by jason
You're right, the sample doesn't do that.

It applies the pattern

Code: Select all
         String xml = XmlUtils.marshaltoString(wmlDocumentEl, true);                 
         Object obj = XmlUtils.unmarshallFromTemplate(xml, mappings);


to the main document part (document.xml) only.

But you can do the same thing to your header and footer parts. You can access them by the wmlPkg's getDocumentModel() method.

Re: UnmarshallFromTemplate cannot replace Header's Text

PostPosted: Mon May 16, 2011 7:49 pm
by yugikhoi
Thanks for your respond however i am just beginner on docx4j so I do not understand how to achieve what your said. Can you give me sample code for that? Thanks.

Re: UnmarshallFromTemplate cannot replace Header's Text

PostPosted: Tue May 17, 2011 7:54 pm
by jason
Which bit are you having trouble understanding?

Perhaps the sample HeaderFooterList will help. The guts of it:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
                List<SectionWrapper> sectionWrappers = wordMLPackage.getDocumentModel().getSections();
               
                for (SectionWrapper sw : sectionWrappers) {
                        HeaderFooterPolicy hfp = sw.getHeaderFooterPolicy();
                       
                        System.out.println("\n\nSECTION  \n");
                       
                        System.out.println("Headers:");
                        if (hfp.getFirstHeader()!=null) System.out.println("-first");
                        if (hfp.getDefaultHeader()!=null) System.out.println("-default");
                        if (hfp.getEvenHeader()!=null) System.out.println("-even");
                       
                        System.out.println("\nFooters:");
                        if (hfp.getFirstFooter()!=null) System.out.println("-first");
                        if (hfp.getDefaultFooter()!=null) System.out.println("-default");
                        if (hfp.getEvenFooter()!=null) System.out.println("-even");
                       
                }
 
Parsed in 0.015 seconds, using GeSHi 1.0.8.4


From the HeaderFooterPolicy object, you can get the relevant HeaderPart or FooterPart:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting

        public HeaderPart getFirstHeader() {
                return firstHeaderActive;
        }
        public FooterPart getFirstFooter() {
                return firstFooterActive;
        }
        /**
         * Returns the odd page header. This is
         *  also the same as the default one...
         */

        public HeaderPart getOddHeader() {
                return defaultHeader;
        }
        /**
         * Returns the odd page footer. This is
         *  also the same as the default one...
         */

        public FooterPart getOddFooter() {
                return defaultFooter;
        }
        public HeaderPart getEvenHeader() {
                return evenHeader;
        }
        public FooterPart getEvenFooter() {
                return evenFooter;
        }
        public HeaderPart getDefaultHeader() {
                return defaultHeader;
        }
        public FooterPart getDefaultFooter() {
                return defaultFooter;
        }
 
Parsed in 0.014 seconds, using GeSHi 1.0.8.4

Re: UnmarshallFromTemplate cannot replace Header's Text

PostPosted: Sat May 21, 2011 11:05 pm
by kgoroway
I'm trying to do the exact same thing...

But when I find my header, and try to marshal it to a string I get this exception:
Code: Select all
        javax.xml.bind.JAXBException: class org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart nor any of its super class is known to this context.
   at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getBeanInfo(JAXBContextImpl.java:540)
   at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:460)
   at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:301)
   at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:230)
   at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:96)
   at org.docx4j.XmlUtils.marshaltoString(XmlUtils.java:406)
   at org.docx4j.XmlUtils.marshaltoString(XmlUtils.java:336)


The relevant code looks like this:
Code: Select all

         for (SectionWrapper sw : sectionWrappers) {
               HeaderFooterPolicy hfp = sw.getHeaderFooterPolicy();
               if (hfp != null) {
                  HeaderPart headerPart = hfp.getDefaultHeader();
                  if (headerPart != null) {
                     //xml --> string
                     xml = XmlUtils.marshaltoString(headerPart, true);      


Any clues?

Thanks!
-Kevin

Re: UnmarshallFromTemplate cannot replace Header's Text

PostPosted: Sun May 22, 2011 1:27 am
by jason
At a glance, looks like you need to call the getJAXBElement() method in that last line:

Code: Select all
xml = XmlUtils.marshaltoString(headerPart.getJAXBElement(), true);   

Re: UnmarshallFromTemplate cannot replace Header's Text

PostPosted: Sun May 22, 2011 4:16 am
by kgoroway
Yes, of course...thanks for spotting that!