In This Topic
    Add bookmarks
    In This Topic

    Introduction

    The following example demonstrates how to add bookmarks in a PDF document. It covers how to add a bookmark linking to the top of a page, to a specific position in a page, as well as how to add a bookmark that is a child of another bookmark.

    Add bookmarks in a PDF document (C#)
    Copy Code
    public static void AddBookmarks()
    {
      Console.WriteLine( "=== ADD BOOKMARKS ===" );
    
      var outputFileName = "AddBookmarks.pdf";
      var outputPath = BookmarksSample.BookmarksSampleOutputDirectory + outputFileName;
    
      // Creates a PdfDocument.
      using( var pdfoutput = PdfDocument.Create( outputPath ) )
      {
        // Gets the first Page.
        var firstPage = pdfoutput.Pages.First();
    
        // Adds a title.
        var textStyle = TextStyle.WithFont( pdfoutput.Fonts.GetStandardFont( StandardFontType.Helvetica ), 15d );
        firstPage.AddParagraph( "Add bookmarks", textStyle, new ParagraphStyle( ParagraphHorizontalAlignment.Center ) );
    
        // Adds 2 more Pages.
        var secondPage = pdfoutput.Pages.Add();
        var thirdPage = pdfoutput.Pages.Add();
    
        // Adds text to the 3 first Pages
        firstPage.AddText( "This is the FIRST page", new Point( 100, 100 ) );
        secondPage.AddText( "This is the SECOND page", new Point( 100, 100 ) );
        thirdPage.AddText( "This is the THIRD page", new Point( 100, 100 ) );
    
        // Adds the first Bookmark, which targets the top of the first Page.
        var firstBookmark = new Bookmark( "Bookmark1" ) { TargetPage = 0 };
        pdfoutput.Bookmarks.Add( firstBookmark );
    
        // Adds a second Bookmark, which targets positionY = 100 on the second Page.
        var secondBookmark = new Bookmark( "Bookmark2" )
        {
          TargetPage = 1,
          TargetPageLocation = new Point( 0, 100 ),
          IsBold = true,
          IsItalic = true,
          TitleColor = Color.Red
        };
        pdfoutput.Bookmarks.Add( secondBookmark );
    
        // Adds a third Bookmark, which is a child of the second Bookmark & targets positionY = 105 on the third Page.
        var thirdBookmark = new Bookmark( "Bookmark3" )
        {
          TargetPage = 2,
          TargetPageLocation = new Point( 0, 105 ),
          IsItalic = true,
        };
        secondBookmark.Children.Add( thirdBookmark );
    
        // Saves the output document.
        pdfoutput.Save();
        Console.WriteLine( $"Info exported to path: {outputFileName}" );
      }
    }

    See Also

    Get elements from a PDF