Click or drag to resize

Bookmarks

The document model adds bookmarks (the outline a PDF viewer shows beside the pages) to a PDF document. The AddBookmark methods of the Document class add root bookmarks and, with a parent bookmark, child bookmarks. Each bookmark leads to an ExplicitDestination: a page, a point on the page, and how the page is displayed (DestinationViewMode, zoom).

The Bookmark returned by the method has a Color and a Style (normal, bold, italic), and its children are in DescendantBookmarks. All the bookmarks of the document, including those of a loaded document and those an HTML conversion created, are in the Bookmarks collection. Bookmarks can also be created automatically from the headings of an HTML document (see HTML to PDF Converter Element and Automatic Bookmarks).

Code Sample

The code sample below, an ASP.NET Core controller action, creates a document with two pages, root bookmarks for the pages and child bookmarks for the images on the first page, and returns the PDF to the browser:

public IActionResult CreatePdf()
{
    using (Document document = new Document())
    {
        // add a page to the PDF document
        PdfPage firstPage = document.AddPage();

        // add a root bookmark to the first page
        Bookmark firstPageBookmark = document.AddBookmark("First Page",
            new ExplicitDestination(firstPage));
        firstPageBookmark.Style = PdfBookmarkStyle.Bold;

        string imagesPath = Path.Combine(AppContext.BaseDirectory, "Images");

        // display an image in the available space in the page, with the height
        // determined automatically to keep the aspect ratio
        ImageElement imageElement1 = new ImageElement(0, 0,
            Path.Combine(imagesPath, "html-to-pdf.png"));
        AddElementResult addResult = firstPage.AddElement(imageElement1);

        // add a child bookmark to this image
        document.AddBookmark("HTML to PDF Converter", new ExplicitDestination(
            firstPage, new PointF(0, 0)), firstPageBookmark);

        // a second image below the first one
        float y = addResult.EndPageBounds.Bottom + 20;
        ImageElement imageElement2 = new ImageElement(0, y,
            Path.Combine(imagesPath, "word-to-pdf.png"));
        addResult = firstPage.AddElement(imageElement2);

        // add a child bookmark to this image
        Bookmark wordBookmark = document.AddBookmark("Word to PDF Converter",
            new ExplicitDestination(firstPage, new PointF(0, y)), firstPageBookmark);
        wordBookmark.Color = Color.Blue;

        // add a new page to the document
        PdfPage secondPage = document.AddPage();

        // add a root bookmark to the second page and set a 150% zoom when visiting
        // this bookmark
        ExplicitDestination secondPageDestination = new ExplicitDestination(
            secondPage);
        secondPageDestination.ZoomPercentage = 150;
        Bookmark secondPageBookmark = document.AddBookmark("Second Page",
            secondPageDestination);

        ImageElement imageElement3 = new ImageElement(0, 0,
            Path.Combine(imagesPath, "pdf-library.png"));
        secondPage.AddElement(imageElement3);

        // send the PDF document to the browser
        byte[] pdf = document.Save();
        return File(pdf, "application/pdf", "Bookmarks.pdf");
    }
}
See Also

Other Resources

Document Class