EVO PDF Logo

Define in HTML the PDF Form Fields

EVO PDF Client for .NET Core

EVO HTML to PDF Converter allows you to select in HTML the fields you want to be converted to PDF form fields using the data-pdf-form-field special attribute. The other HTML fields which were not explicitly selected will not be converted to PDF form fields.

Code Sample - Define in HTML the PDF Form Fields

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;

// Use EVO PDF Namespace
using EvoPdfClient;

namespace EvoHtmlToPdfDemo.Controllers.HTML_to_PDF.PDF_Forms
{
    public class Define_in_HTML_PDF_Form_FieldsController : Controller
    {
        [HttpPost]
        public ActionResult ConvertHtmlToPdf(IFormCollection collection)
        {
            // Get the server options
            string serverIP = collection["textBoxServerIP"];
            uint serverPort = uint.Parse(collection["textBoxServerPort"]);
            string servicePassword = collection["textBoxServicePassword"];
            bool useServicePassword = servicePassword.Length > 0;
            bool useTcpService = collection["ServerType"] == "radioButtonUseTcpService";
            string webServiceUrl = collection["textBoxWebServiceUrl"];

            // Create the HTML to PDF converter object
            HtmlToPdfConverter htmlToPdfConverter = null;
            if (useTcpService)
                htmlToPdfConverter = new HtmlToPdfConverter(serverIP, serverPort);
            else
                htmlToPdfConverter = new HtmlToPdfConverter(true, webServiceUrl);

            // Set optional service password
            if (useServicePassword)
                htmlToPdfConverter.ServicePassword = servicePassword;

            // Set license key received after purchase to use the converter in licensed mode
            // Leave it not set to use the converter in demo mode
            htmlToPdfConverter.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c=";

            // Set an adddional delay in seconds to wait for JavaScript or AJAX calls after page load completed
            // Set this property to 0 if you don't need to wait for such asynchcronous operations to finish
            htmlToPdfConverter.ConversionDelay = 2;

            // Set the submit buttons style
            htmlToPdfConverter.PdfFormOptions.SubmitButtonStyle.BackColor = RgbColor.Beige;

            // Set the style of various types of text boxes
            htmlToPdfConverter.PdfFormOptions.TextBoxStyle.BackColor = RgbColor.AliceBlue;
            htmlToPdfConverter.PdfFormOptions.PasswordTextBoxStyle.BackColor = RgbColor.MistyRose;
            htmlToPdfConverter.PdfFormOptions.MultilineTextBoxStyle.BackColor = RgbColor.AliceBlue;

            // Set the radio buttons style
            htmlToPdfConverter.PdfFormOptions.RadioButtonsGroupStyle.BackColor = RgbColor.AntiqueWhite;

            // Set the checkboxes styles
            htmlToPdfConverter.PdfFormOptions.CheckBoxStyle.BackColor = RgbColor.AntiqueWhite;

            // set the drop down lists style
            htmlToPdfConverter.PdfFormOptions.ComboBoxStyle.BackColor = RgbColor.LightCyan;

            byte[] outPdfBuffer = null;

            if (collection["HtmlPageSource"] == "convertHtmlRadioButton")
            {
                // Convert a HTML string to a PDF document with form fields
                string htmlWithForm = collection["htmlStringTextBox"];
                outPdfBuffer = htmlToPdfConverter.ConvertHtml(htmlWithForm, String.Empty);
            }
            else
            {
                // Convert the HTML page to a PDF document with form fields
                string url = collection["urlTextBox"];
                outPdfBuffer = htmlToPdfConverter.ConvertUrl(url);
            }

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

            return fileResult;
        }
    }
}
HTML Form to Convert to a PDF Form

XML
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Select HTML Form Fields to Convert to PDF Form</title>
</head>
<body style="font-family: 'Times New Roman'; font-size: 14px">
    <form name="inputForm" action="http://www.evopdf.com/pdf_forms_submit/" method="post">
        <table>
            <tr>
                <td>First name:</td>
                <td style="width: 10px"></td>
                <td>
                    <input data-pdf-form-field="true" type="text" name="firstname" value="Enter First Name"></td>
            </tr>
            <tr>
                <td>Last name:</td>
                <td style="width: 10px"></td>
                <td>
                    <input data-pdf-form-field="true" type="text" name="lastname"></td>
            </tr>
            <tr>
                <td>Password:</td>
                <td style="width: 10px"></td>
                <td>
                    <input data-pdf-form-field="true" type="password" name="password"></td>
            </tr>
            <tr>
                <td>Gender:</td>
                <td style="width: 10px"></td>
                <td>
                    <input data-pdf-form-field="true" type="radio" name="gender" value="male" checked="checked">Male &nbsp;
                    <input data-pdf-form-field="true" type="radio" name="gender" value="female">Female
                </td>
            </tr>
            <tr>
                <td>Vehicle:</td>
                <td style="width: 10px"></td>
                <td>
                    <input type="checkbox" name="car" value="car" checked="checked">I have a car &nbsp;
                    <input type="checkbox" name="bike" value="bike">I have a bike
                </td>
            </tr>
            <tr>
                <td>Vehicle Type:</td>
                <td style="width: 10px"></td>
                <td>
                    <select>
                        <option value="volvo">Volvo</option>
                        <option value="saab">Saab</option>
                        <option value="audi" selected="selected">Audi</option>
                        <option value="opel">Opel</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td>Comments:</td>
                <td style="width: 10px"></td>
                <td>
                    <textarea style="width: 250px; height: 75px">Enter your comments here:
First comment line
Second comment line</textarea></td>
            </tr>
            <tr>
                <td colspan="3" style="height: 10px"></td>
            </tr>
            <tr>
                <td colspan="3">
                    <input data-pdf-form-field="true" type="submit" name="submitButton" value="Submit"></td>
            </tr>
        </table>
    </form>
</body>
</html>