Search code examples
c#pdftextitexttableofcontents

Create indents and numeration in Table of Content(itextsharp)


I have a question about TOC. How can I create TOC With indents and numeration?

Now I have TOC without it(just list). I create it using Chunk and Paragraph. What should I use for creating TOC with it? Maybe should I use List and add to document or not?

Here I'm creating TOC:

private int CreateTOC(XmlNode xmlNode, Document doc, PdfWriter writer, int number)
    {
        var toc = ev.GetTOC();
        KeyValuePair<string, int> value;

        Chunk dottedLine = new Chunk(new iTextSharp.text.pdf.draw.DottedLineSeparator());

        for (int i = 0; i < xmlNode.ChildNodes.Count; i++)
        {
            var text = xmlNode.ChildNodes[i].Attributes["text"].Value;
            value = toc[text];
            var dest = value.Key;
            var page = value.Value;

            var c = new Chunk((i+1).ToString()+ ". " + text, font);
            c.SetAction(PdfAction.GotoLocalPage(dest, false));

            var p = new Paragraph(c);
            p.Add(dottedLine);

            c = new Chunk(page.ToString(), font);
            c.SetAction(PdfAction.GotoLocalPage(dest, false));
            p.Add(c);
            doc.Add(p);


            CreateTOC(xmlNode.ChildNodes[i], doc, writer, i+1);
        }
        return writer.PageNumber;
    }

And I get list with reference to chapters in content. But I need the following:

1. chapter1-------------------1page
  1.1 subchupter1-------------2page
  1.2 subchupter2-------------2page
  1.3 subchupter3-------------3page
2. chupter2-------------------4page
  2.1 subchupter4-------------4page
  2.3 subchupter4-------------4page
     2.3.1 subsubchupter------5page
     ...
     ...
     ...

How can I fix it?

Thank you!


Solution

  • Introduce a level and multiply that level with an indentation value. Use that value as the value for IndentationLeft:

    private int CreateTOC(XmlNode xmlNode, Document doc, PdfWriter writer, int number, int level) {
        var toc = ev.GetTOC();
        KeyValuePair<string, int> value;
    
        Chunk dottedLine = new Chunk(new iTextSharp.text.pdf.draw.DottedLineSeparator());
    
        for (int i = 0; i < xmlNode.ChildNodes.Count; i++)
        {
            var text = xmlNode.ChildNodes[i].Attributes["text"].Value;
            value = toc[text];
            var dest = value.Key;
            var page = value.Value;
    
            var c = new Chunk((i+1).ToString()+ ". " + text, font);
            c.SetAction(PdfAction.GotoLocalPage(dest, false));
    
            var p = new Paragraph(c);
            p.IndentationLeft = 10 * level;
            p.Add(dottedLine);
    
            c = new Chunk(page.ToString(), font);
            c.SetAction(PdfAction.GotoLocalPage(dest, false));
            p.Add(c);
            doc.Add(p);
    
    
            CreateTOC(xmlNode.ChildNodes[i], doc, writer, i+1, level + 1);
        }
        return writer.PageNumber;
    }
    

    Use 0 for the level when you first call CreateToc().