How to quickly create a PDF document with hierarchical Multilevel List in C#
Tags: csharp, programming, tutorial, dotnet
Published: 2021-10-17 19:38:17

If your C# application has to generate a PDF report with Multilevel List, there is a way to do it quick and easy with [PDFFlow](https://go.pdfflow.io/PDFFlowLibInfo) library.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fa4ap2ephi0pmdrr6g9r.png)
PDF document consists of sections. A section may have as many multilevel lists as you need. Creating a list with PDFFlow is only a few lines of code:

1. First you need to create DocumentBuilder.
2. Then add Section to it.
3. Now add as many Paragraphs to Section, as many list items you need.
4. Make each Paragraph a list item, set level and style if needed.

You can make a paragraph an item of a ***numbered*** multilevel list using the *ParagraphBuilder.SetListNumbered* method. For each item, you can specify the numbering style, the level of the list, the left indent, and enable the hierarchic numeration is necessary. Items of a numbered list are numbered automatically on each level. The available numbering styles are: Arabic numbers, lower and upper Roman numbers, lower and upper Latin letters, lower and upper Cyrillic letters.

You can make a paragraph an item of a ***bulleted*** multilevel list using the *ParagraphBuilder.SetListBulleted* method. For each item, you can specify the bullet symbol, the level of the list, and the left indent. The available bullet symbols are a *bullet* and a *dash*.

The default level left indent for a multilevel list is 20f, the default numbering style of a numbered list is Arabic, the default bullet symbol is Bullet. You can configure the formatting settings of list items directly or apply a style to them using methods of ParagraphBuilder. You can also set a style for all lists in the section using the *SectionBuilder.SetListStyle* method. In this case, you can use the *ParagraphBuilder.SetList* method to quickly make the necessary paragraphs list items and the set style will be applied to all list items in the section automatically.

Once a paragraph which is not an item of a multilevel list is met, the multilevel list is considered completed. When later another paragraph is made an item of a multilevel list, it becomes the first item of a new multilevel list, and its automatic numeration will start from the beginning.

**Examples**

&#x200B;

**Example 1. 	Create a numbered list**
```c#
                var latin = NumerationStyle.LowerLatin;
                DocumentBuilder.New()
                .AddSection()
                    .AddParagraph("Level 0, Item 1").SetListNumbered()
                .ToSection()
                    .AddParagraph("Level 1, Item a").SetListNumbered(latin, 1)
                .ToSection()
                    .AddParagraph("Level 1, Item b").SetListNumbered(latin, 1)
                .ToSection()
                    .AddParagraph("Level 0, Item 2").SetListNumbered()
                .ToSection()
                    .AddParagraph("Level 0, Item 3").SetListNumbered()
                .ToDocument()
                    .Build("Result.pdf");   
```
**The above code will generate the following:**
![Numbered list](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6blldp88albk38otx07m.png)
&#x200B;

**Example 2. 	Create a bulleted list**
```c#
                DocumentBuilder.New()
                .AddSection()
                .AddParagraph("Level 0, Item 1")
                    .SetFontColor(Color.Red)
                    .SetListBulleted().ToSection()
                .AddParagraph("Level 1, Item 1")
                    .SetListBulleted(ListBullet.Bullet, 1).ToSection()
                .AddParagraph("Level 1, Item 2")
                    .SetListBulleted(ListBullet.Bullet, 1).ToSection()
                .AddParagraph("Level 2, Item 1")
                    .SetListBulleted(ListBullet.Dash, 2).ToSection()
                .AddParagraph("Level 2, Item 2")
                    .SetListBulleted(ListBullet.Dash, 2).ToSection()
                .AddParagraph("Level 0, Item 2")
                    .SetFontColor(Color.Red)
                    .SetListBulleted().ToSection()
                .AddParagraph("Level 0, Item 3")
                    .SetFontColor(Color.Red)
                    .SetListBulleted()
                .ToDocument()
                    .Build("Result.pdf");
```
**The above code will generate the following:**
![Bulleted list](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lkfff4k42m1ed0kesvga.png)
&#x200B;

**Example 3. 	Set level indent values**
```c#
                DocumentBuilder.New()
                .AddSection()
                    .AddParagraph("Level 0, Item 1. Shifted by 5 points")
                        .SetListBulleted(ListBullet.Bullet, 0, 5).ToSection()
                    .AddParagraph("Level 1, Item 1. Shifted by 15 points")
                        .SetListBulleted(ListBullet.Bullet, 1, 15).ToSection()
                    .AddParagraph("Level 1, Item 2. Shifted by 15 points")
                        .SetListBulleted(ListBullet.Bullet, 1, 15).ToSection()
                    .AddParagraph("Level 2, Item 1. Shifted by 20 points")
                        .SetListBulleted(ListBullet.Dash, 2, 20).ToSection()
                    .AddParagraph("Level 2, Item 2. Shifted by 20 points")
                        .SetListBulleted(ListBullet.Dash, 2, 20).ToDocument()
                .Build("Result.pdf"); 
```
**The above code will generate the following:**
![Left indent](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b6f1rhlphkjnpqa2c404.png)
&#x200B;

**Example 4. 	Set hierarchic numeration**
```c#
                var latin = NumerationStyle.LowerLatin;
                var arabic = NumerationStyle.Arabic;
                DocumentBuilder.New()
                .AddSection()
                    .AddParagraph("Level 0, Item 1")
                        .SetListNumbered().ToSection()
                    .AddParagraph("Level 1, Item a")
                        .SetListNumbered(latin, 1, hierarchicNumeration: true).ToSection()
                    .AddParagraph("Level 1, Item b")
                        .SetListNumbered(latin, 1, hierarchicNumeration: true).ToSection()
                    .AddParagraph("Level 0, Item 2")
                        .SetListNumbered().ToSection()
                    .AddParagraph("Level 1, Item 1")
                        .SetListNumbered(arabic, 1, hierarchicNumeration: true).ToSection()
                    .AddParagraph("Level 1, Item 2")
                        .SetListNumbered(arabic, 1, hierarchicNumeration: true).ToSection()
                    .AddParagraph("Level 0, Item 3")
                        .SetListNumbered().ToSection()
                    .AddParagraph("Level 1, Item 1")
                        .SetListNumbered(arabic, 1, hierarchicNumeration: true).ToSection()
                    .AddParagraph("Level 1, Item 2")
                        .SetListNumbered(arabic, 1, hierarchicNumeration: true).ToSection()
                    .AddParagraph("Level 2, Item 1")
                        .SetListNumbered(arabic, 2, hierarchicNumeration: true).ToSection()
                    .AddParagraph("Level 2, Item 2")
                        .SetListNumbered(arabic, 2, hierarchicNumeration: true).ToSection()
                    .AddParagraph("Level 1, Item 3")
                        .SetListNumbered(arabic, 1, hierarchicNumeration: true).ToDocument()
                    .Build("Result.pdf");
```
**The above code will generate the following:**
![Hierarchic numeration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jbamih0p5uwptkn35vqv.png)
&#x200B;

**Example 5. 	Add a list to a table**
```c#
                DocumentBuilder.New()
                .AddSection()
                    .AddTable()
                        .AddColumnToTable().AddColumnToTable()
                        .AddRow()
                            .AddCellToRow("Cell1")
                            .AddCell()
                                .AddParagraphToCell("List 1:")
                                .AddParagraph("Item 1").SetListBulleted().ToCell()
                                .AddParagraph("Item 2").SetListBulleted().ToCell()
                                .AddParagraph("Item 3").SetListBulleted().ToCell()
                                .AddParagraphToCell("List 2:")
                                .AddParagraph("Item 1").SetListNumbered().ToCell()
                                .AddParagraph("Item 2").SetListNumbered().ToCell()
                                .AddParagraph("Item 3").SetListNumbered()
                .ToDocument().Build("Result.pdf");  
```
**The above code will generate the following:**
![List in a table](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/itrwe5t740hxfv0n832v.png)
&#x200B;

To compile the above examples you need to add **Gehtsoft.PDFFlowLib** package from **nuget.org** to your project: [https://www.nuget.org/packages/Gehtsoft.PDFFlowLib](https://go.pdfflow.io/PDFFlowLibNugetPackage).

For the installation instructions please read “Installing PDFFlow Library” article: [https://go.pdfflow.io/InstallingPDFFlowLib](https://go.pdfflow.io/InstallingPDFFlowLib).

The **PDFFlow** library is supplied with **source code samples** of real business documents accompanied by descriptive articles: [https://go.pdfflow.io/PDFFlowLibExamples](https://go.pdfflow.io/PDFFlowLibExamples).

There are also Tutorials with many working code examples that can be used as is or modified: [https://go.pdfflow.io/TutorialsPDFFlowLib](https://go.pdfflow.io/TutorialsPDFFlowLib).

Here is a source code and descriptive article of **Warehouse Shipment Report**, using Multilevel List with hierarchic numeration, barcodes, different fonts, repeating areas and other useful features: [GitHub](https://go.pdfflow.io/ExampleWarehouseShipmentReport).

Hope, this article will help you to create complex PDF documents fast and efficiently.