RestartList.cs
//
// This code is part of GrapeCity Documents for Word samples.
// Copyright (c) GrapeCity, Inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using GrapeCity.Documents.Word;

namespace GcWordWeb.Samples
{
    // This sample demonstrates how to restart numbering
    // in a numbered list
    // with three levels.
    // The code creating the lists is similar to NumberedList sample.
    public class RestartList
    {
        public GcWordDocument CreateDocx()
        {
            GcWordDocument doc = new GcWordDocument();
            var pars = doc.Body.Sections.First.GetRange().Paragraphs;
            pars.Add("A numbered list with 3 levels:");
            // A ListTemplate is used to make paragraphs part of a list:
            var myListTemplate = doc.ListTemplates.Add(BuiltInListTemplateId.NumberDefault, "myListTemplate");

            // Add a 3-level numbered nested list:
            addItem("Top item 1", 0);
            addItem("Top item 2", 0);
            addItem("Top item 3", 0);
            addItem("Nested item 1", 1);
            addItem("Nested item 2", 1);
            addItem("Double nested item 1", 2);
            addItem("Double nested item 2", 2);
            // Restart a list with default numbering:
            addItem("Double nested item 3 - restart at 'i'", 2).RestartList();
            addItem("Double nested item 4", 2);
            addItem("Double nested item 5", 2);
            addItem("Nested item 3", 1);
            addItem("Nested item 4", 1);
            addItem("Nested item 5", 1);
            addItem("Nested item 6", 1);
            // Restart a letter-numbered ("a, b, c,...") list at letter 'c':
            addItem("Nested item 7 - restart at 'c'", 1).RestartList(3);
            addItem("Nested item 8", 1);
            addItem("Nested item 9", 1);
            addItem("Top item 4", 0);
            addItem("Top item 5", 0);

            // Done:
            return doc;

            Paragraph addItem(string t_, int l_)
            {
                var p_ = pars.Add(t_);
                // This makes a paragraph a list item:
                p_.ListFormat.Template = myListTemplate;
                // Set the item's nesting level (first level is 0):
                p_.ListFormat.LevelNumber = l_;
                // This ensures item spacing consistent with MS Word:
                p_.Style = doc.Styles[BuiltInStyleId.ListParagraph];
                return p_;
            }
        }
    }
}