Page 1 of 1

Unordered Lists

PostPosted: Sun Jul 29, 2012 10:53 pm
by sandra
I have a problem with unordered lists in docx4j.

I use the following code to create a list point:

Code: Select all
private void addUnorderedListPoint(String text, int i)  {
    docx4jParagraph[i] = factory.createP();
    org.docx4j.wml.Text  t = factory.createText();
    t.setValue(text);
    org.docx4j.wml.R  run = factory.createR();
    run.getContent().add(t);   
    docx4jParagraph[i].getContent().add(run);   
    org.docx4j.wml.PPr ppr = factory.createPPr();   
    docx4jParagraph[i].setPPr( ppr );         
   
    PPrBase.Spacing space = new PPrBase.Spacing();
    space.setAfter(BigInteger.ZERO);
    space.setBefore(BigInteger.ZERO);
    ppr.setSpacing(space);   
   
    NumPr numPr =  factory.createPPrBaseNumPr();
    ppr.setNumPr(numPr);       
   
    Ilvl ilvlElement = factory.createPPrBaseNumPrIlvl();
    numPr.setIlvl(ilvlElement);
    ilvlElement.setVal(BigInteger.valueOf(0));     
   
    NumId numIdElement = factory.createPPrBaseNumPrNumId();
    numPr.setNumId(numIdElement);
    numIdElement.setVal(BigInteger.valueOf(listNumber));   
  }


but there is no way for me the get a "unordered" list with "dots" in front of each entry. Every time this creates an ordered list with leading numbers. I also tried to compare the XML code Word creates for unordered and ordered lists, but i can't see the difference...

Hope anyone has an idea to solve this problem.

Best regards
Sandra

How does the numbering.xml work?

PostPosted: Mon Jul 30, 2012 9:10 pm
by sandra
I tried a very long time to understand how the numbering.xml works but i'm still not shure.

In an empty Document you don't have any numbering.xml. As soon as you create a listing word will create a numbering.xml for you which looks maybe similar like this:

Code: Select all
<w:abstractNum w:abstractNumId="0">
<w:nsid w:val="330470DD"/>
<w:multiLevelType w:val="hybridMultilevel"/>
<w:tmpl w:val="1B168428"/>-
<w:lvl w:tplc="04070001" w:ilvl="0"><w:start w:val="1"/>
<w:numFmt w:val="bullet"/>
<w:lvlText w:val=" "/>
<w:lvlJc w:val="left"/>
<w:pPr>
  <w:ind w:hanging="360" w:left="720"/>
</w:pPr>
-<w:rPr>
   <w:rFonts w:hint="default" w:hAnsi="Symbol" w:ascii="Symbol"/>
</w:rPr>
</w:lvl>
[....]



But i don't understand the meaning of this code. Because my first listing in document is not "bulleted" - it is an ordered listing with numbers. So I'm confused if abstractNumId="0" refers the numID = 0 in document.xml or not. Also there are much more ilvl entries in the abstractNumId="0" section than there are real listnodes in my listing (which have ilvl = 0 in document.xml - i'm also confused about that).

Is there any documentation about numbering.xml and the other xml files created by word?

Beacuse at the moment i see no chance for me to create any docx document. I'm now trying for days to ceate a simple list with dot's but always getting lists with numbers (guess this is default) but i don't know how to tell word it would be really kind if it could use simple unordered list for me...

I only can assume that i have to construct a usefull numbering.xml. Even if i don't know how i can create this file or how it works at all.

My questions in summary are:

- How does the Numbering.xml work ?
- How can I create the Numbering.xml if I don't have one ?
- How can I use the "styles" I defined in the Numbering.xml ?

Sorry for my bad englisch. I tried my best. I hope that someone can help me.

Looking forward for any reply.

Best regards,

Sandra

Re: Unordered Lists

PostPosted: Mon Jul 30, 2012 11:09 pm
by jason
Please see http://msdn.microsoft.com/en-us/library/ee922775(office.14).aspx for an intro to numbering

You can do something like:

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

                WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
               
                NumberingDefinitionsPart ndp = new NumberingDefinitionsPart();
                wordMLPackage.getMainDocumentPart().addTargetPart(ndp);
                ndp.unmarshalDefaultNumbering();               
 
Parsed in 0.015 seconds, using GeSHi 1.0.8.4

Re: Unordered Lists

PostPosted: Mon Jul 30, 2012 11:48 pm
by sandra
okay i found at the end of the numbering.xml the following code:

Code: Select all
<w:num w:numId="1">
<w:abstractNumId w:val="0"/>
</w:num>

<w:num w:numId="2">
<w:abstractNumId w:val="1"/>
</w:num>


So i guess, this code is resposible for the assignment between list and formatting. Right ? But how can i create this code? And at it to the numbering.xml?
Assume we would have the numbering.xml with the all the different abstractNum "styles"

Code: Select all
<w:abstractNum w:abstractNumId="0">
[...]
Entries how can i add the

Code: Select all
<w:num w:numId="1">
<w:abstractNumId w:val="0"/>
</w:num>


paragraph later to let 'Word' know that the listing 1 should use the style defined by <w:abstractNum w:abstractNumId="0"> ??

Which class of docx4j represents w:num ?

I tried this:

Code: Select all
Numbering.Num myNumbering = new NumeringNum();
myNumbering.setNumID(BigInteger.ONE);
Numbering.Num.AstractNumId aNumId = new Numbering.Num.AstractNumId();
aNumId .setVal(BigInteger.ZERO);
myNumbering.setAstractNumId(aNumId);


But I don't know if this is correct and what i should do this myNumbering now...

Re: Unordered Lists

PostPosted: Mon Jul 30, 2012 11:51 pm
by sandra
Thank you very much for your answer Jason.

jason wrote:Please see http://msdn.microsoft.com/en-us/library/ee922775(office.14).aspx for an intro to numbering

You can do something like:

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

                WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
               
                NumberingDefinitionsPart ndp = new NumberingDefinitionsPart();
                wordMLPackage.getMainDocumentPart().addTargetPart(ndp);
                ndp.unmarshalDefaultNumbering();               
 
Parsed in 0.014 seconds, using GeSHi 1.0.8.4


Yes, then i have a Numbering.xml but how can use this styles for my listings, as mentioned in my last post?

Like:

Code: Select all
<w:num w:numId="1">
<w:abstractNumId w:val="0"/>
</w:num>

Re: Unordered Lists

PostPosted: Tue Jul 31, 2012 9:31 pm
by sandra
I still have the same problem.
The numbering.xml exists, but how can i now use the abstract numberings for my listings ?

The numbering.xml is created by using a tmp document as reference during the creation of the document. That works fine!
Now I have written a own WordList class which contains an array of Paragraphs. But how can I tell a paragraph if it is an ordered or and unordered listing ? How can I use the information from numbering.xml ?


Code: Select all
public class WordList {
 
  private static int listCounter = 0;    //counter for the total number of listings in the document
  private final int listNumber;           //number of this listing in the document (1 for first listing...)
  private int size;                            //number of list nodes
  private List<String> content;         //just for information, not important
  private P[] docx4jParagraph; 
  private org.docx4j.wml.ObjectFactory factory = Context.getWmlObjectFactory(); 
  private WordprocessingMLPackage wmlPackage;
 
  public WordList(List<String> liste, boolean numbered, WordprocessingMLPackage wmlPackage){
    listNumber = ++listCounter; //eindeutige Nummer der Liste
    content = liste;
    size = liste.size();   
    docx4jParagraph = new P[size];
    this.wmlPackage = wmlPackage;
   
   for(int i = 0; i< size; i++){
        addListPoint(liste.get(i), i, numbered);
   }   
   
  }
 
  private void addListPoint(String text, int i)  {
    docx4jParagraph[i] = factory.createP();
    org.docx4j.wml.Text  t = factory.createText();
    t.setValue(text);
    org.docx4j.wml.R  run = factory.createR();
    run.getContent().add(t);   
    docx4jParagraph[i].getContent().add(run);   
    org.docx4j.wml.PPr ppr = factory.createPPr();   
    docx4jParagraph[i].setPPr(ppr);
     
    //spacing
   
    PPrBase.Spacing space = new PPrBase.Spacing();
    space.setAfter(BigInteger.ZERO);
    space.setBefore(BigInteger.ZERO);
    ppr.setSpacing(space);
   
    // Create and add <w:numPr>
    NumPr numPr =  factory.createPPrBaseNumPr();
    ppr.setNumPr(numPr);
       
    // The <w:ilvl> element
    Ilvl ilvlElement = factory.createPPrBaseNumPrIlvl();
    numPr.setIlvl(ilvlElement);
    ilvlElement.setVal(BigInteger.valueOf(0));   
     
    // The <w:numId> element
    NumId numIdElement = factory.createPPrBaseNumPrNumId();
    numPr.setNumId(numIdElement);
    numIdElement.setVal(BigInteger.valueOf(listNumber));   
  }   
 
}


I need very urgend help, working on this for days now. Every proposal is highly appreciated.
Best regards,
Sandra

Re: Unordered Lists

PostPosted: Tue Jul 31, 2012 10:17 pm
by sandra
This is making me crazy. Is it impossible to create the numbering.xml dynamically ???

Because I have to create a very huge word document with many listings, and i can't say in advance how many listings I will have in the end.

What I want:

As soon as I create a WordList, the numbering.xml should be expanded by the code similar like

Code: Select all
<w:num w:numId="1">
<w:abstractNumId w:val="0"/>
</w:num>


and that should happen every time I create a WordList. (I already have a counter)

So I think there should be a way to create some default abstractNum's at the beginning (after creating the document) and just add the code above in the case it is needed to the numbering.xml.

Is this possible? Or am I looking for unicorns?

Re: Unordered Lists

PostPosted: Tue Jul 31, 2012 11:27 pm
by jason
Here is what we do in XHTMLImporter:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
                    // Create and add <w:numPr>
                    NumPr numPr =  Context.getWmlObjectFactory().createPPrBaseNumPr();
                    currentP.getPPr().setNumPr(numPr);

                    // The <w:numId> element
                    NumId numIdElement = Context.getWmlObjectFactory().createPPrBaseNumPrNumId();
                    numPr.setNumId(numIdElement);
                    numIdElement.setVal( num.getNumId() ); // point to the correct list
                   
                    // The <w:ilvl> element
                    Ilvl ilvlElement = Context.getWmlObjectFactory().createPPrBaseNumPrIlvl();
                    numPr.setIlvl(ilvlElement);
                    ilvlElement.setVal(BigInteger.valueOf(0));
 
Parsed in 0.015 seconds, using GeSHi 1.0.8.4


hope this helps .. Jason

Re: Unordered Lists

PostPosted: Wed Aug 01, 2012 1:27 am
by sandra
jason wrote:Here is what we do in XHTMLImporter:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
                    // Create and add <w:numPr>
                    NumPr numPr =  Context.getWmlObjectFactory().createPPrBaseNumPr();
                    currentP.getPPr().setNumPr(numPr);

                    // The <w:numId> element
                    NumId numIdElement = Context.getWmlObjectFactory().createPPrBaseNumPrNumId();
                    numPr.setNumId(numIdElement);
                    numIdElement.setVal( num.getNumId() ); // point to the correct list
                   
                    // The <w:ilvl> element
                    Ilvl ilvlElement = Context.getWmlObjectFactory().createPPrBaseNumPrIlvl();
                    numPr.setIlvl(ilvlElement);
                    ilvlElement.setVal(BigInteger.valueOf(0));
 
Parsed in 0.015 seconds, using GeSHi 1.0.8.4


hope this helps .. Jason


I think this is creating the property for the listings in the document.xml . So with your code i will get something like this:

Code-Example1:
Code: Select all
<w:p>
  <w:pPr>
    <w:pStyle w:val="ListParagraph"/>
    <w:numPr>
      <w:ilvl w:val="0"/>
      <w:numId w:val="1"/>
    </w:numPr>
  </w:pPr>
  <w:r>
    <w:t>One</w:t>
  </w:r>
</w:p>


But this only can refer to already existing entries in the numbering.xml (using the numID and ilvl value) or did i get something wrong?

What i want to create are the entries in the numbering.xml:

Code-Example2:
Code: Select all
<w:abstractNum w:abstractNumId="0">
  <w:lvl w:ilvl="0">
    <w:start w:val="1"/>
    <w:numFmt w:val="bullet"/>
    <w:lvlText w:val="o"/>
    <w:lvlJc w:val="left"/>
    <w:pPr>
      <w:ind w:left="720"
             w:hanging="360"/>
    </w:pPr>
    <w:rPr>
      <w:rFonts w:ascii="Symbol"
                w:hAnsi="Symbol"
                w:hint="default"/>
    </w:rPr>
  </w:lvl>
  <!-- several w:lvl elements elided -->
  <w:num w:numId="1">
    <w:abstractNumId w:val="0"/>
  </w:num>
   <w:num w:numId="2">
    <w:abstractNumId w:val="1"/>
  </w:num>
</w:numbering>


Because there are many listings i can't create the numbering.xml in a static way. For example if i would only have two num's (one for unordered and one for ordered listings) in the numbering.xml:

Code: Select all
<w:num w:numId="1">
    <w:abstractNumId w:val="0"/>
</w:num>
   <w:num w:numId="2">
    <w:abstractNumId w:val="1"/>
</w:num>


the two listings (now in document.xml):

Code: Select all
<w:p>
  <w:pPr>
    <w:pStyle w:val="ListParagraph"/>
    <w:numPr>
      <w:ilvl w:val="0"/>
      <w:numId w:val="1"/>
    </w:numPr>
  </w:pPr>
  <w:r>
    <w:t>One</w:t>
  </w:r>
</w:p>

<w:p>
  <w:pPr>
    <w:pStyle w:val="ListParagraph"/>
    <w:numPr>
      <w:ilvl w:val="0"/>
      <w:numId w:val="1"/>
    </w:numPr>
  </w:pPr>
  <w:r>
    <w:t>Two</w:t>
  </w:r>
</w:p>


would belong together (because both have numId = 1) . So the numbering would be like

1. One
2. Two

But because this are two different listings they should be severed:

1.One
1.Two

So I think the only way can be to create the numbering.xml dynamically. Maybe it will work if i could create the code as seen in Code-Example2 for each listing I physically create in my document.

Summing up: I would like to have a seperate <w:num in numbering.xml for each listing i have in document.xml

Code: Select all

//this for my first listing
<w:num w:numId="1">
    <w:abstractNumId w:val="1"/>
  </w:num>
//this for my second listing
<w:num w:numId="2">
    <w:abstractNumId w:val="1"/>
  </w:num>
//this for my third listing
<w:num w:numId="3">
    <w:abstractNumId w:val="1"/>
  </w:num>
and so on......


Is this possible? Or should I use something like 'restartNumbering' ? (I saw that there is such a methode in NumberingDefinitionsPart but don't know how to use it)

Sorry for my long post, but i tried to explain in detail what I want to do ... maybe I try to do something extraordinary and don't know it :)

Re: Unordered Lists

PostPosted: Wed Aug 01, 2012 5:04 am
by cjohnson
This actually has a more normal case.

Make a list using the ilvl method, make a new paragraph, make another list. The list doesn't reset.

1. Dog
2. Cat
3. Bird

Here's some text that is a new paragraph!

4. Green
5. Blue
6. Red

Re: Unordered Lists

PostPosted: Wed Aug 01, 2012 8:57 am
by jason
See the NumberingRestart sample.

See also org.docx4j.convert.in.xhtml.ListHelper.