title: Add MigraDoc Table of Contents to PDFsharp PDF
body: <p>I am trying to use PDFsharp and MigraDoc to create a PDF report file from many other files.</p>
  
  <p><strong>Basically the workflow is as follows:</strong></p>
  
  <ol>
  <li>The user uploads 2 or more documents to concatenate</li>
  <li>The user selects what pages they want to include in the TOC</li>
  <li>The documents are concatenated together and a table of contents is generated</li>
  </ol>
  
  <p>Now I am able to easily concatenate the documents and add bookmarks/outlines in PDFsharp. Where I have run into problems is when using MigraDoc to create the TOC referencing the PDFsharp created bookmarks/outlines as the targets of the hyperlinks.</p>
  
  <p><strong>Code to add bookmark/outline in PDFsharp (successfully adds the bookmarks):</strong></p>
  
  <pre><code>// Current document is a PdfDocument and Title is the name of the bookmark
  CurrentDocument.Outlines.Add(title, page, true, PdfOutlineStyle.Bold);
  </code></pre>
  
  <p><strong>MigraDoc code to create the TOC page and render it to the current PDFsharp document:</strong></p>
  
  <pre><code>// Create blank page
              PdfPage page = (!hasTitlePage)
                  ? AddPage(null, 0, TOC_BOOKMARK_TITLE) // Add to start
                  : AddPage(null, 1, TOC_BOOKMARK_TITLE); // Add after title page
  
              // Get Graphics obj
              XGraphics gfx = XGraphics.FromPdfPage(page);
              gfx.MUH = PdfFontEncoding.Unicode;
  
              // Create MigraDoc document + Setup styles
              Document document = new Document();
              DefineStyles(document);
  
              // Add header
              Section section = document.AddSection();
              Paragraph paragraph = section.AddParagraph("Table of Contents");
              paragraph.Format.Font.Size = 14;
              paragraph.Format.Font.Bold = true;
              paragraph.Format.SpaceAfter = 24;
              paragraph.Format.OutlineLevel = OutlineLevel.Level1;
  
              // Add links - these are the PdfSharp outlines/bookmarks added previously when concatinating the pages
              foreach (var bookmark in CurrentDocument.Outlines)
              {
                  paragraph = section.AddParagraph();
                  paragraph.Style = "TOC";
                  Hyperlink hyperlink = paragraph.AddHyperlink(bookmark.Title);
                  hyperlink.AddText($"{bookmark.Title}\t");
                  hyperlink.AddPageRefField(bookmark.Title);
              }
  
              // Render document
              DocumentRenderer docRenderer = new DocumentRenderer(document);
              docRenderer.PrepareDocument();
              docRenderer.RenderPage(gfx, 1);
  
              return page;
  </code></pre>
  
  <p><em>Note - the MigraDoc code adds the TOC page but it doesn't recognize the PDFsharp bookmarks.</em></p>
  
  <p><strong>Screenshot of the TOC:</strong>
  The TOC page is added at the start (although the TOC bookmark is listed at the end) but I cannot link to PDFsharp bookmarks
  <a href="https://i.stack.imgur.com/FChpM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FChpM.png" alt="This is what the TOC looks like"></a></p>
  
  <p>I am certain that the issue is with my understanding of MigraDoc and/or PDFsharp.</p>
  
  <p>First prize would be to only use PDFsharp to add the TOC. I would appreciate any help in being pointed in the right direction.</p>