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 is excluded from the PDF and to either remove or simply hide the excluded elements.
The CSS selector can be assigned to the HtmlToPdfExcludedElementsSelector 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 HtmlToPdfRemoveExcludedElements 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 remains reserved in the content layout.
using System;
using System.IO;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using HiQPdf_Next_AspNetDemo.Models;
using HiQPdf.Next;
namespace HiQPdf_Next_AspNetDemo.Controllers
{
public class SelectHtmlElementsToExcludeFromPdfController : Controller
{
private readonly IWebHostEnvironment m_hostingEnvironment;
public SelectHtmlElementsToExcludeFromPdfController(IWebHostEnvironment hostingEnvironment)
{
m_hostingEnvironment = hostingEnvironment;
}
public ActionResult Index()
{
var model = SetViewModel();
return View(model);
}
[HttpPost]
public ActionResult ConvertHtmlToPdf(SelectHtmlElementsToExcludeFromPdfViewModel model)
{
if (!ModelState.IsValid)
{
var errorMessage = ModelStateHelper.GetModelErrors(ModelState);
throw new ValidationException(errorMessage);
}
// Set the serial number received after purchase to use the library in licensed mode; leave it commented for demo mode
// Licensing.SerialNumber = "your-serial-number";
// Create a HTML to PDF converter object with default settings
HtmlToPdf htmlToPdfConverter = new HtmlToPdf();
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.ConvertHtmlToMemory(htmlWithForm, baseUrl);
}
else
{
string url = model.Url;
// Convert the HTML page to a PDF document
outPdfBuffer = htmlToPdfConverter.ConvertUrlToMemory(url);
}
// Send the PDF file to browser
FileResult fileResult = new FileContentResult(outPdfBuffer, "application/pdf");
fileResult.FileDownloadName = "SelectHtmlElementsToExcludeFromPdf.pdf";
return fileResult;
}
private SelectHtmlElementsToExcludeFromPdfViewModel SetViewModel()
{
var model = new SelectHtmlElementsToExcludeFromPdfViewModel();
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 - "SelectHtmlElementsToExcludeFromPdf".Length);
model.HtmlString = System.IO.File.ReadAllText(Path.Combine(contentRootPath, "DemoFiles/Html/Excluded_Elements.html"));
model.BaseUrl = rootUrl + "DemoFiles/Html/";
model.Url = rootUrl + "DemoFiles/Html/Excluded_Elements.html";
return model;
}
}
}