Как создавать собственные списки в C# и .NET

  1. Добавьте SautinSoft.Document из Nuget.
  2. Создайте документ.
  3. Создайте список с символом ASTERISM.
  4. Сохраните документ в формате DOCX.

Полный код

using SautinSoft.Document;

namespace Sample
{
    class Sample
    {

        static void Main(string[] args)
        {
            // Get your free 100-day key here:   
            // https://sautinsoft.com/start-for-free/

            CustomizedLists();
        }

        /// <summary>
        /// How to create customized lists.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/create-customized-list-in-docx-document-net-csharp-vb.php
        /// </remarks>		
        public static void CustomizedLists()
        {
            string documentPath = @"CustomizedLists.docx";

            // Let's create a new document.
            DocumentCore dc = new DocumentCore();

            // Add a new section.
            Section s = new Section(dc);
            dc.Sections.Add(s);

            // Create a new list style based on the ListTemplateType.Bullet template.
            ListStyle myCustomList1 = new ListStyle("MyCustomList1", ListTemplateType.Bullet);

            // Replace the standard bullet with the symbol ASTERISM (unicode 0x2042).
            myCustomList1.ListLevelFormats[0].NumberFormat = "\x2042";

            // Change the character formatting.
            myCustomList1.ListLevelFormats[0].CharacterFormat.FontName = "Calibri";
            myCustomList1.ListLevelFormats[0].CharacterFormat.FontColor = Color.Blue;
            dc.Styles.Add(myCustomList1);

            Paragraph a1 = new Paragraph(dc, "Asterism 1.");
            a1.ListFormat.Style = myCustomList1;
            s.Blocks.Add(a1);

            Paragraph a2 = new Paragraph(dc, "Asterism 2.");
            a2.ListFormat.Style = myCustomList1;
            a2.ParagraphFormat.SpaceAfter = 30;
            s.Blocks.Add(a2);

            // Create a new list style based on the ListTemplateType.NumberWithDot template.
            ListStyle myCustomList2 = new ListStyle("MyCustomList2", ListTemplateType.NumberWithDot);

            // Change the visual representation of the marker number on the first and second numbering levels.
            myCustomList2.ListLevelFormats[0].NumberFormat = "%1.";
            myCustomList2.ListLevelFormats[0].NumberStyle = NumberStyle.UpperRoman;
            myCustomList2.ListLevelFormats[0].TrailingCharacter = ListTrailingCharacter.Tab;
            myCustomList2.ListLevelFormats[1].NumberFormat = "%1. %2.";
            myCustomList2.ListLevelFormats[1].NumberStyle = NumberStyle.LowerLetter;
            myCustomList2.ListLevelFormats[1].TrailingCharacter = ListTrailingCharacter.Space;

            dc.Styles.Add(myCustomList2);

            Paragraph p1 = new Paragraph(dc, "First paragraph.");
            p1.ListFormat.Style = myCustomList2;
            s.Blocks.Add(p1);

            Paragraph p2 = new Paragraph(dc, "Second paragraph.");
            p2.ListFormat.Style = myCustomList2;
            s.Blocks.Add(p2);

            Paragraph p21 = new Paragraph(dc, "Sub paragraph a.");
            p21.ListFormat.Style = myCustomList2;
            p21.ListFormat.ListLevelNumber = 1;
            s.Blocks.Add(p21);

            Paragraph p22 = new Paragraph(dc, "Sub paragraph b.");
            p22.ListFormat.Style = myCustomList2;
            p22.ListFormat.ListLevelNumber = 1;
            s.Blocks.Add(p22);

            // Save our document into DOCX file.
            dc.Save(documentPath, new DocxSaveOptions());

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(documentPath) { UseShellExecute = true });
        }
    }
}

Download

Imports SautinSoft.Document

Namespace Sample
    Friend Class Sample

        Shared Sub Main(ByVal args() As String)
            CustomizedLists()
        End Sub
        ''' Get your free 100-day key here:   
        ''' https://sautinsoft.com/start-for-free/
        ''' <summary>
        ''' How to create customized lists.
        ''' </summary>
        ''' <remarks>
        ''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/create-customized-list-in-docx-document-net-csharp-vb.php
        ''' </remarks>		
        Public Shared Sub CustomizedLists()
            Dim documentPath As String = "CustomizedLists.docx"

            ' Let's create a new document.
            Dim dc As New DocumentCore()

            ' Add a new section.
            Dim s As New Section(dc)
            dc.Sections.Add(s)

            ' Create a new list style based on the ListTemplateType.Bullet template.
            Dim myCustomList1 As New ListStyle("MyCustomList1", ListTemplateType.Bullet)

            ' Replace the standard bullet with the symbol ASTERISM (unicode 0x2042).
            myCustomList1.ListLevelFormats(0).NumberFormat = ChrW(&H2042).ToString()

            ' Change the character formatting.
            myCustomList1.ListLevelFormats(0).CharacterFormat.FontName = "Calibri"
            myCustomList1.ListLevelFormats(0).CharacterFormat.FontColor = Color.Blue
            dc.Styles.Add(myCustomList1)

            Dim a1 As New Paragraph(dc, "Asterism 1.")
            a1.ListFormat.Style = myCustomList1
            s.Blocks.Add(a1)

            Dim a2 As New Paragraph(dc, "Asterism 2.")
            a2.ListFormat.Style = myCustomList1
            a2.ParagraphFormat.SpaceAfter = 30
            s.Blocks.Add(a2)

            ' Create a new list style based on the ListTemplateType.NumberWithDot template.
            Dim myCustomList2 As New ListStyle("MyCustomList2", ListTemplateType.NumberWithDot)

            ' Change the visual representation of the marker number on the first and second numbering levels.
            myCustomList2.ListLevelFormats(0).NumberFormat = "%1."
            myCustomList2.ListLevelFormats(0).NumberStyle = NumberStyle.UpperRoman
            myCustomList2.ListLevelFormats(0).TrailingCharacter = ListTrailingCharacter.Tab
            myCustomList2.ListLevelFormats(1).NumberFormat = "%1. %2."
            myCustomList2.ListLevelFormats(1).NumberStyle = NumberStyle.LowerLetter
            myCustomList2.ListLevelFormats(1).TrailingCharacter = ListTrailingCharacter.Space

            dc.Styles.Add(myCustomList2)

            Dim p1 As New Paragraph(dc, "First paragraph.")
            p1.ListFormat.Style = myCustomList2
            s.Blocks.Add(p1)

            Dim p2 As New Paragraph(dc, "Second paragraph.")
            p2.ListFormat.Style = myCustomList2
            s.Blocks.Add(p2)

            Dim p21 As New Paragraph(dc, "Sub paragraph a.")
            p21.ListFormat.Style = myCustomList2
            p21.ListFormat.ListLevelNumber = 1
            s.Blocks.Add(p21)

            Dim p22 As New Paragraph(dc, "Sub paragraph b.")
            p22.ListFormat.Style = myCustomList2
            p22.ListFormat.ListLevelNumber = 1
            s.Blocks.Add(p22)

            ' Save our document into DOCX file.
            dc.Save(documentPath, New DocxSaveOptions())

            ' Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(documentPath) With {.UseShellExecute = True})
        End Sub
    End Class
End Namespace

Download


Если вам нужен пример кода или у вас есть вопрос: напишите нам по адресу support@sautinsoft.com или спросите в онлайн-чате (правый нижний угол этой страницы) или используйте форму ниже:



Вопросы и предложения всегда приветствуются!

Мы разрабатываем компоненты .Net с 2002 года. Мы знаем форматы PDF, DOCX, RTF, HTML, XLSX и Images. Если вам нужна помощь в создании, изменении или преобразовании документов в различных форматах, мы можем вам помочь. Мы напишем для вас любой пример кода абсолютно бесплатно.