Foxit Quick PDF Library

Programmatically extract form field data from PDF files

As well as enabling you to generate form fields and fill form fields, Foxit Quick PDF Library makes it easy to extract form field data or information about form fields from PDF files.

In the sample code below we demonstrate how to iterate through each page in a PDF to extract information about all of the AcroForm and XFA form fields in the PDF as well as the form field values. This sample code presumes that you don’t know the name of the form fields in advance. If you do know the name of the form fields that you wish to extract values from then you can use the GetFormFieldValueByTitle function to quickly retrieve this data. Look at the bottom of this article for sample code on how to use that function.

Retrieve form field summary and all values

DPL.LoadFromFile("pdf_form.pdf", "");

// Count pages of PDF
int xPageCount = DPL.PageCount();

// Go through each page and get form field summary
for (int i = 1; i <= xPageCount; i++)
{
	DPL.SelectPage(i);

	txtOutput.AppendText("=== Page # " + i + " ===");
	txtOutput.AppendText(Environment.NewLine);
	txtOutput.AppendText(Environment.NewLine);

	int XFAFormFieldCount = DPL.GetXFAFormFieldCount();
	int AcroFormFieldCount = DPL.FormFieldCount();

	txtOutput.AppendText("XFA Field Count: " + XFAFormFieldCount);
	txtOutput.AppendText(Environment.NewLine);
	txtOutput.AppendText("AcroForm Field Count: " + AcroFormFieldCount);
	txtOutput.AppendText(Environment.NewLine);
	txtOutput.AppendText(Environment.NewLine);

	// Process AcroForm Form Fields

	if (AcroFormFieldCount == 0)
	{
		txtOutput.AppendText("This page does not contain any AcroForm form fields.");
		txtOutput.AppendText(Environment.NewLine);
	}
	else
	{
		txtOutput.AppendText("!!!!!! === AcroForm Form Fields === !!!!!! ");
		txtOutput.AppendText(Environment.NewLine);
		txtOutput.AppendText(Environment.NewLine);

		for (int y = 1; y <= AcroFormFieldCount; y++)
		{
			string AcroFieldTitle = DPL.GetFormFieldTitle(y);
			string AcroFieldValue = DPL.GetFormFieldValueByTitle(AcroFieldTitle);
			int AcroFieldKids = DPL.GetFormFieldKidCount(y);

			txtOutput.AppendText("AcroForm Field # " + y);
			txtOutput.AppendText(Environment.NewLine);
			txtOutput.AppendText("AcroForm Name: " + AcroFieldTitle);
			txtOutput.AppendText(Environment.NewLine);
			txtOutput.AppendText("Field Value: " + AcroFieldValue);
			txtOutput.AppendText(Environment.NewLine);
			txtOutput.AppendText("Total Kids: " + AcroFieldKids);
			txtOutput.AppendText(Environment.NewLine);

			int FormFieldType = DPL.GetFormFieldType(y);

			switch (FormFieldType)
			{
				case 1:
					txtOutput.AppendText("Field Type: Text");
					txtOutput.AppendText(Environment.NewLine);
					break;
				case 2:
					txtOutput.AppendText("Field Type: Pushbutton");
					txtOutput.AppendText(Environment.NewLine);
					break;
				case 3:
					txtOutput.AppendText("Field Type: Checkbox");
					txtOutput.AppendText(Environment.NewLine);
					break;
				case 4:
					txtOutput.AppendText("Field Type: Radiobutton");
					txtOutput.AppendText(Environment.NewLine);
					break;
				case 5:
					txtOutput.AppendText("Field Type: Choice");
					txtOutput.AppendText(Environment.NewLine);
					break;
				case 6:
					txtOutput.AppendText("Field Type: Signature");
					txtOutput.AppendText(Environment.NewLine);
					break;
				case 7:
					txtOutput.AppendText("Field Type: Parent");
					txtOutput.AppendText(Environment.NewLine);
					break;
				default:
					txtOutput.AppendText("Field Type: Unknown");
					txtOutput.AppendText(Environment.NewLine);
					break;
			}
			txtOutput.AppendText(Environment.NewLine);
		}
	}

	// Process XFA Form Fields

	if (XFAFormFieldCount == 0)
	{
		txtOutput.AppendText("This page does not contain any XFA form fields.");
		txtOutput.AppendText(Environment.NewLine);
	}
	else
	{
		txtOutput.AppendText("!!!!!! === XFA Form Fields === !!!!!! ");
		txtOutput.AppendText(Environment.NewLine);
		txtOutput.AppendText(Environment.NewLine);

		for (int y = 1; y <= XFAFormFieldCount; y++)
		{
			string XFAFieldName = DPL.GetXFAFormFieldName(y);
			string XFAFieldValue = DPL.GetXFAFormFieldValue(XFAFieldName);

			txtOutput.AppendText("XFA Field # " + y);
			txtOutput.AppendText(Environment.NewLine);
			txtOutput.AppendText("XFA Name: " + XFAFieldName);
			txtOutput.AppendText(Environment.NewLine);
			txtOutput.AppendText("Field Value: " + XFAFieldValue);
			txtOutput.AppendText(Environment.NewLine);

			int xFormFieldType = DPL.GetFormFieldType(y);

			switch (xFormFieldType)
			{
				case 1:
					txtOutput.AppendText("Field Type: Text");
					txtOutput.AppendText(Environment.NewLine);
					break;
				case 2:
					txtOutput.AppendText("Field Type: Pushbutton");
					txtOutput.AppendText(Environment.NewLine);
					break;
				case 3:
					txtOutput.AppendText("Field Type: Checkbox");
					txtOutput.AppendText(Environment.NewLine);
					break;
				case 4:
					txtOutput.AppendText("Field Type: Radiobutton");
					txtOutput.AppendText(Environment.NewLine);
					break;
				case 5:
					txtOutput.AppendText("Field Type: Choice");
					txtOutput.AppendText(Environment.NewLine);
					break;
				case 6:
					txtOutput.AppendText("Field Type: Signature");
					txtOutput.AppendText(Environment.NewLine);
					break;
				case 7:
					txtOutput.AppendText("Field Type: Parent");
					txtOutput.AppendText(Environment.NewLine);
					break;
				default:
					txtOutput.AppendText("Field Type: Unknown");
					txtOutput.AppendText(Environment.NewLine);
					break;
			}
			txtOutput.AppendText(Environment.NewLine);
		}
	}
	txtOutput.AppendText(Environment.NewLine);
}

Retrieve form field value by title

If you’re working with AcroForm form fields and know the title of the form field you want to retrieve a value from then you can use the GetFormFieldValueByTitle function to quickly retrieve the data without first iterating through all of the form fields in the PDF.

string FirstNameFieldValue = DPL.GetFormFieldValueByTitle("FirstName");

This article refers to a deprecated product. If you are looking for support for Foxit PDF SDK, please click here.

Updated on May 16, 2022

Was this article helpful?
Thanks for your feedback. If you have a comment on how to improve the article, you can write it here: