Page 1 of 1

Header dificulties

PostPosted: Tue Dec 07, 2010 10:15 pm
by Mesni
Helo Jason.

I have a problem with the headers in my document.
If someone creates a document and adds a headers ( of first page its empty ).
The pages are spread into sections.
On the second page the person adds a header and clicks the "Different first page" inside the Word.
On the next page he doesn't click it, and so on...

The template i created is in the attachment.

So the problem occurs here:
If I unzip the docx file, inside i find that Word creates tree files "Header1.xml", "Header2.xml", "Header3.xml".
How do i access these three files?

I am going over the sections, getting the headers, default, first, odd, and going over the numbers... noting helps.
Is that even possible to access these tree header files (or even more)?

If there is any way i would be very grateful for your answer, if its not than i would have to find an other way.

Currently i am using the docx4j-2.5.0

Thank you very much for your time,

Lp, Mesni

Re: Header dificulties

PostPosted: Tue Dec 07, 2010 11:02 pm
by jason
WordprocessingMLPackage contains:

Code: Select all
   public DocumentModel getDocumentModel()



DocumentModel contains:

Code: Select all
   public List<SectionWrapper> getSections()


A SectionWrapper contains:

Code: Select all
   public PageDimensions getPageDimensions()

   public HeaderFooterPolicy getHeaderFooterPolicy()

   public SectPr getSectPr()


HeaderFooterPolicy and PageDimensions provide easy access to stuff in the original SectPr

See http://dev.plutext.org/svn/docx4j/trunk ... olicy.java

This is the most user friendly way to access them.

Of course you can also access them via the main document part's relationships, or directly (if you know the name of the part you are after).

hope this helps. .. Jason

ps i suggest you upgrade to 2.6.0. the upgrade should be painless.

Re: Header dificulties

PostPosted: Tue Dec 07, 2010 11:42 pm
by Mesni
Ok i have checked all the options already and the problem might be here:

The document has sections and one section can have different headers. You can accomplish this by selecting the checbox "Different first page" (show in a picture that is attached).

The problem this little box creates is that a unique header is created that isn't directly bound to the section, so that makes it very hard accessible over the docx4j. I checked your code and didn't find any code that try's to find these parts. For now i will have to unzip the file and edit the headers by hand. (this problem is made by the word and the stupidity of some persons that copy paste too much from o document to other).

And thank you for the code you gave me. I already tried all of these attempts and none of them could access these headers.
ex.:

Code: Select all
HeaderPart header = wordMLPackage.getHeaderFooterPolicy().getDefaultHeader();
header = wordMLPackage.getHeaderFooterPolicy().getFirstHeader();
header = wordMLPackage.getHeaderFooterPolicy().getEvenHeader();
header = wordMLPackage.getHeaderFooterPolicy().getOddHeader();

NOTE: all this function have been called on all the sections too but only the default headers have been found.

Code: Select all
List<SectionWrapper> sections  = wordMLPackage.getDocumentModel().getSections();
            for(SectionWrapper section : sections )
            { ...


Sorry for any problems i have caused and thank you very much for your time.

Lp, Mesni

Re: Header dificulties

PostPosted: Wed Dec 08, 2010 12:04 am
by jason
Code: Select all
public HeaderPart getFirstHeader(


should return the first page header. If it doesn't, there is a bug (and a simple test case would help to fix it).

As the code notes, there may be an issue if "same as previous" is set. When "same as previous" is set, the sectPr Word emits contains no w:headerReference at all. Absence of such implies settings are inherited from the previous sectPr. TODO: ensure the code reflects this.

(If you say you want no header in a sectPr, Word inserts something like:

Code: Select all
    <w:sectPr>
      <w:headerReference w:type="default" r:id="rId8"/>


with that w:hdr containing an empty paragraph.)

Re: Header dificulties

PostPosted: Wed Dec 08, 2010 12:34 am
by Mesni
So if i understand you correctly you search for headers inside the main document. And if there the reference is not set you cant find the header?

Sorry i don't fully understand what you meant. Tel me how i can help you and i will do my best. The main problem is that the templates are already created and i am not allowed to alter them.

Re: Header dificulties

PostPosted: Fri Dec 10, 2010 8:02 pm
by jason
I've just spent a bit of time exploring the interaction between first page header and "same as previous".

If you open the attached docx (renamed as zip) in Word 2007, you'll see that the *2nd* section has a different first page.

However, looking at the WordML, this different second section appears in the first sectPr.
Code: Select all

  <w:body>
    <w:p>
      <w:r>
        <w:t>Hello</w:t>
      </w:r>
    </w:p>
    <w:p>
      <w:r>
        <w:br w:type="page"/>
      </w:r>
    </w:p>
    <w:p/>
    <w:p/>
    <w:p>
      <w:pPr>
        <w:sectPr>
          <w:headerReference w:type="default" r:id="rId7"/>
          <w:footerReference w:type="default" r:id="rId8"/>
          <w:headerReference w:type="first" r:id="rId9"/> <!-- note that this in the first sectPr -->
          :
        </w:sectPr>
      </w:pPr>
    </w:p>
    <w:p>
      <w:r>
        <w:lastRenderedPageBreak/>
        <w:t>2nd section</w:t>
      </w:r>
    </w:p>
    <w:p/>
    <w:p>
      <w:r>
        <w:br w:type="page"/>
      </w:r>
    </w:p>
    <w:p>
      <w:r>
        <w:lastRenderedPageBreak/>
        <w:t>s2 p2</w:t>
      </w:r>
    </w:p>
    <w:p/>
    <w:p/>
    <w:p>
      <w:pPr>
        <w:sectPr>
          <!-- no headerReference implies same as previous -->
          <w:footerReference w:type="default" r:id="rId10"/>
          <w:titlePg/> <!-- this specifies that any first page header/footer is actually used in this section -->
        </w:sectPr>
      </w:pPr>
    </w:p>



From http://www.documentinteropinitiative.or ... 1eb31.aspx :
[titlePg] specifies whether the parent section in this document shall have a different header and footer for its first page.

If the val attribute is set to true, then the parent section in the document shall use a first page header for the first page in the section. If the val attribute is set to false, then the first page in the parent section shall use the odd page header.

This setting does not affect the presence of even and odd page header on all sections, which is specified using the evenAndOddHeaders element (ยง2.10.1).

If this element is set to false and a first page header is specified , then it shall be ignored and only the odd page header shall be displayed. Conversely, if this element is set to true and the first page header type is omitted for the given section, then a blank header shall be created as needed (another header type shall not be used in its place).

If this element is omitted, then its value shall be assumed to be false.


I'll change docx4j to reflect this over the next day or so.

Re: Header dificulties

PostPosted: Sun Dec 12, 2010 11:59 pm
by jason
Please see http://dev.plutext.org/trac/docx4j/changeset/1352
and let me know if things work better with this. Thanks .. Jason