SelectPdf for .NET 10 - Automatic Bookmarks with Html to Pdf Converter - C# / .NET 10 Sample

This sample shows how the html to pdf converter can automatically generate pdf bookmarks based on some elements selection using SelectPdf Pdf Library for .NET.

The elements that will be bookmarked are defined using CSS selectors. For example, the selector for all the H1 elements is "H1", the selector for all the elements with the CSS class name 'myclass' is "*.myclass" and the selector for the elements with the id 'myid' is "*#myid". Read more about CSS selectors here.

Test document

Url:


Rendering Engine:


Bookmark the following elements:

Hierarchical bookmarks (nest h2 under h1)

Sample code · C#
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using SelectPdf.Universal;

namespace SelectPdf.Samples.Pages
{
    public class AutomaticBookmarksModel : PageModel
    {
        public void OnGet()
        {
            DdlRenderingEngine = "Chromium";
            TxtUrl = Helper.WWWRoot(Request) + Url.Content("~/files/document.html");
        }

        [BindProperty]
        public string TxtUrl { get; set; }

        [BindProperty]
        public string DdlRenderingEngine { get; set; } = "Chromium";

        public List<SelectListItem> RenderingEngines { get; } = new List<SelectListItem>
        {
            new SelectListItem { Value = "Chromium", Text = "Chromium" },
        };

        [BindProperty]
        public string TxtElements { get; set; }

        [BindProperty]
        public bool ChkHierarchical { get; set; } = true;

        public IActionResult OnPost()
        {
            RenderingEngine renderingEngine =
                SampleForm.ParseEnum(DdlRenderingEngine, RenderingEngine.Chromium);

            // instantiate a html to pdf converter object
            HtmlToPdf converter = new HtmlToPdf();
            converter.Options.NavigationTimeout = Startup.NavigationTimeout;

            // set the css selectors for the automatic bookmarks
            converter.Options.RenderingEngine = renderingEngine;
            converter.Options.PdfBookmarkOptions.CssSelectors =
                TxtElements.Split(new char[] { ',' });

            // nest the bookmarks in the order of the selectors: with "h1, h2" every h2 bookmark
            // is placed under the h1 bookmark that precedes it
            converter.Options.PdfBookmarkOptions.HierarchicalBookmarks = ChkHierarchical;

            // display the bookmarks when the document is opened in a viewer
            converter.Options.ViewerPreferences.PageMode = PdfViewerPageMode.UseOutlines;

            // create a new pdf document converting an url
            PdfDocument doc = converter.ConvertUrl(TxtUrl);

            // save pdf document
            byte[] pdf = doc.Save();

            // close pdf document
            doc.Close();

            // return resulted pdf document
            FileResult fileResult = new FileContentResult(pdf, "application/pdf");
            fileResult.FileDownloadName = "Document.pdf";
            return fileResult;
        }
    }
}