Page 1 of 1

Multiple Sections and Headers

PostPosted: Thu Mar 03, 2016 8:07 am
by garyecoop
I have been trying to do multiple headers and it looks like with Word you need multiple sections. Looks like I have created the section break for next page and when I look at the XML I see both my headers & footers and the rid# referenced in the document.xml but it is not showing correctly If I open in Word and turn on markup I see that there is a Header 1 and Header 2 and the section break but I have to click on "Link to Previous" to turn off and then I can modify the sections 2 header and they stay independent of each other.

Question is how do I disable the "Link to Previous" using the API?



Thanks

Re: Multiple Sections and Headers

PostPosted: Thu Mar 03, 2016 10:57 am
by jason
If all you want is odd/even headers perhaps with a title page, you don't need multiple sections.

If you want to change what you have at some point in the document, then you need multiple sections, since the headers for a particular section are given in the sectPr which follows.

There is no "Link to Previous" setting as such. Headers are inherited from the previous section, unless changed. For more detail, see https://github.com/plutext/docx4j/blob/ ... y.java#L65

In the example xml you gave, you have default header in each section, which is simple.

IIRC though, you get unexpected behaviour in Word if your body starts with sectPr (as in your example). Try putting a paragraph or 2 at the start to exclude this.

Re: Multiple Sections and Headers

PostPosted: Fri Mar 04, 2016 2:39 am
by garyecoop
I need possible the first 3 or 4 pages to have one header and the remaining to have a different headder so First and odd/even wont work.

I create the document using the landscape option which most likely is what is creating the first section. So will probably need to turn that off and modify that setting for the Section itself in order to put a paragraph prior to the SectPr.

So just to understand for myself, the "Link to Previous" that shows up in Word in the menus is not something we can set via the API? So your statement "unless changed" means what exactly? Does it mean using a different header file (Ex: "/word/header2.xml") or the text being changed? I am using two different XMLs for the headers and the rid#'s are different as you can see in the screen shot but not working.

Snap 2016-03-03 at 09.37.52.png
Word docx that is created
Snap 2016-03-03 at 09.37.52.png (27.38 KiB) Viewed 2894 times

Re: Multiple Sections and Headers

PostPosted: Fri Mar 04, 2016 11:04 pm
by jason
The easiest approach is to create a docx in Word with the headers as you'd like them, then simply mimic the resulting XML in your code.

The docx4j webapp and Helper AddIn can both generate the code for you, once you have your example docx.

If you are having trouble creating an example docx using the Word UI, feel free to post whatever you've managed, plus a description of what needs to change.