How To Create Lists In PDF Documents In C#

Introduction

Lists are a great way to organize information. Compared to chunks of text, lists are usually easier to read. Therefore, it is sometimes necessary to insert lists to PDF documents. For instance, to create a Table of Contents at the first page of your PDF.

In this article, you will learn how to programmatically create ordered lists, unordered lists, and nested lists in a PDF document in C# using Free Spire.PDF for .NET library.

  • Create an Ordered List in PDF
  • Create an Unordered List in PDF
  • Create an Unordered List with Image Bullets
  • Create a Nested Numbered List

Installation

The easiest way to install Free Spire.PDF for .NET is via NuGet. Create a .NET Framework application in your Visual Studio, search for “freespire.pdf” in NuGet Package Manager, and have it installed in seconds.

How to Create Lists in PDF Documents in C#

Or you can install it using Package Manager Console with the following line:

PM> Install-Package FreeSpire.PDF

Background

Free Spire.PDF provides the PdfSortedList class and the PdfList class to represent the ordered lists and unordered lists, respectively. To specify the list’s content, indent, font, marker style and other attributes, you can use the methods, properties, or events under these classes. The following table lists the core items and their definitions involved in this tutorial. Learning about these concepts can help you better understand the code examples provided below.

Member Description
PdfSortedList class Represents an ordered list in a PDF document.
PdfList class Represents an unordered list in a PDF document.
Brush property Gets or sets list brush.
Font property Gets or sets the list font.
Indent property Gets or sets the list indent.
TextIndent property Gets or sets the indent from the marker to the list item text.
Items property Gets items of the list.
Marker property Gets or sets the list marker.
Draw() method Draw list on the canvas of a PDF page at the specified location.
PdfOrderedMarker class Represents the marker style of an ordered list, such as numbers, letters, and roman numerals.
PdfMarker class Represents bullet style for an unordered list.

Create an Ordered List in PDF

An ordered list is a container which holds a sequence of objects. Each item in the list is marked with a number, a letter, or a roman numeral. To specify the marker style of an ordered list, use the PdfOrderedMarker class. The following gives you an example of creating a letter-ordered list in PDF in C#.

using System;
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Lists;
namespace CreateOrderedList {
    class Program {
        static void Main(string[] args) {
            //Create a PdfDocument object
            PdfDocument doc = new PdfDocument();
            //Set the margins
            PdfMargins margins = new PdfMargins(30);
            //Add a page
            PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, margins);
            //Create a brush
            PdfBrush brush = PdfBrushes.Black;
            //Create two fonts
            PdfFont titleFont = new PdfFont(PdfFontFamily.TimesRoman, 12 f, PdfFontStyle.Bold);
            PdfFont listFont = new PdfFont(PdfFontFamily.TimesRoman, 12 f, PdfFontStyle.Regular);
            //Create a maker for ordered list
            PdfOrderedMarker marker = new PdfOrderedMarker(PdfNumberStyle.UpperLatin, listFont);
            //Specify the initial coordinate
            float x = 0;
            float y = 0;
            //Draw title
            String title = "Top 5 highest Mountains in the World:";
            page.Canvas.DrawString(title, titleFont, brush, x, y);
            y = y + (float) titleFont.MeasureString(title).Height;
            y = y + 5;
            //Create a numbered list
            String listContent = "Mount Everest\n" + "K-2\n" + "Kangchenjunga\n" + "Lhotse\n" + "Makalu";
            PdfSortedList list = new PdfSortedList(listContent);
            //Set the font, indent, text indent, brush of the list
            list.Font = listFont;
            list.Indent = 2;
            list.TextIndent = 4;
            list.Brush = brush;
            list.Marker = marker;
            //Draw list on the page at the specified location
            list.Draw(page, 0, y);
            //Save to file
            doc.SaveToFile("OrderedList.pdf");
            System.Diagnostics.Process.Start("OrderedList.pdf");
        }
    }
}

Output

How to Create Lists in PDF Documents in C#

Create an Unordered List in PDF

An unordered list, also named bulleted list, is a collection of related items that have no special order or sequence. Each item in the list is marked with a bullet. The marker style can be defined by the PdfMarker class. The following provides an example of creating a bulleted list in PDF in C#.

using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Lists;
using System;

namespace CreateBulletedList
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            //Set the margin
            PdfMargins margin = new PdfMargins(30);

            //Add a page
            PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, margin);

            //Create three fonts
            PdfFont titleFont = new PdfFont(PdfFontFamily.TimesRoman, 12f, PdfFontStyle.Bold);
            PdfFont listFont = new PdfFont(PdfFontFamily.TimesRoman, 12f, PdfFontStyle.Regular);
            PdfFont markerFont = new PdfFont(PdfFontFamily.TimesRoman, 6f, PdfFontStyle.Regular);

            //Create a brush
            PdfBrush brush = PdfBrushes.Black;

            //Specify the initial coordinate
            float x = 0;
            float y = 0;

            //Draw title
            String title = "Daily To-Do List:";
            page.Canvas.DrawString(title, titleFont, brush, x, y);
            y = y + (float)titleFont.MeasureString(title).Height;
            y = y + 5;

            //Specify the marker style
            PdfMarker marker = new PdfMarker(PdfUnorderedMarkerStyle.Square);
            marker.Font = markerFont;

            //Create a bullet list
            String listContent = "Exercise\n"
                    + "Take vitamins\n"
                    + "Drink 6-8 glasses of water\n"
                    + "Make healthy food choices\n"
                    + "Get 8 hours of sleep"; 
            PdfList list = new PdfList(listContent);

            //Set the font, indent, text indent, brush, maker of the list
            list.Font = listFont;
            list.Indent = 2;
            list.TextIndent = 4;
            list.Brush = brush;
            list.Marker = marker;

            //Draw list on a page at the specified location
            list.Draw(page, 0, y);

            //Save to file
            doc.SaveToFile("BulletedList.pdf");
        }
    }
}

Output

How to Create Lists in PDF Documents in C#

Create an Unordered List with Image Bullets

In addition to symbols, the bullet points of an unordered list can be also a picture. The following code snippet demonstrates how to create an unordered list with image bullets in C#.

using System;
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Lists;
namespace CustomizeBulletPointsWithImage {
    class Program {
        static void Main(string[] args) {
            //Create a PdfDocument object
            PdfDocument doc = new PdfDocument();
            //Set the margin
            PdfMargins margin = new PdfMargins(30);
            //Add a page
            PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, margin);
            //Create three types of font
            PdfFont titleFont = new PdfFont(PdfFontFamily.TimesRoman, 12 f, PdfFontStyle.Bold);
            PdfFont listFont = new PdfFont(PdfFontFamily.TimesRoman, 12 f, PdfFontStyle.Regular);
            //Create a brush
            PdfBrush brush = PdfBrushes.Black;
            //Specify the initial coordinate
            float x = 0;
            float y = 0;
            //Draw title
            String title = "Healthy Vegetables that You Should Eat Often:";
            page.Canvas.DrawString(title, titleFont, brush, x, y);
            y = y + (float) titleFont.MeasureString(title).Height;
            y = y + 5;
            //Specify the marker style to image
            PdfMarker marker = new PdfMarker(PdfUnorderedMarkerStyle.CustomImage);
            //Set the image for the marker
            marker.Image = PdfImage.FromFile(@ "C:\Users\Administrator\Desktop\carrot.png");
            //Create a bullet list
            String listContent = "Spinach\n" + "Carrots\n" + "Broccoli\n" + "Garlic\n" + "Brussels sprouts";
            PdfList list = new PdfList(listContent);
            //Set the font, indent, text indent, brush, maker of the list
            list.Font = listFont;
            list.Indent = 2;
            list.TextIndent = 4;
            list.Brush = brush;
            list.Marker = marker;
            //Draw list on a page at the specified location
            list.Draw(page, 0, y);
            //Save to file
            doc.SaveToFile("ImageBullets.pdf");
        }
    }
}

Output

How to Create Lists in PDF Documents in C#

Create a Nested Numbered List

A nested list is a list that contains at least one sub list. Nested lists are used to present data in hierarchical structures. The following is an example of creating a nested numbered list in a PDF document in C#.

using System;
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;
using Spire.Pdf.Lists;
namespace CreateMultiLevelLists {
    class Program {
        static void Main(string[] args) {
            //Create a PdfDocument object
            PdfDocument doc = new PdfDocument();
            //Set the margin
            PdfMargins margin = new PdfMargins(30);
            //Add a page
            PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, margin);
            //Specify the initial coordinate
            float x = 0;
            float y = 0;
            //Create two brushes
            PdfBrush blackBrush = PdfBrushes.Black;
            PdfBrush purpleBrush = PdfBrushes.Purple;
            //Create two fonts
            PdfFont titleFont = new PdfFont(PdfFontFamily.TimesRoman, 12 f, PdfFontStyle.Bold);
            PdfFont listFont = new PdfFont(PdfFontFamily.TimesRoman, 12 f, PdfFontStyle.Regular);
            //Create a maker for ordered list
            PdfOrderedMarker marker = new PdfOrderedMarker(PdfNumberStyle.Numeric, listFont);
            //Draw title
            String title = "Below is a Nested Numbered List:";
            page.Canvas.DrawString(title, titleFont, blackBrush, x, y);
            y = y + (float) titleFont.MeasureString(title).Height;
            y = y + 5;
            //Create a parent list
            String parentListContent = "Parent Item 1\n" + "Parent Item 2";
            PdfSortedList parentList = new PdfSortedList(parentListContent);
            parentList.Font = listFont;
            parentList.Indent = 2;
            parentList.Brush = purpleBrush;
            parentList.Marker = marker;
            //Create a sub list - "subList_1"
            String subListContent_1 = "Child Item 1-1\n" + "Child Item 1-2\n" + "Child Item 1-3\n" + "Child Item 1-4";
            PdfSortedList subList_1 = new PdfSortedList(subListContent_1);
            subList_1.Indent = 10;
            subList_1.Font = listFont;
            subList_1.Brush = blackBrush;
            subList_1.Marker = marker;
            subList_1.MarkerHierarchy = true;
            //Create another sub list -"subList_2"
            String subListContent_2 = "Child Item 2-1\n" + "Child Item 2-2\n" + "Child Item 2-3";
            PdfSortedList subList_2 = new PdfSortedList(subListContent_2);
            subList_2.Indent = 10;
            subList_2.Font = listFont;
            subList_2.Brush = blackBrush;
            subList_2.Marker = marker;
            subList_2.MarkerHierarchy = true;
            //Create a sub-sub list - "subList_1"
            String subSubListContent_1 = "Child Item 1-1-1\n" + "Child Item 1-1-2";
            PdfSortedList subSubList = new PdfSortedList(subSubListContent_1);
            subSubList.Indent = 18;
            subSubList.Font = listFont;
            subSubList.Brush = blackBrush;
            subSubList.Marker = marker;
            subSubList.MarkerHierarchy = true;
            //Set subList_1 as sub list of the first item of parent list
            PdfListItem item_1 = parentList.Items[0];
            item_1.SubList = subList_1;
            //Set subList_2 as sub list of the second item of parent list
            PdfListItem item_2 = parentList.Items[1];
            item_2.SubList = subList_2;
            //Set subSubList as sub list of the first item of subList_1
            PdfListItem item_1_1 = subList_1.Items[0];
            item_1_1.SubList = subSubList;
            //Draw parent list
            parentList.Draw(page, x, y);
            //Save to file
            doc.SaveToFile("MultiLevelList.pdf");
        }
    }
}

Output

How to Create Lists in PDF Documents in C#