Search code examples
c#itext

iTextSharp - Multiple levels within List()


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


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