HiQPdf HTML to PDF Converter can be configured to automatically convert HTML form controls to interactive PDF form fields by setting the PdfDocumentControlGeneratePdfFormFields property to true. An object of type PdfDocumentControl is exposed by the HtmlToPdfDocument property
You can also use the object of type PdfFormOptions, exposed by the PdfDocumentControlPdfFormOptions property, to control field naming, button actions, submit behavior, validation mapping and diagnostics for HTML to PDF form conversion.
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 CreatePdfFormsFromHtmlFormsController : Controller
{
private readonly IWebHostEnvironment m_hostingEnvironment;
public CreatePdfFormsFromHtmlFormsController(IWebHostEnvironment hostingEnvironment)
{
m_hostingEnvironment = hostingEnvironment;
}
public ActionResult Index()
{
var model = SetViewModel();
return View(model);
}
[HttpPost]
public ActionResult ConvertHtmlToPdf(CreatePdfFormsFromHtmlFormsViewModel 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();
// Specifies whether HTML form controls are converted to interactive PDF form fields
htmlToPdfConverter.Document.GeneratePdfFormFields = model.GeneratePdfFormFields;
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 = "CreatePdfForms.pdf";
return fileResult;
}
private CreatePdfFormsFromHtmlFormsViewModel SetViewModel()
{
var model = new CreatePdfFormsFromHtmlFormsViewModel();
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 - "CreatePdfFormsFromHtmlForms".Length);
model.HtmlString = System.IO.File.ReadAllText(Path.Combine(contentRootPath, "DemoFiles/Html/HTML_Form.html"));
model.BaseUrl = rootUrl + "DemoFiles/Html/";
model.Url = rootUrl + "DemoFiles/Html/HTML_Form.html";
return model;
}
}
}