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.
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
}
]
}
curl --location 'https://api.dpdf.io/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.
- C# (.NET)
- Java
- Node.js
- PHP
- Go
- Python
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);
}
}
static async Run() {
var pdfEndpoint = new Pdf();
pdfEndpoint.apiKey = "DP.--api-key--";
var resource = new PdfResource("./resources/form-field-flatten/fw9AcroForm_14.pdf", "fw9AcroForm_14.pdf");
var input = new PdfInput(resource);
pdfEndpoint.inputs.push(input);
var field = new FormField("topmostSubform[0].Page1[0].f1_1[0]", "Any Company, Inc.");
field.remove = true;
pdfEndpoint.formFields.push(field);
var field1 = new FormField("topmostSubform[0].Page1[0].f1_2[0]", "Any Company");
pdfEndpoint.formFields.push(field1);
var field2 = new FormField("topmostSubform[0].Page1[0].FederalClassification[0].c1_1[0]", "1");
field2.remove = true;
pdfEndpoint.formFields.push(field2);
var field3 = new FormField("topmostSubform[0].Page1[0].Address[0].f1_7[0]", "123 Main Street");
pdfEndpoint.formFields.push(field3);
var field4 = new FormField("topmostSubform[0].Page1[0].Address[0].f1_8[0]", "Washington, DC 22222");
pdfEndpoint.formFields.push(field4);
var field5 = new FormField("topmostSubform[0].Page1[0].f1_9[0]", "Any Requester");
pdfEndpoint.formFields.push(field5);
var field6 = new FormField("topmostSubform[0].Page1[0].f1_10[0]", "17288825617");
pdfEndpoint.formFields.push(field6);
var field7 = new FormField("topmostSubform[0].Page1[0].EmployerID[0].f1_14[0]", "52");
field7.remove = true;
pdfEndpoint.formFields.push(field7);
var field8 = new FormField("topmostSubform[0].Page1[0].EmployerID[0].f1_15[0]", "1234567");
pdfEndpoint.formFields.push(field8);
var res = await pdfEndpoint.process();
var res = await pdfEndpoint.process();
if (res.isSuccessful) {
var outFile = "./output/form-flatten-delete-nodejs-output.pdf";
var outStream = fs.createWriteStream(outFile);
outStream.write(res.content);
outStream.close();
console.log("Pdf was generated and saved at: ", outFile);
}
else {
console.log(res.errorJson);
}
}
public static void Run(String apiKey, String basePath)
{
Pdf pdf = new Pdf();
pdf.setApiKey(apiKey);
PdfResource resource = new PdfResource(basePath + "fw9AcroForm_14.pdf");
PdfInput input = new PdfInput(resource);
pdf.getInputs().add(input);
FormField field = new FormField("topmostSubform[0].Page1[0].f1_1[0]", "Any Company, Inc.");
field.setFlatten(true);
pdf.getFormFields().add(field);
FormField field1 = new FormField("topmostSubform[0].Page1[0].f1_2[0]", "Any Company");
field1.setRemove(true);
pdf.getFormFields().add(field1);
FormField field2 = new FormField("topmostSubform[0].Page1[0].FederalClassification[0].c1_1[0]", "1");
field2.setFlatten(true);
pdf.getFormFields().add(field2);
FormField field3 = new FormField("topmostSubform[0].Page1[0].Address[0].f1_7[0]", "123 Main Street");
field3.setFlatten(false);
pdf.getFormFields().add(field3);
FormField field4 = new FormField("topmostSubform[0].Page1[0].Address[0].f1_8[0]", "Washington, DC 22222");
field4.setRemove(true);
pdf.getFormFields().add(field4);
FormField field5 = new FormField("topmostSubform[0].Page1[0].f1_9[0]", "Any Requester");
field5.setRemove(true);
pdf.getFormFields().add(field5);
PdfResponse response = pdf.process();
if(response.getIsSuccessful())
{
try {
FileUtils.writeByteArrayToFile(new File(DynamicPdfCloudApiExamples.OUTPUT_PATH + "/form-flatten-delete-output.pdf"),
response.getContent());
} catch (IOException e) {
e.printStackTrace();
}
} else
{
System.out.println(response.getErrorJson());
}
}
public static function Run(string $apikey, string $path, string $outputPath)
{
$pdf = new Pdf();
$pdf->ApiKey = $apikey;
$pdfInput = $pdf->AddPdf(new PdfResource($path . "fw9AcroForm_14.pdf"));
$formField = new FormField("topmostSubform[0].Page1[0].f1_1[0]", "Any Company, Inc.");
$formField->Flatten = true;
$formField2 = new FormField("topmostSubform[0].Page1[0].f1_2[0]", "Any Company");
$formField2->Remove = true;
$formField3 = new FormField("topmostSubform[0].Page1[0].FederalClassification[0].c1_1[0]", "1");
$formField3->Flatten = true;
$formField4 = new FormField("topmostSubform[0].Page1[0].Address[0].f1_7[0]", "123 Main Street");
$formField5 = new FormField("topmostSubform[0].Page1[0].Address[0].f1_8[0]", "Washington, DC 22222");
$formField6 = new FormField("topmostSubform[0].Page1[0].f1_9[0]", "Any Requester");
$formField7 = new FormField("topmostSubform[0].Page1[0].f1_10[0]", "17288825617");
$formField8 = new FormField("topmostSubform[0].Page1[0].EmployerID[0].f1_14[0]", "1234567");
$formField9 = new FormField("topmostSubform[0].Page1[0].EmployerID[0].f1_15[0]", "1234567");
$formField9->Remove = true;
array_push($pdf->FormFields, $formField);
array_push($pdf->FormFields, $formField2);
array_push($pdf->FormFields, $formField3);
array_push($pdf->FormFields, $formField4);
array_push($pdf->FormFields, $formField5);
array_push($pdf->FormFields, $formField6);
array_push($pdf->FormFields, $formField7);
array_push($pdf->FormFields, $formField8);
array_push($pdf->FormFields, $formField9);
$response = $pdf->Process();
if($response->IsSuccessful)
{
file_put_contents($outputPath . "form-flatten-output.pdf", $response->Content);
} else {
echo("Error: ");
echo($response->StatusCode);
echo($response->ErrorJson);
}
}
func main() {
pdfAcro := endpoint.NewPdf()
pdfAcro.Endpoint.BaseUrl = "https://api.dpdf.io"
pdfAcro.Endpoint.ApiKey = "DP--api-key--"
outputPath := "./output/"
basePath := "./resources/fill-acro-form-pdf-endpoint/"
pdfResource := resource.NewPdfResourceWithResourcePath(basePath+"fw9AcroForm_18.pdf", "fw9AcroForm_18.pdf")
pdfInput := input.NewPdfWithResource(pdfResource)
pdfAcro.Inputs = append(pdfAcro.Inputs, pdfInput)
field1 := endpoint.NewFormFieldWithValue("topmostSubform[0].Page1[0].f1_1[0]", "Any Company, Inc.")
field1.Remove = true
pdfAcro.FormFields = append(pdfAcro.FormFields, *field1)
field2 := endpoint.NewFormFieldWithValue("topmostSubform[0].Page1[0].f1_2[0]", "Any Company")
field2.Flatten = true
pdfAcro.FormFields = append(pdfAcro.FormFields, *field2)
field3 := endpoint.NewFormFieldWithValue("topmostSubform[0].Page1[0].FederalClassification[0].c1_1[0]", "1")
field3.Flatten = false
pdfAcro.FormFields = append(pdfAcro.FormFields, *field3)
field4 := endpoint.NewFormFieldWithValue("topmostSubform[0].Page1[0].Address[0].f1_7[0]", "123 Main Street")
pdfAcro.FormFields = append(pdfAcro.FormFields, *field4)
field5 := endpoint.NewFormFieldWithValue("topmostSubform[0].Page1[0].Address[0].f1_8[0]", "Washington, DC 22222")
field5.Remove = true
pdfAcro.FormFields = append(pdfAcro.FormFields, *field5)
resp := pdfAcro.Process()
res := <-resp
if res.IsSuccessful() == false {
if res.ClientError() != nil {
fmt.Print("Failed with error: " + res.ClientError().Error())
} else {
fmt.Print("Failed with error: " + res.ErrorJson())
}
} else {
os.WriteFile(outputPath+"form-flatten-delete-output.pdf",
res.Content().Bytes(), os.ModeType)
}
}
def form_field_flatten_delete(apikey, full_path):
pdf=Pdf()
pdf.api_key = apikey
resource = PdfResource(full_path + "fw9AcroForm_14.pdf", "fw9AcroForm_14.pdf")
input = PdfInput(resource)
pdf.inputs.append(input)
field = FormField("topmostSubform[0].Page1[0].f1_1[0]", "Any Company, Inc.")
field.flatten = True
pdf.form_fields.append(field)
field1 = FormField("topmostSubform[0].Page1[0].f1_2[0]", "Any Company")
field1.remove = True
pdf.form_fields.append(field1)
field2 = FormField("topmostSubform[0].Page1[0].FederalClassification[0].c1_1[0]", "1")
field2.flatten = True
pdf.form_fields.append(field2)
field3 = FormField("topmostSubform[0].Page1[0].Address[0].f1_7[0]", "123 Main Street")
field3.Flatten = False
pdf.form_fields.append(field3)
field4 = FormField("topmostSubform[0].Page1[0].Address[0].f1_8[0]", "Washington, DC 22222")
field4.flatten = False
pdf.form_fields.append(field4)
field5 = FormField("topmostSubform[0].Page1[0].f1_9[0]", "Any Requester")
field5.remove = True
pdf.form_fields.append(field5)
response = pdf.process()
if response.is_successful:
with open(output_path + "form-flatten-delete-output-python.pdf", "wb") as file:
file.write(response.content)
else:
print(response.error_id)
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.