Select HTML Elements to Convert to PDF

You can convert only selected parts of an HTML page to PDF by specifying a CSS selector. This allows you to choose exactly which content will be included in the PDF and to either remove or simply hide the unselected elements.

The CSS selector can be assigned to the HtmlToPdfConverterConvertedElementsSelector property of the converter. For example, you can select an HTML element by its ID using the #ConvertedHtmlElementID selector, or select elements with a specific CSS class using the .ConvertedHtmlElementClass selector.

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

When you convert only a part of the HTML page, the content is reduced when the unselected elements are removed. By default the converter will render a number of PDF pages with the selected content but naturally some white space can remain in the last page. You can use the PdfDocumentOptionsAutoResizePdfPageHeight property to create a single PDF page to display the converted elements. This property must be used in conjunction with the HtmlToPdfConverterHtmlViewerHeight property, which must be set to the minimum value of 1 pixel. An object of type PdfDocumentOptions is accessible through the HtmlToPdfConverterPdfDocumentOptions property.

Code Sample - Select HTML Elements to Convert to PDF

C#
using System;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

// Use Winnovative Namespace
using Winnovative.Pdf.Chromium;

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

        public ActionResult Index()
        {
            SetCurrentViewData();

            return View();
        }

        [HttpPost]
        public ActionResult ConvertHtmlToPdf(IFormCollection collection)
        {
            // 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 = "fvDh8eDx4fHg4P/h8eLg/+Dj/+jo6Og=";

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

            bool enableElementsSelector = collection["enableElementsSelectorCheckBox"].Count > 0;
            if (enableElementsSelector)
            {
                // The CSS selector used to identify the elements to include in the PDF
                htmlToPdfConverter.ConvertedElementsSelector = collection["convertedElementsSelectorTextBox"];

                // Specify whether elements that are not matched by ConvertedElementsSelector
                // should be completely removed from the layout rather than just hidden
                htmlToPdfConverter.RemoveUnselectedElements = collection["removeUnselectedElementsCheckBox"].Count > 0;

                // Automatically resizes the PDF page height to match the selected HTML content height
                htmlToPdfConverter.PdfDocumentOptions.AutoResizePdfPageHeight = collection["autoResizePdfPageHeightCheckBox"].Count > 0;
                if (htmlToPdfConverter.PdfDocumentOptions.AutoResizePdfPageHeight)
                    htmlToPdfConverter.HtmlViewerHeight = 1;
            }

            byte[] outPdfBuffer = null;

            if (collection["HtmlPageSource"] == "convertHtmlRadioButton")
            {
                string htmlWithForm = collection["htmlStringTextBox"];
                string baseUrl = collection["baseUrlTextBox"];

                // Convert a HTML string to a PDF document
                outPdfBuffer = htmlToPdfConverter.ConvertHtml(htmlWithForm, baseUrl);
            }
            else
            {
                string url = collection["urlTextBox"];

                // 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 void SetCurrentViewData()
        {
            ViewData["ContentRootPath"] = 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();

            ViewData["CurrentPageUrl"] = uriBuilder.Uri.AbsoluteUri;
        }
    }
}

See Also