HiQPdf Documentation

Create and Submit Forms

HiQPdf Client for .NET Core

An interactive PDF form, also known as an AcroForm, is a collection of fields used to gather information interactively from the PDF document users. A PDF document may contain any number of fields appearing on any combination of pages, all of which make up a single, global interactive form spanning the entire document.

Using the HiQPdf library you can create PDF documents with interactive forms containing the following types of fields: check boxes, text boxes, list boxes, combo boxes, radio buttons groups, submit and reset buttons.

Create Interactive PDF Forms Demo

In this demo you can learn how to create a PDF form with various fields and how to submit the values entered in the form to a web page. You can choose what type of fields to include in form and also the URL where to GET or POST the values entered in form.

When the Submit button of the PDF form is pressed, the PDF viewer will make a GET or a POST request to the URL below function of the selected method.

When the selected method is GET the form fields names and values will be added as key-value pairs in the query string of the URL and they can be accessed in ASP.NET using the Request.QueryString collection. When the selected method is POST the form fields names and values will be posted as key-value pairs to the URL and they can be accessed in ASP.NET using the Request.Form collection.

Demo Source Code

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

using HiQPdfClient;

namespace HiQPdf_Demo.Controllers
{
    public class CreateAndSubmitFormsController : Controller
    {
        // GET: CreateAndSubmitForms
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult CreatePdf(IFormCollection collection)
        {
            string serverIP = collection["textBoxServerIP"];
            uint serverPort = uint.Parse(collection["textBoxServerPort"]);
            string serverPassword = collection["textBoxServerPassword"];

            // create a PDF document
            PdfDocument document = new PdfDocument(serverIP, serverPort);

            // use server password if necessary
            if (serverPassword.Length > 0)
                document.ServerPassword = serverPassword;

            // set a demo serial number
            document.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ==";

            // add a page to document
            PdfPage page = document.AddPage();

            // create true type fonts that can be used in document            
            PdfFont newTimesFont = document.CreateFontFromName("Times New Roman", 10, false);

            // create a standard font that can be used in document
            PdfFont helveticaStd = document.CreateStandardFont(PdfStandardFont.Helvetica, 10);

            float crtXPos = 10;
            float crtYPos = 10;

            #region Add Check Box Field

            if (collection["checkBoxAddCheckBox"].Count > 0)
            {
                // add a check box field to PDF form
                PdfFormCheckBox checkBoxField = document.Form.AddCheckBox(page, new RectangleFloat(crtXPos, crtYPos, 10, 10));

                checkBoxField.Checked = collection["checkBoxCheckedState"].Count > 0;

                // common field properties 
                checkBoxField.Name = "cb";
                checkBoxField.ToolTip = "Click to change the checked state";
                checkBoxField.Required = false;
                checkBoxField.ReadOnly = false;
                checkBoxField.Flatten = false;

                // advance the current drawing position in PDF page
                crtYPos = checkBoxField.BoundingRectangle.Bottom + 5;
            }

            #endregion

            #region Add Text Box Field

            if (collection["checkBoxAddTextBox"].Count > 0)
            {
                string initialText = collection["textBoxInitialText"];
                PdfFormTextBox textBoxField = document.Form.AddTextBox(page, new RectangleFloat(crtXPos, crtYPos, 300, 50), initialText, newTimesFont);

                textBoxField.IsMultiLine = collection["checkBoxMultiline"].Count > 0;
                textBoxField.IsPassword = collection["checkBoxIsPassword"].Count > 0;

                textBoxField.Style.ForeColor = PdfColor.Navy;
                textBoxField.Style.BackColor = PdfColor.WhiteSmoke;
                textBoxField.Style.BorderStyle = PdfBorderStyle.FixedSingle;
                textBoxField.Style.BorderColor = PdfColor.Green;

                // common field properties
                textBoxField.Name = "tb";
                textBoxField.ToolTip = "Please enter some text";
                textBoxField.Required = false;
                textBoxField.ReadOnly = false;
                textBoxField.DefaultValue = "Default text";
                textBoxField.Flatten = false;

                // advance the current drawing position in PDF page
                crtYPos = textBoxField.BoundingRectangle.Bottom + 5;
            }

            #endregion

            #region Add  List Box Field

            if (collection["checkBoxAddListBox"].Count > 0)
            {
                string[] listValues = collection["textBoxListBoxValues"][0].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                PdfFormListBox listBoxField = document.Form.AddListBox(page, new RectangleFloat(crtXPos, crtYPos, 300, 50), listValues, helveticaStd);

                // common field properties
                listBoxField.Name = "lb";
                listBoxField.ToolTip = "Select an element from the list";
                listBoxField.Required = false;
                listBoxField.ReadOnly = false;
                if (listValues.Length > 0)
                    listBoxField.DefaultValue = listValues[0];
                listBoxField.Flatten = false;

                // advance the current drawing position in PDF page
                crtYPos = listBoxField.BoundingRectangle.Bottom + 5;
            }

            #endregion

            #region Add Combo Box Field

            if (collection["checkBoxAddComboBox"].Count > 0)
            {
                string[] listValues = collection["textBoxComboBoxValues"][0].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                PdfFormComboBox comboBoxField = document.Form.AddComboBox(page, new RectangleFloat(crtXPos, crtYPos, 300, 15), listValues, helveticaStd);

                comboBoxField.Editable = collection["checkBoxEditableCombo"].Count > 0;

                // common field properties
                comboBoxField.Name = "combo";
                comboBoxField.ToolTip = "Select an element from the combo drop down";
                comboBoxField.Required = false;
                comboBoxField.ReadOnly = false;
                if (listValues.Length > 0)
                    comboBoxField.DefaultValue = listValues[0];
                comboBoxField.Flatten = false;

                // advance the current drawing position in PDF page
                crtYPos = comboBoxField.BoundingRectangle.Bottom + 5;
            }

            #endregion

            #region Add Radio Buttons Group Field

            if (collection["checkBoxAddRadioButtons"].Count > 0)
            {
                PdfFormRadioButtonsGroup radioGroup = document.Form.AddRadioButtonsGroup(page);

                PdfFormRadioButton rb1 = radioGroup.AddRadioButton(new RectangleFloat(crtXPos, crtYPos, 10, 10), "rb1", page);
                PdfFormRadioButton rb2 = radioGroup.AddRadioButton(new RectangleFloat(crtXPos + 20, crtYPos, 10, 10), "rb2", page);

                radioGroup.SetCheckedRadioButton(rb2);

                radioGroup.Name = "rg";

                // advance the current drawing position in PDF page
                crtYPos = rb1.BoundingRectangle.Bottom + 20;
            }

            #endregion

            #region Create the Submit Button

            // create the Submit button
            PdfFormButton submitButton = document.Form.AddButton(page, new RectangleFloat(crtXPos, crtYPos, 100, 20), "Submit Form", newTimesFont);
            submitButton.Name = "submitButton";

            // create the submit action with the submit URL
            PdfSubmitFormAction submitAction = new PdfSubmitFormAction(collection["textBoxSubmitUrl"]);
            // set the action flags such that the form values are submitted in HTML form format
            submitAction.Flags |= PdfFormSubmitFlags.ExportFormat;
            if (collection["SubmitMethod"] == "radioButtonGet")
                submitAction.Flags |= PdfFormSubmitFlags.GetMethod;

            // set the submit button action
            submitButton.Action = submitAction;

            #endregion

            #region Create the Reset Button

            if (collection["checkBoxAddResetButton"].Count > 0)
            {
                // create the reset button
                PdfFormButton resetButton = document.Form.AddButton(page, new RectangleFloat(crtXPos + 120, crtYPos, 100, 20),
                                                        "Reset Form", newTimesFont);
                resetButton.Name = "resetButton";

                // create the reset action
                PdfResetFormAction resetAction = new PdfResetFormAction();

                // set the reset button action
                resetButton.Action = resetAction;
            }

            #endregion

            try
            {
                // write the PDF document to a memory buffer
                byte[] pdfBuffer = document.WriteToMemory();

                FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf");
                fileResult.FileDownloadName = "CreateForms.pdf";

                return fileResult;
            }
            finally
            {
                document.Close();
            }
        }
    }
}