Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Lists in few areas #110

Merged
merged 25 commits into from
Jul 30, 2023
Merged

Improve Lists in few areas #110

merged 25 commits into from
Jul 30, 2023

Conversation

PrzemyslawKlys
Copy link
Member

@PrzemyslawKlys PrzemyslawKlys commented Feb 18, 2023

Initially, when you add more than one list of the same style, it seems it's continuing numbering. While it's "ok" it's not as it was designed. When you use AddList(), it's supposed to create a new list and start from 1. It's not doing that now.

This PR:

  • forces every new list to start from "1", instead of continuing
  • adds the ability to choose that you want to continue numbering

This is initial commit, that seems to work using

LevelOverride levelOverride1 = new LevelOverride() { LevelIndex = 0 };
StartOverrideNumberingValue startOverrideNumberingValue1 = new StartOverrideNumberingValue() { Val = 1 };

But it seems that what Word sometimes uses is Nsid

Nsid nsid1 = new Nsid() { Val = "21351BF1" };

Why Word chooses one over the other - I've no clue.

  • adds ability to add lists to Headers and Footers
  • adds ability to add lists to TableCell (notice how paragraphs are handled in code)
internal static void Example_BasicLists7(string folderPath, bool openWord) {
    Console.WriteLine("[*] Creating standard document with lists - Document 7");
    string filePath = System.IO.Path.Combine(folderPath, "Document with Lists10.docx");
    using (WordDocument document = WordDocument.Create(filePath)) {

        // add list and nest a list
        WordList wordList1 = document.AddList(WordListStyle.Headings111, true);
        Console.WriteLine("List (0) ElementsCount (0): " + wordList1.ListItems.Count);
        Console.WriteLine("List (0) ElementsCount (1): " + document.Lists[0].ListItems.Count);
        wordList1.AddItem("Text 1");
        Console.WriteLine("List (0) ElementsCount (1): " + document.Lists[0].ListItems.Count);
        Console.WriteLine("Lists count (1): " + document.Lists.Count);
        Console.WriteLine("List (0) ElementsCount (1): " + wordList1.ListItems.Count);

        wordList1.AddItem("Text 2");
        Console.WriteLine("List (0) ElementsCount (2): " + wordList1.ListItems.Count);
        wordList1.AddItem("Text 2.1", 1);
        Console.WriteLine("List (0) ElementsCount (3): " + wordList1.ListItems.Count);

        WordList wordListNested = document.AddList(WordListStyle.Bulleted, false);
        wordListNested.AddItem("Nested 1", 1);
        wordListNested.AddItem("Nested 2", 1);

        WordList wordList2 = document.AddList(WordListStyle.Headings111, true);
        Console.WriteLine("List 2 - Restart numbering: " + wordList2.RestartNumbering);
        wordList2.AddItem("Section 2");
        wordList2.AddItem("Section 2.1", 1);

        WordList wordList3 = document.AddList(WordListStyle.Headings111, true);
        Console.WriteLine("List 3 - Restart numbering: " + wordList3.RestartNumbering);
        wordList3.RestartNumbering = true;
        Console.WriteLine("List 3 - Restart numbering after change: " + wordList3.RestartNumbering);
        wordList3.AddItem("Section 1");
        wordList3.AddItem("Section 1.1", 1);

        WordList wordList4 = document.AddList(WordListStyle.Headings111, true);
        //wordList4.RestartNumbering = true;
        wordList4.AddItem("Section 2");
        wordList4.AddItem("Section 2.1", 1);

        WordList wordList5 = document.AddList(WordListStyle.Headings111, true);
        //wordList5.RestartNumbering = true;
        wordList5.AddItem("Section 3");
        wordList5.AddItem("Section 3.1", 1);

        WordList wordList6 = document.AddList(WordListStyle.Headings111);
        wordList1.AddItem("Text 4");
        wordList1.AddItem("Text 4.1", 1);

        document.AddBreak();

        //// add a table
        var table = document.AddTable(3, 3);

        //// add a list to a table and attach it to a first paragraph 
        var listInsideTable = table.Rows[0].Cells[0].Paragraphs[0].AddList(WordListStyle.Bulleted);

        //// this will force the current Paragraph to be converted into a list item
        Console.WriteLine("Table List (0) ElementsCount (0): " + listInsideTable.ListItems.Count);
        listInsideTable.AddItem("text", 0, table.Rows[0].Cells[0].Paragraphs[0]);
        Console.WriteLine("Table List (0) ElementsCount (1): " + listInsideTable.ListItems.Count);

        // add new items to the list (as last paragraph)
        listInsideTable.AddItem("Test 1");
        Console.WriteLine("Table List (0) ElementsCount: " + listInsideTable.ListItems.Count);

        // add new items to the list (as last paragraph)
        listInsideTable.AddItem("Test 2");
        Console.WriteLine("Table List (0) ElementsCount: " + listInsideTable.ListItems.Count);

        table.Rows[0].Cells[0].AddParagraph("Test Text 1");
        listInsideTable.AddItem("Test 3");
        table.Rows[0].Cells[0].AddParagraph("Test Text 2");

        table.Rows[1].Cells[0].Paragraphs[0].Text = "Text Row 1 - 0";
        table.Rows[1].Cells[0].AddParagraph("Text Row 1 - 1").AddText(" More text").AddParagraph("Text Row 1 - 2");

        // add a list to a table by adding it to a cell, notice that that the first paragraph is empty
        var listInsideTableColumn2 = table.Rows[0].Cells[1].AddList(WordListStyle.Bulleted);
        Console.WriteLine("Table List (1) ElementsCount (0): " + listInsideTableColumn2.ListItems.Count);
        listInsideTableColumn2.AddItem("Test 1 - Column 2");
        Console.WriteLine("Table List (1) ElementsCount (1): " + listInsideTableColumn2.ListItems.Count);
        listInsideTableColumn2.AddItem("Test 2  - Column 2");
        Console.WriteLine("Table List (1) ElementsCount (2): " + listInsideTableColumn2.ListItems.Count);

        // add a list to a table by adding it to a cell, notice that I'm adding text before list first
        table.Rows[0].Cells[2].Paragraphs[0].Text = "This is list: ";
        // add list, and add all items
        var listInsideTableColumn3 = table.Rows[0].Cells[2].AddList(WordListStyle.Bulleted);
        Console.WriteLine("Table List (2) ElementsCount: " + listInsideTableColumn3.ListItems.Count);
        listInsideTableColumn3.AddItem("Test 1 - Column 2");
        Console.WriteLine("Table List (2) ElementsCount: " + listInsideTableColumn3.ListItems.Count);
        listInsideTableColumn3.AddItem("Test 2  - Column 2");
        Console.WriteLine("Table List (2) ElementsCount: " + listInsideTableColumn3.ListItems.Count);

        Console.WriteLine("Lists count in a document (10): " + document.Lists.Count);

        document.Save(false);
    }


    using (WordDocument document = WordDocument.Load(filePath)) {
        Console.WriteLine("Lists count in a document (10): " + document.Lists.Count);

        document.Lists[0].AddItem("More then enough");

        document.AddHeadersAndFooters();

        var listInHeader = document.Header.Default.AddList(WordListStyle.Bulleted);

        listInHeader.AddItem("Test Header 1");

        document.Footer.Default.AddParagraph("Test Me Header");

        listInHeader.AddItem("Test Header 2");


        var listInFooter = document.Footer.Default.AddList(WordListStyle.Headings111);

        listInFooter.AddItem("Test Footer 1");

        document.Footer.Default.AddParagraph("Test Me Footer");

        listInFooter.AddItem("Test Footer 2");

        document.Save(openWord);
    }
}
  • Resolves restartNumberingAfterBreak is not declared

image

  • Add RestartNumberingAfterBreak to lists
  • Fix for missing TableGrid

@PrzemyslawKlys PrzemyslawKlys marked this pull request as ready for review July 30, 2023 13:36
@PrzemyslawKlys PrzemyslawKlys merged commit e4f573e into master Jul 30, 2023
@PrzemyslawKlys PrzemyslawKlys deleted the ImproveLists branch March 8, 2024 08:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant