New to Telerik Document ProcessingStart a free 30-day trial

Create Table of Contents (TOC)

Updated on Jun 9, 2026

Environment

Product VersionProductAuthor
2021.2.614RadPdfProcessingMartin 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 show how to:

  1. Import two PDF documents into RadFixedDocument instances by using the PdfFormatProvider.
  2. Merge them into a single RadFixedDocument by using the Merge() method.
  3. Create a Table of Contents (TOC) by using Link annotations pointing to the merged document pages.
  4. Export the merged document to a single PDF file.

Example 1: Main Method

csharp

	PdfFormatProvider provider = new PdfFormatProvider();

	RadFixedDocument document1, document2;
	ImportDocuments(provider, out document1, out document2);

	document1.Merge(document2);

	CreateTOC(document1);

	ExportToPdf(provider, document1);

Example 2: Import PDF Files

csharp

	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);
		}
	}

Example 3: Create the Table of Contents

csharp

	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);
			}
		}
	}

Example 4: Export to PDF File

csharp

	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);
		}
	}

See Also