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
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Forms();
// Open document
Document pdfDocument = new Document(dataDir + "GetValuesFromAllFields.pdf");
// Get values from all fields
foreach (Field formField in pdfDocument.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
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Forms();
// Open document
Document pdfDocument = new Document(dataDir + "GetValueFromField.pdf");
// Get a field
TextBoxField textBoxField = pdfDocument.Form["textbox1"] as 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
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Forms();
// Open document
Document pdfDocument = new Document(dataDir + "GetValueFromField.pdf");
SubmitFormAction act = pdfDocument.Form[1].OnActivated as SubmitFormAction;
if (act != null)
{
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:
- Open the PDF file using the Document object.
- Get the form from the document’s Forms collection.
- Specify the rectangular region and pass it to the Form object’s GetFieldsInRect method. A Fields collection is returned.
- 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
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Forms();
// Open pdf file
Aspose.Pdf.Document doc = new Aspose.Pdf.Document(dataDir + "GetFieldsFromRegion.pdf");
// Create rectangle object to get fields in that area
Aspose.Pdf.Rectangle rectangle = new Aspose.Pdf.Rectangle(35, 30, 500, 500);
// Get the PDF form
Aspose.Pdf.Forms.Form form = doc.Form;
// Get fields in the rectangular area
Aspose.Pdf.Forms.Field[] fields = form.GetFieldsInRect(rectangle);
// Display Field names and values
foreach (Field field in fields)
{
// Display image placement properties for all placements
Console.Out.WriteLine("Field Name: " + field.FullName + "-" + "Field Value: " + field.Value);
}