List (2)
A simple PDF List is always one level list which displays briefly the content items of a PDF file. When the PDF is very huge and complex, people need more details, creating multiple level list in PDF becomes pretty necessary. Here a solution will be introduced to you about how to create multiple level list in PDF via a .NET PDF component Spire.PDF for .NET in C#, VB.NET. First let us see the effect of PDF multiple level list as below picture:
Here we can download Spire.PDF for .NET. After adding Spire.Pdf dll in project, please view below key code to start PDF list task.
In this solution, apart from creating PDF list, we also can set list format, position and layout. This class Graphics.PdfTrueTypeFont which is provided by Spire.PDF enables to set list font and size. From above picture, we can view that the first level list is red. It can be set in this class Spire.Pdf.Graphics.PdfLinearGradientBrush. In order to let users themselves to set PDF list style according to their needs, Spire.Pdf.Lists.PdfOrderedMarker can help us to set label style by this enum Spire.Pdf.PdfNumberStyle. Here there are six styles available: LowerLatin, LowerRoman, None, Numeric, UpperLatin and UpperRoman. In below code, I only show a part of code:
//Set PDF list label style PdfOrderedMarker marker1 = new PdfOrderedMarker(PdfNumberStyle.LowerRoman, new PdfFont(PdfFontFamily.Helvetica, 12f)); PdfOrderedMarker marker2 = new PdfOrderedMarker(PdfNumberStyle.Numeric, new PdfFont(PdfFontFamily.Helvetica, 10f));
'Set PDF list label style Dim marker1 As New PdfOrderedMarker(PdfNumberStyle.LowerRoman, New PdfFont(PdfFontFamily.Helvetica, 12F)) Dim marker2 As New PdfOrderedMarker(PdfNumberStyle.Numeric, New PdfFont(PdfFontFamily.Helvetica, 10F))
After setting the PDF list format, we can search data in database by System.Data.Oledb.OleDbCommand and then, create multi-level list by two classes in Spire.Pdf: Spire.Pdf.Lists. PdfListItem and Spire.Pdf.Lists. PdfSortedList. The former class represents list item while the later represents the ordered list.
//Create multi-level list in PDF PdfSortedList vendorList = new PdfSortedList(font2); vendorList.Indent = 0; vendorList.TextIndent = 5; vendorList.Brush = brush2; vendorList.Marker = marker1; using (OleDbConnection conn = new OleDbConnection()) { conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=demo.mdb"; OleDbCommand command = new OleDbCommand(); command.CommandText = " select VendorNo, VendorName from vendors "; command.Connection = conn; OleDbCommand command2 = new OleDbCommand(); command2.CommandText = " select Description from parts where VendorNo = @VendorNo"; command2.Connection = conn; OleDbParameter param = new OleDbParameter("@VendorNo", OleDbType.Double); command2.Parameters.Add(param); conn.Open(); OleDbDataReader reader = command.ExecuteReader(); while (reader.Read()) { double id = reader.GetDouble(0); PdfListItem item = vendorList.Items.Add(reader.GetString(1)); PdfSortedList subList = new PdfSortedList(font3); subList.Marker = marker2; subList.Brush = brush3; item.SubList = subList; subList.TextIndent = 20; command2.Parameters[0].Value = id; using (OleDbDataReader reader2 = command2.ExecuteReader()) { while (reader2.Read()) { subList.Items.Add(reader2.GetString(0)); } } String maxNumberLabel = Convert.ToString(subList.Items.Count); subList.Indent = 30 - font3.MeasureString(maxNumberLabel).Width; } }
'Create multi-level list in PDF Dim vendorList As New PdfSortedList(font2) vendorList.Indent = 0 vendorList.TextIndent = 5 vendorList.Brush = brush2 vendorList.Marker = marker1 Using conn As New OleDbConnection() conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=demo.mdb" Dim command As New OleDbCommand() command.CommandText = " select VendorNo, VendorName from vendors " command.Connection = conn Dim command2 As New OleDbCommand() command2.CommandText = " select Description from parts where VendorNo = @VendorNo" command2.Connection = conn Dim param As New OleDbParameter("@VendorNo", OleDbType.[Double]) command2.Parameters.Add(param) conn.Open() Dim reader As OleDbDataReader = command.ExecuteReader() While reader.Read() Dim id As Double = reader.GetDouble(0) Dim item As PdfListItem = vendorList.Items.Add(reader.GetString(1)) Dim subList As New PdfSortedList(font3) subList.Marker = marker2 subList.Brush = brush3 item.SubList = subList subList.TextIndent = 20 command2.Parameters(0).Value = id Using reader2 As OleDbDataReader = command2.ExecuteReader() While reader2.Read() subList.Items.Add(reader2.GetString(0)) End While End Using Dim maxNumberLabel As [String] = Convert.ToString(subList.Items.Count) subList.Indent = 30 - font3.MeasureString(maxNumberLabel).Width End While End Using
Finally, if we want to make sure that PDF multi-level list fits the page perfectly, we need to set the PDF text layout. When doing this you can call this method: Spire.Pdf.Graphics.PdfLayoutResult.Draw(PdfPageBase page, PointF location, PdfTextLayout format); there are three parameters passed, one is to draw text in PDF page, another is to set text location, and the last one is to set text layout.
//Set PDF Layout and position PdfTextLayout textLayout = new PdfTextLayout(); textLayout.Break = PdfLayoutBreakType.FitPage; textLayout.Layout = PdfLayoutType.Paginate; vendorList.Draw(page, new PointF(0, y), textLayout);
'Set PDF Layout and position Dim textLayout As New PdfTextLayout() textLayout.Break = PdfLayoutBreakType.FitPage textLayout.Layout = PdfLayoutType.Paginate vendorList.Draw(page, New PointF(0, y), textLayout)
Spire.PDF for .NET is a .NET PDF component that enables you to create, read, edit and handle PDF files in C#, VB.NET.
A list is a collection of related values written one after another. Lists are the best tool to make important information stand out on the page, show the steps in a process, or present an overview to the readers. In this article, you will learn how to create ordered or unordered lists in a PDF document in C# and VB.NET using Spire.PDF for .NET.
- Create an Ordered List in PDF
- Create an Unordered List with Symbol Bullets in PDF
- Create an Unordered List with Image Bullets in PDF
- Create a Nested Numbered List in PDF
Spire.PDF provides the PdfSortedList class and the PdfList class to represent the ordered lists and unordered lists, respectively. To set the list's content, indent, font, marker style and other attributes, use the methods, properties, or events under these two classes. The following table lists some of the core items involved in this tutorial.
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 a list's brush. |
Font property | Gets or sets a list's font. |
Indent property | Gets or sets a list's indent. |
TextIndent property | Gets or sets the indent from the marker to the list item text. |
Items property | Gets items of a list. |
Marker property | Gets or sets the marker of a list. |
Draw() method | Draw list on the canvas of a 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. |
Install Spire.PDF for .NET
To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.PDF
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. The following are the step to create an ordered list in PDF using Spire.PDF for .NET.
- Create a PdfDocument object.
- Add a page to it using PdfDocument.Pages.Add() method.
- Create PdfBrush and PdfFont objects for the list.
- Create a PdfOrderedMarker object, specifying the marker style.
- Specify the list content with a string, and create an object of PdfSortedList class based on the string.
- Set the font, indent, brush and marker of the list though the properties under the PdfSortedList object.
- Draw the list on the page at the specified location using PdfSortedList.Draw() method.
- Save the document to another file using PdfDocument.SaveToFile() method.
- C#
- VB.NET
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 fonts PdfFont titleFont = new PdfFont(PdfFontFamily.TimesRoman, 12f, PdfFontStyle.Bold); PdfFont listFont = new PdfFont(PdfFontFamily.TimesRoman, 12f, PdfFontStyle.Regular); //Create a maker for ordered list PdfOrderedMarker marker = new PdfOrderedMarker(PdfNumberStyle.LowerLatin, listFont); //Specify the initial coordinate float x = 10; float y = 20; //Draw title String title = "Required Web Development Skills:"; page.Canvas.DrawString(title, titleFont, brush, x, y); y = y + (float)titleFont.MeasureString(title).Height; y = y + 5; //Create a numbered list String listContent = "Command-line Unix\n" + "Vim\n" + "HTML\n" + "CSS\n" + "Python\n" + "JavaScript\n" + "SQL"; 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, x, y); //Save to file doc.SaveToFile("OrderedList.pdf"); } } }
Create an Unordered List with Symbol Bullets 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 following are the step to create an unordered list in PDF using Spire.PDF for .NET.
- Create a PdfDocument object.
- Add a page to it using PdfDocument.Pages.Add() method.
- Create PdfBrush and PdfFont objects for the list.
- Create a PdfMarker object, specifying the marker style.
- Specify the list content with a string, and create an object of PdfList class based on the string.
- Set the font, indent, brush and marker of the list though the properties under the PdfList object.
- Draw the list on the page at the specified location using PdfList.Draw() method.
- Save the document to another file using PdfDocument.SaveToFile() method.
- C#
- VB.NET
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 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 = 10; float y = 20; //Draw title String title = "Computer Science Subjects:"; 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.Asterisk); marker.Font = markerFont; //Create an unordered list String listContent = "Data Structure\n" + "Algorithm\n" + "Computer Networks\n" + "Operating System\n" + "Theory of Computations\n" + "C Programming\n" +"Computer Organization and Architecture"; 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, x, y); //Save to file doc.SaveToFile("UnorderedList.pdf"); } } }
Create an Unordered List with Image Bullets in PDF
In addition to symbols, the bullet points of an unordered list can be also a picture. The steps to create an unordered list with images bullets are as follows.
- Create a PdfDocument object.
- Add a page to it using PdfDocument.Pages.Add() method.
- Create PdfBrush and PdfFont objects for the list.
- Create a PdfMarker object, setting the marker style as CustomImage.
- Set an image as the mark through PdfMarker.Image property.
- Specify the list content with a string, and create an object of PdfList class based on the string.
- Set the font, indent, brush and marker of the list though the properties under the PdfList object.
- Draw the list on the page at the specified location using PdfList.Draw() method.
- Save the document to another file using PdfDocument.SaveToFile() method.
- C#
- VB.NET
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 fonts PdfFont titleFont = new PdfFont(PdfFontFamily.TimesRoman, 12f, PdfFontStyle.Bold); PdfFont listFont = new PdfFont(PdfFontFamily.TimesRoman, 12f, PdfFontStyle.Regular); //Create a brush PdfBrush brush = PdfBrushes.Black; //Specify the initial coordinate float x = 10; float y = 20; //Draw title String title = "Project Task 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 to image PdfMarker marker = new PdfMarker(PdfUnorderedMarkerStyle.CustomImage); //Set the image for the marker marker.Image = PdfImage.FromFile(@"C:\Users\Administrator\Desktop\checkmark.jpg"); //Create an unordered list String listContent = "Define projects and tasks you're working on\n" + "Assign people to tasks\n" + "Define the priority levels of your tasks\n" + "Keep track of the progress status of your tasks\n" + "Mark tasks as done when completed"; 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, x, y); //Save to file doc.SaveToFile("ImageBullets.pdf"); } } }
Create a Nested Numbered List in PDF
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 are the steps to create a nested numbered list in PDF using Spire.PDF for .NET.
- Create a PdfDocument object.
- Add a page to it using PdfDocument.Pages.Add() method.
- Create a PdfOrderedMarker object, specifying the marker style as Numeric.
- Specify the list content with a string, and create a parent list based on the string. And then set the font, indent, brush and marker of the list though the properties under the PdfSortedList object.
- Repeat the above step to create sub lists and sub-sub lists.
- Get the specific item of the parent list through PdfSortedList.Items[] property, and add a list to it as its sub list through PdfListItem.Sublist property.
- Draw the list on the page at the specified location using PdfSortedList.Draw() method.
- Save the document to another file using PdfDocument.SaveToFile() method.
- C#
- VB.NET
using System; using Spire.Pdf; using Spire.Pdf.Graphics; 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 = 10; float y = 20; //Create two brushes PdfBrush blackBrush = PdfBrushes.Black; PdfBrush purpleBrush = PdfBrushes.Purple; //Create two fonts PdfFont titleFont = new PdfFont(PdfFontFamily.TimesRoman, 12f, PdfFontStyle.Bold); PdfFont listFont = new PdfFont(PdfFontFamily.TimesRoman, 12f, 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 = "Sub Item 1\n" + "Sub Item 2\n" + "Sub Item 3\n" + "Sub Item 4"; PdfSortedList subList_1 = new PdfSortedList(subListContent_1); subList_1.Indent = 12; 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 = "Sub Item 1\n" + "Sub Item 2\n" + "Sub Item 3"; PdfSortedList subList_2 = new PdfSortedList(subListContent_2); subList_2.Indent = 12; 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 = "Sub Sub Item 1\n" + "Sub Sub Item 2"; PdfSortedList subSubList = new PdfSortedList(subSubListContent_1); subSubList.Indent = 20; 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"); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.