Extract AcroForm - Extract Form Data from PDF in C#

The following code snippet also work with Aspose.PDF.Drawing library.

Extract data from form

Get Values from all the Fields of PDF Document

To get values from all the fields in a PDF document, you need to navigate through all the form fields and then get the value using the Value property. Get each field from the Form collection, in the base field type called Field and access its Value property.

The following C# code snippets show how to get the values of all the fields from a PDF document.

// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET

private static void GetValuesFromFields()
{
    // The path to the documents directory
    string dataDir = RunExamples.GetDataDir_AsposePdf_Forms();

    // Open document
    using (var document = new Aspose.Pdf.Document(dataDir + "GetValuesFromAllFields.pdf"))
	{
		// Get values from all fields
		foreach (Aspose.Pdf.Forms.Field formField in document.Form)
		{
			Console.WriteLine("Field Name : {0} ", formField.PartialName);
			Console.WriteLine("Value : {0} ", formField.Value);
		}
	}
}

Get Value from an Individual Field of PDF Document

The form field’s Value property allows you to get the value of a particular field. To get the value, get the form field from the Document object’s Form collection. This C# example selects a TextBoxField and retrieves its value using the Value property.

// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET

private static void GetValueFromField()
{
    // The path to the documents directory
    string dataDir = RunExamples.GetDataDir_AsposePdf_Forms();

    // Open document
    using (var document = new Aspose.Pdf.Document(dataDir + "GetValueFromField.pdf"))
	{
		// Get a field
		if (document.Form["textbox1"] is Aspose.Pdf.Forms.TextBoxField textBoxField)
		{
			// Get field value
			Console.WriteLine("PartialName : {0} ", textBoxField.PartialName);
			Console.WriteLine("Value : {0} ", textBoxField.Value);
		}
	}
}

To get the submit button’s URL, use the following lines of code.

// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET

private static void GetSubmitFormActionUrl()
{
    // The path to the documents directory
    string dataDir = RunExamples.GetDataDir_AsposePdf_Forms();

    // Open document
    using (var document = new Aspose.Pdf.Document(dataDir + "GetValueFromField.pdf"))
	{
		// Get the SubmitFormAction from the form field
		if (document.Form[1].OnActivated is Aspose.Pdf.Annotations.SubmitFormAction act)
		{
			// Output the URL of the SubmitFormAction
			Console.WriteLine(act.Url.Name);
		}
	}
}

Get Form Fields from a Specific Region of PDF File

Sometimes, you might know where in a document a form field is, but not have it’s name. For example, if all you have to go from is a schematic of a printed form. With Aspose.PDF for .NET, this is not a problem. You can find out which fields are in a given region of a PDF file. To get form fields from a specific region of a PDF file:

  1. Open the PDF file using the Document object.
  2. Get the form from the document’s Forms collection.
  3. Specify the rectangular region and pass it to the Form object’s GetFieldsInRect method. A Fields collection is returned.
  4. Use this to manipulate the fields.

The following C# code snippet shows how to get form fields in a specific rectangular region of a PDF file.

// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void GetFieldsFromRegion()
{
    // The path to the documents directory
    string dataDir = RunExamples.GetDataDir_AsposePdf_Forms();

    // Open pdf file
    using (var document = new Aspose.Pdf.Document(dataDir + "GetFieldsFromRegion.pdf"))
	{
		// Create rectangle object to get fields in that area
		var rectangle = new Aspose.Pdf.Rectangle(35, 30, 500, 500);

		// Get the PDF form
		var form = document.Form;

		// Get fields in the rectangular area
		var fields = form.GetFieldsInRect(rectangle);

		// Display Field names and values
		foreach (var field in fields)
		{
			// Display image placement properties for all placements
			Console.Out.WriteLine("Field Name: " + field.FullName + "-" + "Field Value: " + field.Value);
		}
	}
}