SelectPdf for .NET 10 - Html Forms to PDF Form Fields - C# / .NET 10 Sample

This sample shows how the controls of a html form become fillable PDF form fields, with the CreateFormFields option of the SelectPdf html to pdf converter. Text boxes, text areas, check boxes, radio buttons and lists are converted, each placed over the control as it was printed and carrying its value.

Html code:


Convert the form controls into PDF form fields
Standard: 

Sample code · C#
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using SelectPdf.Universal;

namespace SelectPdf.Samples.Pages
{
    public class HtmlFormsToPdfFieldsModel : PageModel
    {
        public void OnGet()
        {
            TxtHtmlCode = @"<html lang=""en"">
<head><title>Application form</title></head>
<body style=""font-family: Arial"">
    <h1>Application form</h1>
    <form>
        <p>Full name <input type=""text"" name=""fullName"" value=""John Smith""></p>
        <p>Email <input type=""email"" name=""email""></p>
        <p><label><input type=""checkbox"" name=""agree"" checked> I agree</label></p>
        <fieldset><legend>Plan</legend>
            <label><input type=""radio"" name=""plan"" value=""basic""> Basic</label>
            <label><input type=""radio"" name=""plan"" value=""pro"" checked> Pro</label>
        </fieldset>
        <p>Country <select name=""country"">
            <option value=""ro"">Romania</option>
            <option value=""de"" selected>Germany</option>
        </select></p>
        <p>Comments<br><textarea name=""comments"" rows=""3"" cols=""40""></textarea></p>
    </form>
</body>
</html>
";
        }

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

        [BindProperty]
        public bool ChkCreateFormFields { get; set; } = true;

        [BindProperty]
        public string DdlStandard { get; set; } = "Full";

        public List<SelectListItem> Standards { get; } = new List<SelectListItem>
        {
            new SelectListItem { Value = "Full", Text = "None" },
            new SelectListItem { Value = "PdfA2B", Text = "PDF/A-2b" },
            new SelectListItem { Value = "UA1", Text = "PDF/UA-1 (accessible)" },
            new SelectListItem { Value = "PdfA3A", Text = "PDF/A-3a + PDF/UA-1" },
        };

        public IActionResult OnPost()
        {
            // instantiate a html to pdf converter object
            HtmlToPdf converter = new HtmlToPdf();
            converter.Options.NavigationTimeout = Startup.NavigationTimeout;

            // the form controls become fillable pdf form fields
            converter.Options.CreateFormFields = ChkCreateFormFields;

            // an accessible document needs a title and a language
            if (DdlStandard == "UA1" || DdlStandard == "PdfA3A")
            {
                converter.Options.AccessibilityStandard = PdfUAStandard.UA1;
                converter.Options.Title = "Application form";
                converter.Options.Language = "en";
            }
            if (DdlStandard == "PdfA2B") converter.Options.PdfStandard = PdfStandard.PdfA2B;
            if (DdlStandard == "PdfA3A") converter.Options.PdfStandard = PdfStandard.PdfA3A;

            // create a new pdf document converting the html code
            PdfDocument doc = converter.ConvertHtmlString(TxtHtmlCode);

            // save pdf document
            byte[] pdf = doc.Save();

            // close pdf document
            doc.Close();

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