Page 1 of 1

Heading Style not recognized in Word 2007

PostPosted: Fri Nov 26, 2010 12:41 am
by pandaadb
Hi,

I am trying to create a document and want to use different styles for my headings so I can easily generate a table of contents in the end.

Using following code, I try to generate a paragraph, using the "Heading1" style.

Code: Select all
P paragraph = parent.getMainDocumentPart().addParagraphOfText( text );
        PPr paragraphProperties = objFactory.createPPr();
        PStyle pStyle = new PStyle();
        pStyle.setVal( style.getStyle().getStyleId() );
        paragraphProperties.setPStyle( pStyle );
        paragraph.setPPr( paragraphProperties );


This adds the paragraph and the text to my document.
However, in OpenOffice3 the heading 1 is recognized, the table of contents is there and everything works fine. In Word 2007, the style is simply not recognized by word, so no table of contents and the default style on my text.

I get the style by name from

Code: Select all
StyleDefinitionsPart.getKnownStyles()


Out of that map, I use the style with the Key "Heading1".

Any help is greatly appreciated.

-- artur

Re: Heading Style not recognized in Word 2007

PostPosted: Fri Nov 26, 2010 8:19 am
by jason
When you create a docx from scratch using docx4j, it won't have a styles part until you add one.

The convenience method:

Code: Select all
   public static WordprocessingMLPackage createPackage()


does add some styles:

Code: Select all
      StyleDefinitionsPart stylesPart = new StyleDefinitionsPart();
         stylesPart.unmarshalDefaultStyles();         
         wordDocumentPart.addTargetPart(stylesPart);


These come from "org/docx4j/openpackaging/parts/WordprocessingML/styles.xml"

Thinks of this as a core subset of styles. It includes only: Normal, heading 1-4, Title, Default Paragraph Font, Subtitle, Emphasis, Table Grid

A second set of styles is defined in "org/docx4j/openpackaging/parts/WordprocessingML/KnownStyles.xml"

This list of styles is longer. It is the set of styles known to English language Word 2007.

The styles in KnownStyles.xml exist in:

Code: Select all
   // A variety of pre-defined styles, available for use in a StyleDefinitionsPart.
   private static java.util.Map<String, org.docx4j.wml.Style>  knownStyles;


however a style in KnownStyles.xml is only added to your styles definitions part when you do so explicitly.

So, Heading 1 should be available if you do any of the following 3 things:

1. Use the createPackage() method;
2. stylesPart.unmarshalDefaultStyles(); or
3. use getKnownStyles() to get it, then add the style yourself

hth .. Jason

Re: Heading Style not recognized in Word 2007

PostPosted: Wed Dec 01, 2010 1:43 am
by pandaadb
Hi Jason

Code: Select all
StyleDefinitionsPart stylesPart = new StyleDefinitionsPart();
         stylesPart.unmarshalDefaultStyles();         
         wordDocumentPart.addTargetPart(stylesPart);


Thanks for your fast reply.
This worked perfectly fine - all the styles are recognized. The only thing is, that the styles killed my numbering.
Before, I got the style and then added my own numbering, which seemed to work. It looks like this:

Code: Select all
org.docx4j.wml.Style styleObj = Style.values()[heading].getStyle();
        PPr stylePpr = styleObj.getPPr();
        NumPr numPr = objFactory.createPPrBaseNumPr();
        Ilvl iLvl = new Ilvl();
        iLvl.setVal(BigInteger.valueOf( heading ));
        numPr.setIlvl(iLvl);
        NumId nId = new NumId();
        nId.setVal(BigInteger.valueOf(8));
        numPr.setNumId(nId);
        stylePpr.setNumPr( numPr );


Now, the numbering is gone. I am not really sure where I lost them.
Aren't the default headings (1 through 5) numbered anyway, or do I have to enable it specifically somehow?

Another issue that I saw, and I am not sure why that is, is that the table of contents is not populated automatically.
I use headings and I template of the table of contents in my word document. Now I have two cases:

case 1 is in OpenOffice.org3
The table of contents is automatically updated when I open the document. However, the links to the headings do not work.

case 2 is in MS Word07
Table of contents shows only my template (the template document has a picture and a general introduction which uses two headings. Also, the table of contents is in that template). When I right click on the TOC and refresh it manually, everything is recognized and the links to the headings (CTRL + Click) work just fine.
I figure that there is a way to invoke that refresh on the table of contents programmatically through docx4j but I couldn't find it. Any ideas why I get this behaviour?

Kind regards,
-- artur

Re: Heading Style not recognized in Word 2007

PostPosted: Wed Dec 01, 2010 7:59 am
by jason
pandaadb wrote:Now, the numbering is gone. I am not really sure where I lost them.
Aren't the default headings (1 through 5) numbered anyway, or do I have to enable it specifically somehow?


In Word's default styles, headings are not numbered, so you do have to enable it, by adding a numbering part (see recent post entitled 'bullets' for more on this), and by adding a reference to the list to your style. As always, the easiest way to see what you need to do is to create appropriate styles in Word, then look at the result (by unzipping the docx).

pandaadb wrote:case 2 is in MS Word07
When I right click on the TOC and refresh it manually, everything is recognized and the links to the headings (CTRL + Click) work just fine.
I figure that there is a way to invoke that refresh on the table of contents programmatically through docx4j but I couldn't find it. Any ideas why I get this behaviour?


The best you can do is to set the TOC to dirty, which iirc results in the user getting a prompt in Word when the docx is opened. see prior posts on this topic.

Re: Heading Style not recognized in Word 2007

PostPosted: Tue Dec 07, 2010 11:09 pm
by pandaadb
Hey,

so I managed to figure out how the styles and numbering parts work together.

First, I used the example code, to create a numbering part. I added references in the level elements to the styles I want to number ( Styles 1 through 7) It looks kind of like this:

Code: Select all
<w:lvl w:ilvl=\"0\">"
            + "<w:pStyle w:val=\"Heading1\"/>"
            + "<w:start w:val=\"1\"/>"
            + "<w:numFmt w:val=\"decimal\"/>"
            + "<w:lvlText w:val=\"%1)\"/>"
            + "<w:lvlJc w:val=\"left\"/>"
            + "<w:pPr>"
                + "<w:ind w:left=\"360\" w:hanging=\"360\"/>"
            + "</w:pPr>"
        + "</w:lvl>"


This works fine and is present in the document. The second part however doesn't want to behave the way I want it to. here is what happens:

Like in previous post, I unmarshall the default styles (to not having the create the headings by myself). I add them to the document.
After this, I now added a initialization method to reference my numbering to the headings:

Code: Select all
void initStyleNumbering() {
        for (Style style : Style.values()) {
            if (style == Style.DEFAULT) {
                continue;
            }
            org.docx4j.wml.Style styleObj = style.getStyle();
            PPr stylePpr = styleObj.getPPr();
            NumPr numPr = objFactory.createPPrBaseNumPr();
            NumId nId = new NumId();
            nId.setVal(BigInteger.valueOf( 1 ));
            numPr.setNumId(nId);
            stylePpr.setNumPr(numPr);
        }
    }


This method adds the reference to heading styles.
When I further debug to the place I actually add a heading and check on the style object, the numbering is referenced.
After my whole document is created, I save it by calling the save method:

Code: Select all
public void saveDocument() throws Docx4JException, JAXBException {
        wordMLPackageFATScript.save(new File(path));
    }


This works fine too.

However there is a problem with the document now. When I open it, there is no numbering. When I unzip it, and check the styles file, the styles I changed before, are unchanged again. non of my references are included anymore, so there is no way there can be numbering in the document. I know however, that I am heading down the right path, because:
After I saw that my references are gone, I manually typed them in (after unzipping my file) and saved the styles.xml. I zipped it again and opened it with MS Word 07 and there's my numbering in the styles.

So do you have any idea, why my Numbering references are gone after saving my document. is there any option I need to enable for this to be working?

On reference to the TOC before. You told me to set the TOC flag to dirty. I am not sure how to do this ...

Best regards and thanks for your help so far :),
-- artur

Re: Heading Style not recognized in Word 2007

PostPosted: Tue Dec 07, 2010 11:21 pm
by jason
pandaadb wrote:However there is a problem with the document now. When I open it, there is no numbering. When I unzip it,


When you unzip it, is the numbering part present?

If not, you probably didn't .addTargetPart.

pandaadb wrote:On reference to the TOC before. You told me to set the TOC flag to dirty. I am not sure how to do this ...


Please browse/search previous posts for the answer to this, or see Getting Started. You need:
Code: Select all
<w:fldChar w:fldCharType="begin" w:dirty="true"/>

Re: Heading Style not recognized in Word 2007

PostPosted: Tue Dec 07, 2010 11:49 pm
by pandaadb
Hi Jason,

I will browse for TOC - hopefully I'll find something :)

Anyway, I found the other error and it is my fault.

I was unmarshalling the styles, but the class that handled them, used a static access on the known styles. These styles had different references ( not the once that I added before ). So I was using two different objects for those styles and this way the ones I manipulated to number didn't get added to the document. I changed the access and now use the same objects for the styles and it works :)

Getting more and more into this framework :)

-- artur