Question

Creating a nested list within iTextSharp is simple

var list = new List(true);
list.add("Something here");
list.add("Something else here");
var nestedList = new List(true);
nestedList.add("Some other value");
list.add(nestedList);
document.add(list); // assuming here of course you have 
                    // created an instance of Document()!

will produce something really basic such as

  1. Something here
  2. Something else here
    1. Some other value

I want to create something a little more complicated; I am creating a document which has clauses and sub-clauses so want to list each list item like this:

1. Parent list item
1.1 Something here
1.2 Something else here

but I cannot see anywhere within the API where this is possible. The only way I can think to do this would be to use Paragraph() but has anyone else come across a more elegant solution to this?

Thanks

Was it helpful?

Solution

The only way I can see to do this is to manually keep track of the parent nesting and use the PreSymbol property to set it.

As follows:

                List list = new List(List.ORDERED, 20f);
                list.IndentationLeft = 20f;

                // add sublist
                List subList = new List(List.ORDERED);
                subList.PreSymbol = string.Format("{0}.", i);
                subList.Add("Something here");
                subList.Add("Something else here");

                list.Add(subList);

                doc.Add(list);

This would result in:

Screen grab

OTHER TIPS

As I understood from the comments, you need automatic hierarchical numeration of multilevel list in PDF. iTextSharp doesn't calculate level's numeration automatically, but I found the library, which does. It's called PDFFlow. You only need to set level to every item and set hierarchical numeration to "true":

using Gehtsoft.PDFFlow.Builder;
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:

Multilevel list with hierarchic numeration in PDF.

Hope, this will help you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top