HTML to PDF Converter ASP.NET Core samples · .NET 10 and .NET 11

ExpertPdf Html To Pdf Converter for .NET - Repeat the Table Header on Each Page - C# / ASP.NET Sample

When a table spans several PDF pages, its header can be repeated at the top of every page: put the header rows in a thead element styled with display: table-header-group. The Chromium and Blink engines repeat the header with the print CSS media type, the way browsers do when they print; the WebKit engine repeats it with both media types.

Table page (view it):

Rendering engine:


C# code of this sample

Pages/RepeatTableHeader.cshtml.cs
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using ExpertPdf.HtmlToPdf;

namespace ExpertPdf.Samples.Pages
{
    public class RepeatTableHeaderModel : PageModel
    {
        public void OnGet()
        {
            TxtUrl = Helper.WWWRoot(Request) + Url.Content("~/files/table/table_with_repeated_head.html");
        }

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

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

        public List<SelectListItem> RenderingEngines { get; } = Helper.RenderingEngines();

        public IActionResult OnPost()
        {
            // instantiate a html to pdf converter object
            PdfConverter converter = new PdfConverter() { AllowLocalAccess = false };
            // converter.LicenseKey = "your license key";

            // the rendering engine: Chromium is the recommended engine for modern pages
            converter.RenderingEngine = (RenderingEngine)Enum.Parse(typeof(RenderingEngine), DdlRenderingEngine);
            converter.PdfDocumentOptions.TopMargin = 20;
            converter.PdfDocumentOptions.BottomMargin = 20;

            // lay out the page for printing: Chromium and Blink repeat the thead of the table on every page with the
            // print media type
            converter.UsePrintCSSMediaType = true;

            // convert the url to pdf
            byte[] pdf = converter.GetPdfBytesFromUrl(TxtUrl);

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