PDF for UWP | ComponentOne
Using PDF for UWP / Adding Bookmarks to a PDF Document
In This Topic
    Adding Bookmarks to a PDF Document
    In This Topic

    When you open a PDF document using Adobe's Acrobat Reader application, you will notice that most long documents contain an outline structure that is displayed on a pane on the left of the reader. The outline makes it easy to browse through a document's structure and find specific topics. The picture below shows a PDF document with an outline:

     

    The outline entries are called Bookmarks, and you can add them to your PDF for UWP documents using the C1PdfDocument.AddBookmark method. The C1PdfDocument.AddBookmark method takes three parameters: the title of the outline entry, the outline level, and the 'y' position of the entry on the current page (measured in points from the top of the page).

    For example, the routine below adds a paragraph to a document and optionally marks it as a level-zero outline entry:

    VB
    Copy Code
     Private Function RenderParagraph(text As String, font As Font, rect As Rect, rectPage As Rect, outline As
            Boolean) As Rect
    ' If it doesn't fit on this page, add a page break
            rect.Height = pdf.MeasureString(text, font, rect.Width).Height
          If rect.Bottom > rectPage.Bottom Then
            pdf.NewPage()
            rect.Y = rectPage.Top
         End If
    
         ' Draw the string
           pdf.DrawString(text, font, Colors.Black, rect)
    
          ' Add headings to outline.
         If outline Then
            DrawLine(Pens.Black, rect.X, rect.Y, rect.Right,
            rect.Y)
            AddBookmark(text, 0, rect.Y)
         End If
    
          ' Update rectangle for next time
            rect.Offset(0, rect.Height)
            Return rect
     End Function
    

                

    Example Title
    Copy Code
    private Rect RenderParagraph(string text, Font font, Rect rect, Rect rectPage, bool outline)
    {
        // If it doesn't fit on this page, add a page break
        rect = new Rect(72, 72, 100, 50);
        rect.Height = pdf.MeasureString(text, font, rect.Width).Height;
    
        if (rect.Bottom > rectPage.Bottom)
        {
            pdf.NewPage();
            rect.Y = rectPage.Top;
        }
    
        // Draw the string
        pdf.DrawString(text, font, Windows.UI.Colors.Black, rect);
    
        // Add headings to outline
        if (outline)
        {
            pdf.DrawLine(Windows.UI.Colors.Black, rect.X, rect.Y, rect.Right, rect.Y);
            pdf.AddBookmark(text, 0, rect.Y);
        }
    
        // Update rectangle for next time
        rect.Offset(0, rect.Height);
        return rect;                       
    }