Page 1 of 1

Autogeneration numbering. Why?

PostPosted: Wed Feb 12, 2014 12:55 am
by Oleksandr
Does anybody know why docx4j generates "Numbering" for Headings?
Here is an example of code:

Code: Select all
template = WordprocessingMLPackage.load(this.getClass().getClassLoader().getResourceAsStream(templatePath));
MainDocumentPart documentPart = template.getMainDocumentPart();

documentPart.addStyledParagraphOfText("Heading1", "test1");
documentPart.addStyledParagraphOfText("Heading1", "test2");
documentPart.addStyledParagraphOfText("Heading1", "test3");


The output looks like this:
1. test1
2. test2
3. test3

The "Heading1" style in the template(template is empty docx file) doesn't have numbering.

Re: Autogeneration numbering. Why?

PostPosted: Wed Feb 12, 2014 7:20 am
by jason
Please attach your docx.

Re: Autogeneration numbering. Why?

PostPosted: Wed Feb 12, 2014 7:59 pm
by Oleksandr
Hi Jason,
Thank you for you reply. I've changed the post by adding attachments.

Re: Autogeneration numbering. Why?

PostPosted: Wed Feb 12, 2014 9:04 pm
by jason
Your after document's heading 1 style definition includes numbering (<w:numId w:val="3"/>):

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  <w:style w:type="paragraph" w:styleId="Heading1">
    <w:name w:val="heading 1"/>
    <w:basedOn w:val="Normal"/>
    <w:next w:val="Normal"/>
    <w:link w:val="Heading1Char"/>
    <w:uiPriority w:val="9"/>
    <w:qFormat/>
    <w:pPr>
      <w:keepNext/>
      <w:keepLines/>
      <w:numPr>
        <w:numId w:val="3"/>
      </w:numPr>
      <w:spacing w:before="480" w:after="0"/>
      <w:outlineLvl w:val="0"/>
    </w:pPr>
 
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


So the numbering is to be expected.

Your before docx does not have a definition for heading 1, so when you use it, docx4j falls back to use the entry from:

org/docx4j/openpackaging/parts/WordprocessingML/KnownStyles.xml

defined as above. (As an aside, I noticed that the addStyledParagraphOfText method didn't cause a Numbering part to be added. I must confess I never use this method myself...)

So to avoid this behaviour you should ensure the style is defined in your before document. In practice, this happens if in Word you use the style in the docx.

Alternatively, you can programmatically add the style.

Or you could change the definition of the style in KnownStyles.xml

Re: Autogeneration numbering. Why?

PostPosted: Wed Feb 12, 2014 9:50 pm
by Oleksandr
Thanks a lot for your help. I'll investigate it.