SelectPdf for .NET 10 - Fast Web View (Linearized Pdf) - C# / ASP.NET 10 Sample

This sample shows how to convert from html to pdf using SelectPdf and save the document linearized ("Fast Web View"): its objects are ordered first page first, with hint tables, so a viewer reading it over the web - Chrome, Edge, Adobe Acrobat - shows the first page before the rest of the file has arrived. It helps large documents served from a web site.

Url:


Rendering Engine:

Linearize (Fast Web View)

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 PdfLinearizeModel : PageModel
    {
        public void OnGet()
        {
            DdlRenderingEngine = "Chromium";
        }

        [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 bool ChkLinearize { 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;
            converter.Options.RenderingEngine = renderingEngine;

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

            // save it linearized (Fast Web View): first page first, with hint tables, so a viewer
            // reading it over the web shows the first page before the rest has arrived
            doc.Linearize = ChkLinearize;

            // 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;
        }
    }
}