HiQPdf Documentation

Create and Submit Forms

Quickly Create High Quality PDFs

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#
private void buttonCreatePdf_Click(object sender, EventArgs e)
{
    // create a PDF document
    PdfDocument document = new PdfDocument();

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

    // create true type fonts that can be used in document
    Font ttfFont = new Font("Times New Roman", 10, System.Drawing.GraphicsUnit.Point);
    PdfFont newTimesFont = document.CreateFont(ttfFont);
    PdfFont newTimesFontEmbed = document.CreateFont(ttfFont, true);

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

    float crtXPos = 10;
    float crtYPos = 10;

    #region Add Check Box Field

    if (checkBoxAddCheckBox.Checked)
    {
        // add a check box field to PDF form
        PdfFormCheckBox checkBoxField = document.Form.AddCheckBox(page, new RectangleF(crtXPos, crtYPos, 10, 10));

        checkBoxField.Checked = checkBoxCheckedState.Checked;

        // 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 (checkBoxAddTextBox.Checked)
    {
        string initialText = textBoxInitialText.Text;
        PdfFormTextBox textBoxField = document.Form.AddTextBox(page, new RectangleF(crtXPos, crtYPos, 300, 50), initialText, newTimesFontEmbed);

        textBoxField.IsMultiLine = checkBoxMultiline.Checked;
        textBoxField.IsPassword = checkBoxIsPassword.Checked;

        textBoxField.Style.ForeColor = Color.Navy;
        textBoxField.Style.BackColor = Color.WhiteSmoke;
        textBoxField.Style.BorderStyle = PdfBorderStyle.FixedSingle;
        textBoxField.Style.BorderColor = Color.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 (checkBoxAddListBox.Checked)
    {
        string[] listValues = textBoxListBoxValues.Text.Split(new char[]{','}, StringSplitOptions.RemoveEmptyEntries);

        PdfFormListBox listBoxField = document.Form.AddListBox(page, new RectangleF(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;
        listBoxField.DefaultValue = listValues.Length > 0 ? listValues[0] : null;
        listBoxField.Flatten = false;

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

    #endregion

    #region Add Combo Box Field

    if (checkBoxAddComboBox.Checked)
    {
        string[] listValues = textBoxComboBoxValues.Text.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

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

        comboBoxField.Editable = checkBoxEditableCombo.Checked;

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

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

    #endregion

    #region Add Radio Buttons Group Field

    if (checkBoxAddRadioButtons.Checked)
    {
        PdfFormRadioButtonsGroup radioGroup = document.Form.AddRadioButtonsGroup(page);

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

        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 RectangleF(crtXPos, crtYPos, 100, 20), "Submit Form", newTimesFont);
    submitButton.Name = "submitButton";

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

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

    #endregion

    #region Create the Reset Button

    if (checkBoxAddResetButton.Checked)
    {
        // create the reset button
        PdfFormButton resetButton = document.Form.AddButton(page, new RectangleF(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

    Cursor = Cursors.WaitCursor;
    string pdfFile = Application.StartupPath + @"\DemoOutput\CreateForms.pdf";
    try
    {
        document.WriteToFile(pdfFile);
    }
    catch (Exception ex)
    {
        MessageBox.Show(String.Format("Cannot create the PDF document. {0}", ex.Message));
        return;
    }
    finally
    {
        document.Close();
        Cursor = Cursors.Arrow;
    }

    // open the created PDF document
    try
    {
        System.Diagnostics.Process.Start(pdfFile);
    }
    catch (Exception ex)
    {
        MessageBox.Show(String.Format("The PDF document was created but cannot open '{0}'. {1}", pdfFile, ex.Message));
    }
}
See Also

Other Resources