1


i am trying to create a vb application which creates pdf documents by taking xmls as input.I want to get the table of contents for the pdf being created in the following format

heading1 ----------------page number  
  heading2---------------page number  
heading3-----------------page number  

using the chapters and section functions of itextsharp , all i can get is

heading1  
  heading2  
heading3  

Can anyone help me on getting the page numbers beside the appropriate entry..!??

Thanks,
Aditya

Mark Storer
  • 15,672
  • 3
  • 42
  • 80
aditya vaka
  • 11
  • 1
  • 2

2 Answers2

2

Here is the code in C# to further explain what Mark Storer provided above.

This snippet assumes that you are looping over the xmlHeaders to build the table of contents section:

        PdfContentByte cb = writer.DirectContent;
        MyCustomPageEventHandler pageEventHandler

        foreach (string headerStr in xmlHeaders)
        {
            PdfTemplate currChapTemplate = cb.CreateTemplate(50, 50);


            Paragraph titlePhrase = new Paragraph();
            titlePhrase.Add(headerStr);
            titlePhrase.IndentationLeft = 150f;
            pdfDoc.Add(titlePhrase);

            float curY = writer.GetVerticalPosition(false);
            float x = 450;
            //here we add the template to the pdf content byte
            cb.AddTemplate(currChapTemplate, x, curY);

            //Now we have to send the template object to our custom eventhandler 
            //method that will store a template for each item in our TOC
            pageEventHandler.addChapTemplateList(currChapTemplate);
        }

After you have built the TOC protion of the document, the next step is to generate the actualy content that corresponds to the TOC. When you create the actual page each of the headers you will need to create a new Chapter variable and add it to the document. This will fire the custom code that you will be adding to the OnChapter eventhandler.

Finally, in the custom page eventhandler we need to add code to the OnChapter method and we need to create a custom method to store a List of templates.

    int chapTemplateCounter = 0;
    public override void OnChapter(PdfWriter writer, Document document, float paragraphPosition, Paragraph title)
    {
        base.OnChapter(writer, document, paragraphPosition, title);

        BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);

        tableOfContentsTemplateList[chapTemplateCounter].BeginText();
        tableOfContentsTemplateList[chapTemplateCounter].SetFontAndSize(bfTimes, 12);
        tableOfContentsTemplateList[chapTemplateCounter].SetTextMatrix(0, 0);
        tableOfContentsTemplateList[chapTemplateCounter].ShowText("" + writer.PageNumber);
        tableOfContentsTemplateList[chapTemplateCounter].EndText();

        chapTemplateCounter++;
    }

Array of templates:

    List<PdfTemplate> tableOfContentsTemplateList = new List<PdfTemplate>();
    public void addChapTemplateList(PdfTemplate chapTemplate)
    {

        tableOfContentsTemplateList.Add(chapTemplate);

    }

I hope that helps!

damnnewbie
  • 156
  • 4
0

I'm surprised this variant of the "Page X of Y" question hasn't come up yet. But it clearly hasn't, so here goes.

When you create the "heading" text, you need to also add a PdfTemplate to the "DirectContent" of the TOC page where each page number will appear.

In a PdfPageEvent class, you need to use the OnChapter and OnSection events to determine the current page number, and write that number into the PdfTemplate for that particular section.

You can see something similar done in C# with iTextSharp in this question: iTextSharp Creating a Footer Page # of #. You'll have to translate it to VB.net on your own. They're differing a single PdfTemplate till they know the total number of pages in the PDF, where you'll be differing a PdfTemplate for each entry in your TOC and filling them in when you know what page they're on... but the concept is the same.

Community
  • 1
  • 1
Mark Storer
  • 15,672
  • 3
  • 42
  • 80