News Category

Add form field to an existing PDF via Spire.PDF

2015-04-03 09:13:46 Written by  support iceblue
Rate this item
(2 votes)

PDF form is often used to display, catch and edit data. People can fill blanks with data and submit data to server. Spire.PDF now enables users to add fields and create form in existing PDF document.

Here are the steps:

Step 1: Create PDF document, and load a blank PDF file, then get the first page.

PdfDocument pdf = new PdfDocument("Blank.pdf");
pdf.AllowCreateForm = (pdf.Form == null) ? true : false;
PdfPageBase page = pdf.Pages[0];

Step 2: Set font and font color. Preset coordinate.

PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 13f);
PdfBrush brush = PdfBrushes.Black;
float x = 10;
float y = 10;
float tempX = 0;
float tempY = 0;

Step 3: Draw textbox.

string text = "Textbox: ";
page.Canvas.DrawString(text, font, brush, x, y);
tempX = font.MeasureString(text).Width + 15;
tempY = font.MeasureString(text).Height + 15;
PdfTextBoxField textbox = new PdfTextBoxField(page, "TextBox");
textbox.Bounds = new RectangleF(tempX, y, tempX * 2, 15);
textbox.BorderWidth = 0.75f;
textbox.BorderStyle = PdfBorderStyle.Solid;
pdf.Form.Fields.Add(textbox);

Step 4: Draw checkbox.

text = "Checkbox: ";
y += tempY;
page.Canvas.DrawString(text, font, brush, x, y);
tempX = font.MeasureString(text).Width + 15;
tempY = font.MeasureString(text).Height + 15;
PdfCheckBoxField checkbox = new PdfCheckBoxField(page, "CheckBox");
checkbox.Bounds = new RectangleF(tempX, y, 15, 15);
checkbox.BorderWidth = 0.75f;
checkbox.Style = PdfCheckBoxStyle.Cross;
pdf.Form.Fields.Add(checkbox);

Step 5: Draw Listbox and add content.

//ListBox
text = "Listbox: ";
y += tempY;
page.Canvas.DrawString(text, font, brush, x, y);
tempX = font.MeasureString(text).Width + 15;
tempY = font.MeasureString(text).Height + 15;
PdfListBoxField listbox = new PdfListBoxField(page, "ListBox");
listbox.Bounds = new RectangleF(tempX, y, tempX * 2, tempY * 2);
listbox.BorderWidth = 0.75f;
for (int i = 0; i < 3; i++)
{
     string tempText = string.Format("Text {0}", i);
     string tempValue = string.Format("Value {0}", i);
     listbox.Items.Add(new PdfListFieldItem(tempText, tempValue));
}
pdf.Form.Fields.Add(listbox);

Step 6: Draw RadioButton.

//RadioButton
text = "Radiobutton: ";
y += tempY * 2 + 15;
page.Canvas.DrawString(text, font, brush, x, y);
tempX = font.MeasureString(text).Width + 15;
tempY = font.MeasureString(text).Height + 15;
PdfRadioButtonListField radiobutton = new PdfRadioButtonListField(page, "RadioButton");
for (int i = 0; i < 3; i++)
{
     PdfRadioButtonListItem item = new PdfRadioButtonListItem(string.Format("rb{0}", i));
     item.BorderWidth = 0.75f;
     item.Bounds = new RectangleF(tempX + i * 20, y, 15, 15);
     radiobutton.Items.Add(item);
}
pdf.Form.Fields.Add(radiobutton);

Step 7: Draw combobox and add content.

//ComboBox
text = "ComboBox: ";
y += tempY;
page.Canvas.DrawString(text, font, brush, x, y);
tempX = font.MeasureString(text).Width + 15;
tempY = font.MeasureString(text).Height + 15;
PdfComboBoxField combobox = new PdfComboBoxField(page, "ComboBox");
combobox.Bounds = new RectangleF(tempX, y, tempX, 15);
combobox.BorderWidth = 0.75f;
for (int i = 0; i < 3; i++)
{
    string tempText = string.Format("Text {0}", i);
    string tempValue = string.Format("Value {0}", i);
    combobox.Items.Add(new PdfListFieldItem(tempText, tempValue));
}
pdf.Form.Fields.Add(combobox);

Step 8: Save and review

loDoc.SaveToFile("result.pdf");
System.Diagnostics.Process.Start("result.pdf");

Result screenshot:

How to add form field to an existing PDF via Spire.PDF

Full Code:

using Spire.Pdf;
using Spire.Pdf.Fields;
using Spire.Pdf.Graphics;
using System.Drawing;


namespace AddFormFieldToExistingPDF
{
    class Program
    {
        static void Main(string []args)
        {
            PdfDocument pdf = new PdfDocument("Blank.pdf");
            pdf.AllowCreateForm = (pdf.Form == null) ? true : false;
            PdfPageBase page = pdf.Pages[0];


            PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 13f);
            PdfBrush brush = PdfBrushes.Black;
            float x = 10;
            float y = 10;
            float tempX = 0;
            float tempY = 0;

            //TextBox
            string text = "Textbox: ";
            page.Canvas.DrawString(text, font, brush, x, y);
            tempX = font.MeasureString(text).Width + 15;
            tempY = font.MeasureString(text).Height + 15;
            PdfTextBoxField textbox = new PdfTextBoxField(page, "TextBox");
            textbox.Bounds = new RectangleF(tempX, y, tempX * 2, 15);
            textbox.BorderWidth = 0.75f;
            textbox.BorderStyle = PdfBorderStyle.Solid;
            pdf.Form.Fields.Add(textbox);

            //CheckBox
            text = "Checkbox: ";
            y += tempY;
            page.Canvas.DrawString(text, font, brush, x, y);
            tempX = font.MeasureString(text).Width + 15;
            tempY = font.MeasureString(text).Height + 15;
            PdfCheckBoxField checkbox = new PdfCheckBoxField(page, "CheckBox");
            checkbox.Bounds = new RectangleF(tempX, y, 15, 15);
            checkbox.BorderWidth = 0.75f;
            checkbox.Style = PdfCheckBoxStyle.Cross;
            pdf.Form.Fields.Add(checkbox);

            //ListBox
            text = "Listbox: ";
            y += tempY;
            page.Canvas.DrawString(text, font, brush, x, y);
            tempX = font.MeasureString(text).Width + 15;
            tempY = font.MeasureString(text).Height + 15;
            PdfListBoxField listbox = new PdfListBoxField(page, "ListBox");
            listbox.Bounds = new RectangleF(tempX, y, tempX * 2, tempY * 2);
            listbox.BorderWidth = 0.75f;
            for (int i = 0; i < 3; i++)
            {
                string tempText = string.Format("Text {0}", i);
                string tempValue = string.Format("Value {0}", i);
                listbox.Items.Add(new PdfListFieldItem(tempText, tempValue));
            }
            pdf.Form.Fields.Add(listbox);

            //RadioButton
            text = "Radiobutton: ";
            y += tempY * 2 + 15;
            page.Canvas.DrawString(text, font, brush, x, y);
            tempX = font.MeasureString(text).Width + 15;
            tempY = font.MeasureString(text).Height + 15;
            PdfRadioButtonListField radiobutton = new PdfRadioButtonListField(page, "RadioButton");
            for (int i = 0; i < 3; i++)
            {
                PdfRadioButtonListItem item = new PdfRadioButtonListItem(string.Format("rb{0}", i));
                item.BorderWidth = 0.75f;
                item.Bounds = new RectangleF(tempX + i * 20, y, 15, 15);
                radiobutton.Items.Add(item);
            }
            pdf.Form.Fields.Add(radiobutton);

            //ComboBox
            text = "ComboBox: ";
            y += tempY;
            page.Canvas.DrawString(text, font, brush, x, y);
            tempX = font.MeasureString(text).Width + 15;
            tempY = font.MeasureString(text).Height + 15;
            PdfComboBoxField combobox = new PdfComboBoxField(page, "ComboBox");
            combobox.Bounds = new RectangleF(tempX, y, tempX, 15);
            combobox.BorderWidth = 0.75f;
            for (int i = 0; i < 3; i++)
            {
                string tempText = string.Format("Text {0}", i);
                string tempValue = string.Format("Value {0}", i);
                combobox.Items.Add(new PdfListFieldItem(tempText, tempValue));
            }
            pdf.Form.Fields.Add(combobox);

            pdf.SaveToFile("Result.pdf");
            System.Diagnostics.Process.Start("Result.pdf");
        }
    }
}

Additional Info

  • tutorial_title: Add form field to an existing PDF
Last modified on Sunday, 26 September 2021 02:13