poi-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jim Klo <jim.klo....@gmail.com>
Subject Reset numbering in a list
Date Mon, 24 Oct 2016 16:52:23 GMT
Greetings,

I’m trying to figure out how to reset numbering in a list… but can’t seem to figure
out how this works.

I’ve searched the archives of this list, and found this same question unanswered [1]. Hoping
someone might be able to provide me with some hints on how to do this.

So far, I’m doing this a bit differently than the original asker.

My approach is to use an existing Word Document as a Styles template. Within the template
I defined a style named "SRINumberList".

So to load the template and remove everything that's not in the Header or Footer:

protected void createDocFromTemplate() {
    try {
        document = new XWPFDocument(this.getClass().getResourceAsStream(styleTemplate));

        int pos = document.getBodyElements().size()-1;

        while (pos >= 0) {
            IBodyElement element = document.getBodyElements().get(pos);
            if (!EnumSet.of(BodyType.HEADER, BodyType.FOOTER).contains(element.getPartType()))
{
                boolean success = document.removeBodyElement(pos);
                logger.log(Level.INFO, "Removed body element "+pos+": "+success);
            }
            pos--;
        }

    } catch (IOException e) {
        logger.log(Level.WARNING, "Not able to load style template", e);
        document = new XWPFDocument();
    }

}
Now within my document there are several different sections that contain a numbered lists.
Each should be restart numbering from 1. This is the typical way I'm doing this:

if (itemStem.getItems().size() > 0) {
        p = document.createParagraph();
        p.setStyle(ParaStyle.StemAndItemTitle.styleId);

        run = p.createRun();
        run.setText("Sub Items");

        itemStem.getItems().stream().forEach(item -> {
            XWPFParagraph p2 = document.createParagraph();
            p2.setStyle(ParaStyle.NumberList.styleId);

            XWPFRun run2 = p2.createRun();
            run2.setText(item.getSubItemText());
        });
        p = document.createParagraph();
        p.createRun();
}
So this correctly applies the Style that contains the number format, but there is only a single
sequence (1 ... to however many list items exit in the doc).

Heading 1
1. item a
2. item b
3. item c

Heading 2
4. item a
5. item d
6. item g
But what I want is:

Heading 1
1. item a
2. item b
3. item c

Heading 2
1. item a
2. item d
3. item g
So basically I'm trying to figure out how to use the style I have but restart page numbering
a various spots in the document. Can someone provide a sample of how this would work?

Thanks,

Jim

[1] https://lists.apache.org/thread.html/a8f6c93961f6a7d0d4cd6401a9bac7335fab69cef1ac0093972d4715@1455711857@%3Cuser.poi.apache.org%3E
<https://lists.apache.org/thread.html/a8f6c93961f6a7d0d4cd6401a9bac7335fab69cef1ac0093972d4715@1455711857@%3Cuser.poi.apache.org%3E>
On 2016-02-17 04:24 (-0700), Torsten Weber <t...@gmail.com> wrote: 
> Hello,> 
> 
> 
> if I have two lists, how do I reset the numbering to start at 1?> 
> 
> Example:> 
> 
> 1. a> 
> 2. a> 
> 3. a> 
> 
> 1. b> 
> 2. b> 
> 3. b> 
> 
> 
> In the example below, the second list continues counting:> 
> ..> 
> 
> 4. b> 
> 5. b> 
> 6. b> 
> 
> 
> public class Example {> 
>     private static XWPFNumbering numbering;> 
> 
>     public static void main(String[] args)throws Exception> 
>     {> 
>         XWPFDocument document= new XWPFDocument();> 
>         FileOutputStream out = new FileOutputStream(new File("test.docx"));> 
> 
>         numbering = document.createNumbering();> 
> 
>         XWPFParagraph paragraph;> 
> 
>         for(int i = 1; i <= 6; i++) {> 
>             paragraph = document.createParagraph();> 
>             BigInteger numId = numbering.addNum(BigInteger.valueOf(0));> 
>             paragraph.setNumID(numId);> 
>             paragraph.getCTP().getPPr().getNumPr().addNewIlvl().setVal(BigInteger.valueOf(1));>

>             XWPFRun run=paragraph.createRun();> 
>             run.setText(String.valueOf(i));> 
> 
>             if(i == 3) {> 
>                 // Reset numbering. Start by 1.> 
> 
> *                // ..*> 
> 
>             }> 
>         }> 
> 
> 
>         document.write(out);> 
>         out.close();> 
>     }> 
> }> 
> 
> 
> 
> Thanks!> 
> 
Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message