Skip to main content

Lists

Lists

Use ordered lists for steps or rankings and unordered lists for related items. Create ordered and unordered lists in separate PDFs. This example follows ListsPdf.cs and demonstrates how a text element's CSS overrides document-wide styles.

Fluent recipe / 07ListsCreate styled ordered and unordered lists in separate PDFs.

What you’ll create​

  • An ordered list with three items numbered I, II, and III.
  • An unordered list with three disc-bulleted items.
  • Red, 16-point Times list text, with navy, 22-point text above each list.
  • Two files in your chosen output directory: ListsPdf_ordered_output.pdf and ListsPdf_unordered_output.pdf.
PDF output with the full-color DynamicPDF image.PDF output with the pale DynamicPDF background image.

Example code​

Add the following class to a console project that references the DynamicPDF Flow API. See the quick start for an overview. The class creates both list examples and saves each document to an existing output directory.

using System;
using System.IO;
using DynamicPDF.Flow;

namespace FluentExamples.Examples;

internal static class ListsPdf
{
public static void Run(string outputRoot)
{
Ordered(outputRoot);
Unordered(outputRoot);
}

public static void Ordered(string outputRoot)
{
var path = Path.Combine(
outputRoot, "ListsPdf_ordered_output.pdf");

DocumentBuilder.Create()
.Title("Ordered list")

.Style("text, ol { font-family: 'times'; "
+ "font-size: 16pt; color: red; }")
.Pages(page => page
.Text("My Ordered List", text => text
.Style("color: navy; font-size: 22pt;"))

.OrderedList(items => items
.ListStyleType(ListStyleType.UpperRoman)
.Items(
"Styled ordered item",
"Second styled item",
"Third styled item")))
.Save(path);

Console.WriteLine(
$"Created {Path.GetFullPath(path)}");
}

public static void Unordered(string outputRoot)
{
var path = Path.Combine(
outputRoot, "ListsPdf_unordered_output.pdf");

DocumentBuilder.Create()
.Title("My Unordered list")
.Style("text, ul { font-family: 'times'; "
+ "font-size: 16pt; color: red; }")
.Pages(page => page
.Text("Hello world!", text => text
.Style("color: navy; font-size: 22pt;"))
.UnorderedList(items => items
.ListStyleType(ListStyleType.Disc)
.Items(
"Styled unordered item",
"Second styled item",
"Third styled item")))
.Save(path);

Console.WriteLine(
$"Created {Path.GetFullPath(path)}");
}
}
Imports System
Imports System.IO
Imports DynamicPDF.Flow

Friend Module ListsPdf
Public Sub Run(outputRoot As String)
Ordered(outputRoot)
Unordered(outputRoot)
End Sub

Public Sub Ordered(outputRoot As String)
Dim outputPath = Path.Combine(
outputRoot, "ListsPdf_ordered_output.pdf")

DocumentBuilder.Create().
Title("Ordered list").
Style("text, ol { font-family: 'times'; " &
"font-size: 16pt; color: red; }").
Pages(Sub(page) page.
Text("My Ordered List", Sub(text) text.
Style("color: navy; font-size: 22pt;")).
OrderedList(Sub(items) items.
ListStyleType(ListStyleType.UpperRoman).
Items(
"Styled ordered item",
"Second styled item",
"Third styled item"))).
Save(outputPath)

Console.WriteLine(
$"Created {Path.GetFullPath(outputPath)}")
End Sub

Public Sub Unordered(outputRoot As String)
Dim outputPath = Path.Combine(
outputRoot, "ListsPdf_unordered_output.pdf")

DocumentBuilder.Create().
Title("My Unordered list").
Style("text, ul { font-family: 'times'; " &
"font-size: 16pt; color: red; }").
Pages(Sub(page) page.
Text("Hello world!", Sub(text) text.
Style("color: navy; font-size: 22pt;")).
UnorderedList(Sub(items) items.
ListStyleType(ListStyleType.Disc).
Items(
"Styled unordered item",
"Second styled item",
"Third styled item"))).
Save(outputPath)

Console.WriteLine(
$"Created {Path.GetFullPath(outputPath)}")
End Sub
End Module
note

Notice how the Text element's .Style(...) overrides the document-wide CSS: the text above each list is navy and 22 points, while the list text remains red and 16 points.

How it works​

1. Create the two documents​

Run calls Ordered and then Unordered. Each method starts its own document with DocumentBuilder.Create(). The ordered document's title is Ordered list; the unordered document's title is My Unordered list. These titles are metadata, separate from the visible text on each page.

2. Set document-wide CSS​

Each document calls .Style(...) before adding its page. The text, ol rule styles text and ordered lists; text, ul styles text and unordered lists. Both rules select Times, a 16-point font size, and red text.

3. Override the text element's style​

Ordered adds “My Ordered List” above its list. Unordered adds “Hello world!” above its list. Both text elements use .Style("color: navy; font-size: 22pt;") to override the document's color and font size for that element. The font family remains Times, and the list retains the document's red, 16-point styling.

4. Add the list items​

OrderedList uses .ListStyleType(ListStyleType.UpperRoman) to label the three items I, II, and III. UnorderedList uses .ListStyleType(ListStyleType.Disc) to give each item a filled circular bullet. Each .Items(...) call adds the supplied strings in order.

5. Save each PDF​

System.IO.Path.Combine joins outputRoot with ListsPdf_ordered_output.pdf or ListsPdf_unordered_output.pdf. Make sure the output directory exists. Each .Save(...) builds and saves its document through the fluent builder, overwriting an existing file with the same name.

After saving, Console.WriteLine prints the full output path.

Next steps​

tip

See AdvancedListPdf.cs for advanced list examples, including nested lists, custom bullets, styling, and pagination.

Change the item text or adjust the document and text-element CSS to see how the styles interact. Explore Create a PDF for another document-wide styling example, or browse all examples.