New to Telerik Document Processing? Download free 30-day trial

Create Table of Contents (TOC)

Product Version Product Author
2021.2.614 RadPdfProcessing Martin Velikov

Description

This article describes how to import PDF documents, merge them, and create a Table of Contents pointing to the merged document pages.

Solution

The following code snippets shows how to: 1. Import two PDF documents into RadFixedDocument instances using the PdfFormatProvider; 2. Merge them into a single RadFixedDocument using the RadFixedDocument`s Merge() method; 3. Create a Table of Contents (TOC) using Link annotations pointing to the merged document pages; 4. Export the merged document to a single PDF file.

Example

PdfFormatProvider provider = new PdfFormatProvider(); 
 
RadFixedDocument document1, document2; 
ImportDocuments(provider, out document1, out document2); 
 
document1.Merge(document2); 
 
CreateTOC(document1); 
 
ExportToPdf(provider, document1); 

Import PDF files

private static void ImportDocuments(PdfFormatProvider provider, out RadFixedDocument document1, out RadFixedDocument document2) 
{ 
    using (Stream stream = File.OpenRead("SampleDocument1.pdf")) 
    { 
        document1 = provider.Import(stream); 
    } 
 
    using (Stream stream = File.OpenRead("SampleDocument2.pdf")) 
    { 
        document2 = provider.Import(stream); 
    } 
} 

Create the Table of Contents

private static void CreateTOC(RadFixedDocument document1) 
{ 
    RadFixedPage toc = new RadFixedPage(); 
    document1.Pages.Insert(0, toc); 
 
    FixedContentEditor editor = new FixedContentEditor(toc); 
 
    foreach (RadFixedPage page in document1.Pages) 
    { 
        int pageNumber = document1.Pages.IndexOf(page); 
 
        if (pageNumber > 0) 
        { 
            int factor = 20; 
 
            int offsetX = 70; 
            int offsetY = 20 + factor * pageNumber; 
            editor.Position.Translate(offsetX, offsetY); 
 
            Block block = new Block(); 
            block.GraphicProperties.FillColor = new RgbColor(255, 5, 99, 193); 
            block.InsertText($"Page {pageNumber}"); 
            Size blockSize = block.Measure(); 
            editor.DrawBlock(block); 
 
            Location location = new Location 
            { 
                Left = 0, 
                Top = 0, 
                Zoom = 0, 
                Page = page 
            }; 
 
            GoToAction goToAction = new GoToAction(); 
            goToAction.Destination = location; 
 
            Link uriLink = toc.Annotations.AddLink(goToAction); 
            uriLink.Rect = new Rect(offsetX, offsetY, blockSize.Width, blockSize.Height); 
        } 
    } 
} 

Export to PDF file

private static void ExportToPdf(PdfFormatProvider provider, RadFixedDocument document1) 
{ 
    string exportedPdf = "Exported.pdf"; 
    if (File.Exists(exportedPdf)) 
    { 
        File.Delete(exportedPdf); 
    } 
 
    using (Stream output = File.OpenWrite(exportedPdf)) 
    { 
        provider.Export(document1, output); 
    } 
} 
In this article