Skip to main content

Remove or Flatten Form Fields


Complete a PDF with form fields and programmatically flatten or remove one or more of the fields and then save the modified form as a new completed PDF document.

The DynamicPDF API makes flattening and removing form fields programmatically from forms a breeze. In this example, you complete a form and then flatten and delete several fields.

tip

Check out Getting Started and Task Roadmap if you are new to The DynamicPDF API.

The first example illustrates calling the endpoint directly. The next example illustrates calling the endpoint using The DynamicPDF API's client libraries.

Calling Endpoint Directly

Create a JSON instructions document and call it from the pdf endpoint directly.

  • Define an input of type pdf containing the form to complete.
  • Create a formfields array and add each field name and desired value to the array.
  • Flatten the fields you wish to flatten and remove any fields you wish to delete.
{
"inputs": [
{
"type": "pdf",
"resourceName": "fw9AcroForm_14.pdf"
}
],
"formFields": [
{
"name": "topmostSubform[0].Page1[0].f1_1[0]",
"value": "Any Company, Inc.",
"flatten": true
},
{
"name": "topmostSubform[0].Page1[0].f1_2[0]",
"value": "Any Company",
"remove": true
},
{
"name": "topmostSubform[0].Page1[0].FederalClassification[0].c1_1[0]",
"value": "1",
"flatten": true
},
{
"name": "topmostSubform[0].Page1[0].Address[0].f1_7[0]",
"value": "123 Main Street",
"flatten": false
},
{
"name": "topmostSubform[0].Page1[0].Address[0].f1_8[0]",
"value": "Washington, DC 22222",
"remove": true
},
{
"name": "topmostSubform[0].Page1[0].f1_9[0]",
"value": "Any Requester",
"remove": true
}
]
}

Run In Postman

curl --location 'https://api.dynamicpdf.com/v1.0/pdf'
--header 'Authorization: Bearer DP--api-key--'
--form 'Instructions=@"/C:/temp/solutions/form-field-flatten/instructions.json"' \
--form 'Resource=@"/C:/temp/solutions/form-field-flatten/fw9AcroForm_14.pdf"'

Calling Endpoint Using Client Library

Flattening and delete form fields of a form by calling the pdf programmatically using one of the DynamicPDF API's client libraries.

public static void Run(string apiKey, string basePath, string outputPath)
{
Pdf pdf = new Pdf();
pdf.ApiKey = apiKey;

PdfResource resource = new PdfResource(basePath + "fw9AcroForm_14.pdf");
PdfInput input = new PdfInput(resource);
pdf.Inputs.Add(input);

FormField field = new FormField("topmostSubform[0].Page1[0].f1_1[0]", "Any Company, Inc.");
field.Flatten = true;
pdf.FormFields.Add(field);

FormField field1 = new FormField("topmostSubform[0].Page1[0].f1_2[0]", "Any Company");
field1.Remove = true;
pdf.FormFields.Add(field1);

FormField field2 = new FormField("topmostSubform[0].Page1[0].FederalClassification[0].c1_1[0]", "1");
field2.Flatten = true;
pdf.FormFields.Add(field2);

FormField field3 = new FormField("topmostSubform[0].Page1[0].Address[0].f1_7[0]", "123 Main Street");
field3.Flatten = false;
pdf.FormFields.Add(field3);

FormField field4 = new FormField("topmostSubform[0].Page1[0].Address[0].f1_8[0]", "Washington, DC 22222");
field4.Remove = true;
pdf.FormFields.Add(field4);

FormField field5 = new FormField("topmostSubform[0].Page1[0].f1_9[0]", "Any Requester");
field5.Remove = true;
pdf.FormFields.Add(field5);

PdfResponse response = pdf.Process();

if (response.ErrorJson != null)
{
Console.WriteLine(response.ErrorJson));
}
else
{
File.WriteAllBytes(outputPath + "/form-field-flatten-output.pdf", response.Content);
}
}
Source: FormFieldFlattenAndRemove.cs
info

The pdf endpoint takes a JSON instructions document that provides instructions for creating, transforming, merging, and formatting inputs into a combined PDF. Refer to documentation on the instructions schema for information on how to use the pdf endpoint.