Adding a TOC to Report Book programmatically

2 posts, 0 answers
  1. Hari
    Hari avatar
    4 posts
    Member since:
    Mar 2019

    Posted 09 May Link to this post

    I have a report book which I create through C# code. I want to have a single TOC covering all the reports in the report book. 

    Following this (https://docs.telerik.com/reporting/designing-reports-reportbook-toc) I started creating a TOC but this article (https://docs.telerik.com/reporting/table-of-contents-report-book) says to add TocReportSource of ReportBook programmatically. But the problem is it doesn't have any example of how to do that. Can you add a sample code of how to achieve this..

     

    //Report Book
    var reportBook = new ReportBook();
     
    //Cover Page
    var coverPageReportSource = new InstanceReportSource();
    var coverPagereport = new CoverPage(reportId)
    {
        DocumentName = "Cover Page Report"
    };
    coverPageReportSource.ReportDocument = coverPagereport;
    reportBook.ReportSources.Add(coverPageReportSource);
     
    //TOC Page
    var tocPageReportSource = new InstanceReportSource();
    var tocPagereport = new FirstPage()
    {
        DocumentName = "toc Page Report"
    };
    tocPageReportSource.ReportDocument = tocPagereport;
    reportBook.ReportSources.Add(tocPageReportSource);
     
    //Image Report
    var imagesReportSource = new InstanceReportSource();
    var imagesDatareport = new ImageReport()
    {
        DocumentName = "Image Report"
    };
    imagesReportSource.ReportDocument = imagesDatareport;
    reportBook.ReportSources.Add(imagesReportSource);
     
    var instanceReportBookSource = new InstanceReportSource
    {
        ReportDocument = reportBook
    };
     
    var reportProcessor = new ReportProcessor();
    var renderingResult = reportProcessor.RenderReport("PDF", instanceReportBookSource, new Hashtable());
  2. Neli
    Admin
    Neli avatar
    60 posts

    Posted 14 May Link to this post

    Hi Hari,

    Adding a TOC to a Report Book is demonstrated in our Report Book demo which you can find locally at the installation folder of our Reporting tool - by default this would be C:\Program Files (x86)\Progress\Telerik Reporting [Version]\Examples\CSharp\ReportLibrary\ReportBook.

    Note that the TOC should be included in a separate report.
    1) For that reason, create a new report. Right-click outside of the report and select Table of Contents
    2) Configure its Title, Levels, and Styles as described in the Table of Contents article. 
    3) When it comes to adding TocReportSource of  ReportBook programmatically the code might look as follows:

    void AddTocTemplate()
      {
        var tocReportSource = new TypeReportSource();
         tocReportSource.TypeName = typeof(ReportBookToc).AssemblyQualifiedName;
         this.TocReportSource = tocReportSource;
      }

    Note that the TOC Report should be added as TocReportSource and not as a regular report.

    4) You also need to add all reports - for example through the following method:

    void AddReports()
    {
        this.ReportSources.Add(new TypeReportSource
        {
            TypeName = typeof(Report1).AssemblyQualifiedName
        });  
    }


    5) The methods should be called in the constructor of the report book. Finally the ReportBook class might look as follows:

    namespace ReportLibrary
    {
        using System.ComponentModel;
        using Telerik.Reporting;
      
        public class ReportBook : Telerik.Reporting.ReportBook
        {
            public ReportBook()
            {
                AddTocTemplate();
      
                AddReports();
            }
      
            void AddTocTemplate()
            {
                var tocReportSource = new TypeReportSource();
                tocReportSource.TypeName = typeof(ReportBookToc).AssemblyQualifiedName;
                this.TocReportSource = tocReportSource;
            }
      
            void AddReports()
            {
                this.ReportSources.Add(new TypeReportSource
                {
                    TypeName = typeof(Report1).AssemblyQualifiedName
                });
     
            // add more reports here
     
            }
        }
    }


    6) You might want to set the Toc Text property of each report and the corresponding inner level sections/items, which is going to be added in the report book TOC.

    Regards,
    Neli
    Progress Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top