Select HTML Elements to Exclude from PDF

You can exclude selected parts of an HTML page from conversion to PDF by specifying a CSS selector. This allows you to choose exactly which content will be excluded from the PDF and to either remove or simply hide the excluded elements.

The CSS selector can be assigned to the HtmlToPdfConverterExcludedElementsSelector property of the converter. For example, you can exclude an HTML element by its ID using the #ExcludedHtmlElementID selector, or exclude elements with a specific CSS class using the .ExcludedHtmlElementClass selector.

Furthermore, you can use the HtmlToPdfConverterRemoveExcludedElements property of the converter to specify what happens to the excluded elements. By default, this property is set to true, and the excluded elements are removed from the layout, allowing the content to reflow without them. If you set this property to false, the excluded elements are simply hidden and the space they originally occupied in the HTML page is still reserved in the content layout.

Code Sample - Select HTML Elements to Convert to PDF

C#
using System;
using System.IO;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using EvoPdf_Next_AspNetDemo.Models;
using EvoPdf_Next_AspNetDemo.Models.HTML_to_PDF;

// Use EVO PDF Namespace
using EvoPdf.Next;

namespace EvoPdf_Next_AspNetDemo.Controllers.HTML_to_PDF
{
    public class Select_HTML_Elements_to_Exclude_from_PDFController : Controller
    {
        private readonly IWebHostEnvironment m_hostingEnvironment;
        public Select_HTML_Elements_to_Exclude_from_PDFController(IWebHostEnvironment hostingEnvironment)
        {
            m_hostingEnvironment = hostingEnvironment;
        }

        public ActionResult Index()
        {
            var model = SetViewModel();
            return View(model);
        }

        [HttpPost]
        public ActionResult ConvertHtmlToPdf(Select_HTML_Elements_to_Exclude_from_PDF_ViewModel model)
        {
            if (!ModelState.IsValid)
            {
                var errorMessage = ModelStateHelper.GetModelErrors(ModelState);
                throw new ValidationException(errorMessage);
            }

            // Set license key received after purchase to use the converter in licensed mode
            // Leave it not set to use the library in demo mode
            Licensing.LicenseKey = "3FJDU0ZDU0NTQkddQ1NAQl1CQV1KSkpKU0M=";

            // Create a HTML to PDF converter object with default settings
            HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();

            bool enableExcludedElementsSelector = model.EnableExcludedElementsSelector;
            if (enableExcludedElementsSelector)
            {
                // The CSS selector used to identify the elements to exclude from conversion to PDF
                htmlToPdfConverter.ExcludedElementsSelector = model.ExcludedElementsSelector;

                // Specify whether elements that are not matched by ExcludedElementsSelector
                // should be completely removed from the layout rather than just hidden
                htmlToPdfConverter.RemoveExcludedElements = model.RemoveExcludedElements;
            }

            byte[] outPdfBuffer = null;

            if (model.HtmlPageSource == "Html")
            {
                string htmlWithForm = model.HtmlString;
                string baseUrl = model.BaseUrl;

                // Convert a HTML string to a PDF document
                outPdfBuffer = htmlToPdfConverter.ConvertHtml(htmlWithForm, baseUrl);
            }
            else
            {
                string url = model.Url;

                // Convert the HTML page to a PDF document
                outPdfBuffer = htmlToPdfConverter.ConvertUrl(url);
            }

            // Send the PDF file to browser
            FileResult fileResult = new FileContentResult(outPdfBuffer, "application/pdf");
            fileResult.FileDownloadName = "Select_HTML_Elements_to_Convert.pdf";

            return fileResult;
        }

        private Select_HTML_Elements_to_Exclude_from_PDF_ViewModel SetViewModel()
        {
            var model = new Select_HTML_Elements_to_Exclude_from_PDF_ViewModel();

            var contentRootPath = Path.Combine(m_hostingEnvironment.ContentRootPath, "wwwroot");

            HttpRequest request = ControllerContext.HttpContext.Request;
            UriBuilder uriBuilder = new UriBuilder();
            uriBuilder.Scheme = request.Scheme;
            uriBuilder.Host = request.Host.Host;
            if (request.Host.Port != null)
                uriBuilder.Port = (int)request.Host.Port;
            uriBuilder.Path = request.PathBase.ToString() + request.Path.ToString();
            uriBuilder.Query = request.QueryString.ToString();

            string currentPageUrl = uriBuilder.Uri.AbsoluteUri;
            string rootUrl = currentPageUrl.Substring(0, currentPageUrl.Length - "Select_HTML_Elements_to_Exclude_from_PDF".Length);

            model.HtmlString = System.IO.File.ReadAllText(Path.Combine(contentRootPath, "DemoAppFiles/Input/HTML_Files/Excluded_Elements.html"));
            model.BaseUrl = rootUrl + "DemoAppFiles/Input/HTML_Files/";
            model.Url = rootUrl + "DemoAppFiles/Input/HTML_Files/Excluded_Elements.html";

            return model;
        }
    }
}

See Also