Table Of Contents

Skip Navigation LinksHome  /  Support  /  Forums  /  DynamicPDF CoreSuite for .NET (v10)  /  Re: Table Of Contents

DynamicPDF CoreSuite for .NET (v10) Forum

 Oct 23 2020 7:00 AM
Hi,

Just want to ask if there is a feature of DynamicPDF where it will automatically create the table of contents?

For now I am using this code.

public void BuildPDF()
        {
            IList<KeyValuePair<string, string>> tableOfContentList = new List<KeyValuePair<string, string>>();

            Document document = new Document();

            #region -- Front Page Creation --
            //Page frontPage = new FrontPageContent().CreateFrontPage(imagePath);
            //document.Pages.Add(frontPage);
            #endregion

            // total Number of page and page Description: example ("2", "Comparison of Inbound Exam Results with Outbound Exam Results")
            tableOfContentList.Add(new KeyValuePair<string, string>("2", "Comparison of Inbound Exam Results with Outbound Exam Results"));
            tableOfContentList.Add(new KeyValuePair<string, string>("1", "Inbound Exam"));
            tableOfContentList.Add(new KeyValuePair<string, string>("1", "Inbound Exam Summary"));
            tableOfContentList.Add(new KeyValuePair<string, string>("1", "Outbound Exam"));
            tableOfContentList.Add(new KeyValuePair<string, string>("1", "Outbound Exam Summary"));
            tableOfContentList.Add(new KeyValuePair<string, string>("1", "Accounting"));


            #region -- Table of Contents Creation --
            Page tableOfContentPage = BuildTableOfContent(
                "ON-BOARDING PACKET TABLE OF CONTENTS",
                tableOfContentList);

            document.Pages.Add(tableOfContentPage);
            #endregion

            #region -- Page 4 Creation --
            //Page page4 = new Page();
            //document.Pages.Add(frontPage);
            #endregion


            document.Draw(@"C:\Junk\Test Files\TableOfContents.pdf");

        }

public Page BuildTableOfContent(string title, IList<KeyValuePair<string, string>> dataList)
        {
            Page page = new Page(PageSize.Letter);

            Label header = new Label(title, 0, 0, 512, 14, Font.HelveticaBold, 15,
                TextAlign.Center);
            header.TextColor = new RgbColor(38, 53, 72);
            page.Elements.Add(header);

            int count = 3; // Start of paging for the dynamic pages
            int x = 50;

            Table2 table = new Table2(0, 40, 512, 600);
            table.Columns.Add(490);
            table.Columns.Add(22);
            table.CellDefault.Border.Width = 0.001f;
            table.CellDefault.VAlign = VAlign.Center;


            foreach (KeyValuePair<string, string> list in dataList)
            {
                Row2 row = table.Rows.Add(25);
                row.CellDefault.Align = TextAlign.Left;
                row.CellDefault.VAlign = VAlign.Center;
                row.CellDefault.Border.Top.LineStyle = LineStyle.None;
                row.CellDefault.Border.Left.LineStyle = LineStyle.None;
                row.CellDefault.Border.Right.LineStyle = LineStyle.None;
                row.CellDefault.Border.Width = 0.001f;
                row.CellDefault.Padding.Left = 5;
                row.CellDefault.Padding.Right = 5;
                row.CellDefault.FontSize = 10;

                Cell2 cell = row.Cells.Add("");

                Group group = new Group();
                FormattedTextAreaStyle style = new FormattedTextAreaStyle(FontFamily.Helvetica, 10, true);
                FormattedTextArea area = new FormattedTextArea(list.Value, 0, 2, 502, 20, style);
                area.VAlign = VAlign.Center;

                group.Add(area);
                cell.Element = group;

                row.Cells.Add(count.ToString());

                count += Convert.ToInt32(list.Key);
                x = x + 20;
            }

            page.Elements.Add(table);

            return page;
        }

If there is a more faster way without doing this one?

Thank you,
Leo

 Oct 23 2020 12:05 PM
Posted by a ceTe Software moderator
Hello,

We don’t currently have a way to automate the table of contents creation. The code you provided looks like a great solution. You could also use a Link page element as well to link to the pages from your table. Here is more information on the Link page element:
  here.

Thanks,
ceTe Software Support Team
 Oct 25 2020 10:05 PM
Hi,

Thanks for the reply, one more thing.

If my table of contents exceeded and it has more than 1 page.  How will the DynamicPDF will automatically know that it should be place now in the next page?
 Oct 26 2020 9:38 AM
Posted by a ceTe Software moderator
Hello,

You can achieve this by building adding all the content to the Table object then checking if it needs to overflow and adding continuing the table to as many pages as needed to fit the contents. Please refer to the documentation on table continuation here.

Thanks,
ceTe Software Support Team

All times are US Eastern Standard time. The time now is 3:34 AM.