Page 1 of 1

Problem when create a new numbered list

PostPosted: Mon Jul 17, 2017 7:55 pm
by ClementCvl
Hi,

I want to create a new numbered list, visually it's work fine but it doesn't appear in the numbering.xml part of my docx (in attachment) when I unzip it so it's cause trouble whit my pdf converter (it's not the pdf converter of docx4j).

I create my list like that:
Code: Select all
                PPr pprList = factory.createPPr();
      NumPr numprList = factory.createPPrBaseNumPr();
      Ilvl ilvlList = factory.createPPrBaseNumPrIlvl();
      NumId numidList = factory.createPPrBaseNumPrNumId();
      R rList = factory.createR();
      Text textList = factory.createText();
         
      rList.setRPr(this.runProperties);
      pprList = this.paragraphStyle;
      ilvlList.setVal(BigInteger.valueOf(1));
      numidList.setVal(BigInteger.valueOf(numberList));
      numprList.setIlvl(ilvlList);
      numprList.setNumId(numidList);
      pprList.setNumPr(numprList);
      PStyle listStyle = new PStyle();
      listStyle.setVal("ListParagraph");
      pprList.setPStyle(listStyle);
      pList.setPPr(pprList);
      textList.setValue("test");
      rList.getContent().add(textList); \\My rList it's a run which contains the list


Anyone know how t oadd my list in the numbering.xml ?

Re: Problem when create a new numbered list

PostPosted: Mon Jul 17, 2017 9:21 pm
by jason
You need to create abstract and concrete lists (your numPr points to the concrete list, and the concrete points to the abstract list), and add both to your NumberingDefinitionsPart (ndp).

See for example https://github.com/plutext/docx4j-Impor ... .java#L155

concreteList = ndp.addAbstractListNumberingDefinition(abstractList)

You can get a reference to your NDP from your MainDocumentPart

Re: Problem when create a new numbered list

PostPosted: Tue Jul 18, 2017 12:09 am
by ClementCvl
I don't understand how to do, how can I create my "concretelist " ?

Re: Problem when create a new numbered list

PostPosted: Tue Jul 18, 2017 10:16 am
by jason
Create an abstract list. Easiest to do this by defining a list in Word, then using the docx4j webapp to generate corresponding code. (Navigate to the numbering definitions part)

Once you have your abstract list, you can create a simple concrete list (ie one which just points at the abstract list):

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
concreteList = ndp.addAbstractListNumberingDefinition(abstractList)
 
Parsed in 0.013 seconds, using GeSHi 1.0.8.4


Here is that method:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
        /**
         * Add the specified definition, allocating it a new w:abstractNumId.
         *
         * Also create and add an associated ListNumberingDefinition, and return
         * this associated ListNumberingDefinition (since that is
         * what you are likely to use in the document).
         *
         * @param abstractNum
         * @return
         */

        public Numbering.Num addAbstractListNumberingDefinition(Numbering.AbstractNum abstractNum) {
               
                //////////////////////////////////////////////
                // Numbering.AbstractNum abstractNum
               
                // Generate a unique w:abstractNumId for it
                int nextId = getAbstractListDefinitions().size();              
        do {
                nextId++;              
        } while (getAbstractListDefinitions().containsKey( "" + nextId ));     
        abstractNum.setAbstractNumId( BigInteger.valueOf(nextId) );
       
        // Add it to our JAXB object
        this.getJaxbElement().getAbstractNum().add(abstractNum);
       
        // Add it to our hashmap
        AbstractListNumberingDefinition absNumDef = new AbstractListNumberingDefinition(abstractNum);
        abstractListDefinitions.put(absNumDef.getID(), absNumDef);

                //////////////////////////////////////////////
                // Numbering.Num num
       
        // Now make an associated ListNumberingDefinition
                //      <w:num w:numId="1">
                //        <w:abstractNumId w:val="1"/>
                //      </w:num>"        
        Numbering.Num num = Context.getWmlObjectFactory().createNumberingNum();
        Numbering.Num.AbstractNumId abstractNumId = Context.getWmlObjectFactory().createNumberingNumAbstractNumId();
        abstractNumId.setVal(BigInteger.valueOf(nextId) );
        num.setAbstractNumId(abstractNumId);
       
        nextId = getInstanceListDefinitions().size();          
        do {
                nextId++;              
        } while (getInstanceListDefinitions().containsKey( "" + nextId ));     
        num.setNumId( BigInteger.valueOf(nextId) );  
       
        // Add it to our JAXB object
        this.getJaxbElement().getNum().add(num);
       
        // Add it to our hashmap
        ListNumberingDefinition listDef = new ListNumberingDefinition(num, abstractListDefinitions);
        instanceListDefinitions.put(listDef.getListNumberId(), listDef);
       
        //
        return num;
               
        }
 
Parsed in 0.017 seconds, using GeSHi 1.0.8.4


Notice it returns Numbering.Num. That gives you the num id you need to add in your w:p/w:pPr/w:numPr

Re: Problem when create a new numbered list

PostPosted: Tue Jul 18, 2017 6:58 pm
by ClementCvl
I've add an abstract list like you said, and it give me a Num that i've add to my list but in my numbering.xml it doesn't appear. And I have a second question, if I don't have a previous list I don't have a numbering part in my docx so when I use
Code: Select all
this.mainDoc.getNumberingDefinitionsPart();
it's return null.
Code: Select all
NumberingDefinitionsPart ndp = this.mainDoc.getNumberingDefinitionsPart();
      
Numbering.AbstractNum numberingabstractnum = Context.getWmlObjectFactory().createNumberingAbstractNum();
      
Num concreteList = ndp.addAbstractListNumberingDefinition(numberingabstractnum);

ilvlList.setVal(BigInteger.valueOf(1));
numidList.setVal(concreteList.getNumId());

Re: Problem when create a new numbered list

PostPosted: Tue Jul 18, 2017 9:59 pm
by jason
The code you listed is a long way short of a working example.

https://github.com/plutext/docx4j/blob/ ... start.java might help you. It also shows how to create the NDP (if absent).

Re: Problem when create a new numbered list

PostPosted: Wed Jul 19, 2017 6:23 pm
by ClementCvl
When I create the NDP like you do in the exemple, it seems he replaced by an other NDP in the final doc.
I create exactly the ndp in the exemple:

Code: Select all
   NumberingDefinitionsPart ndp;
   try {
      ndp = new NumberingDefinitionsPart();
      this.mainDoc.addTargetPart(ndp);
      ndp.setJaxbElement( (Numbering) XmlUtils.unmarshalString(createNumbering()) );   
   } catch (InvalidFormatException | JAXBException e) {
      e.printStackTrace();
   }


And the function createNumbering() return the string with definition.

But at the end in my document the ndp is:

Code: Select all
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:numbering xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:sl="http://schemas.openxmlformats.org/schemaLibrary/2006/main" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart" xmlns:cdr="http://schemas.openxmlformats.org/drawingml/2006/chartDrawing" xmlns:dgm="http://schemas.openxmlformats.org/drawingml/2006/diagram" xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture" xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing" xmlns:dsp="http://schemas.microsoft.com/office/drawing/2008/diagram" xmlns:xvml="urn:schemas-microsoft-com:office:excel" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:pvml="urn:schemas-microsoft-com:office:powerpoint" xmlns:cppr="http://schemas.microsoft.com/office/2006/coverPageProps" xmlns:odx="http://opendope.org/xpaths" xmlns:odc="http://opendope.org/conditions" xmlns:odq="http://opendope.org/questions" xmlns:oda="http://opendope.org/answers" xmlns:odi="http://opendope.org/components" xmlns:odgm="http://opendope.org/SmartArt/DataHierarchy" xmlns:b="http://schemas.openxmlformats.org/officeDocument/2006/bibliography" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid" xmlns:wetp="http://schemas.microsoft.com/office/webextensions/taskpanes/2010/11" xmlns:we="http://schemas.microsoft.com/office/webextensions/webextension/2010/11" xmlns:comp="http://schemas.openxmlformats.org/drawingml/2006/compatibility" xmlns:lc="http://schemas.openxmlformats.org/drawingml/2006/lockedCanvas">
    <w:abstractNum w:abstractNumId="1">
        <w:nsid w:val="12D402B7"/>
        <w:multiLevelType w:val="hybridMultilevel"/>
        <w:tmpl w:val="AECAFC2E"/>
        <w:lvl w:tplc="04090001" 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:hAnsi="Symbol" w:ascii="Symbol" w:hint="default"/>
            </w:rPr>
        </w:lvl>
    </w:abstractNum>
    <w:num w:numId="1">
        <w:abstractNumId w:val="1"/>
    </w:num>
</w:numbering>


Re: Problem when create a new numbered list

PostPosted: Wed Jul 19, 2017 8:33 pm
by jason
ClementCvl wrote:it seems he replaced by an other NDP in the final doc


That must be happening somewhere else in your code. It might not be another NDP, but rather, that you are replacing its content somewhere.