GuidePDF Form Operations

Detect fields

This documentation provides an example of how to use the FileforgeClient to detect form fields in a PDF document. This endpoint returns a list of form fields detected in the PDF document, along with their location, options and requirements.

Prerequisites

Ensure you have the following:

  • An API key for Fileforge as an environment variable: process.env.FILEFORGE_API_KEY
  • Node.js and npm installed.
  • The Fileforge Client installed.

Guide

Lets look into our PDF file with a form

For this example, we will use the following PDF file with form fields:

sample pdf file

Extract form fields from the PDF as object

1import { FileforgeClient } from "@fileforge/client";
2import * as fs from "fs";
3
4(async () => {
5 const ff = new FileforgeClient({
6 apiKey: process.env.FILEFORGE_API_KEY,
7 });
8
9 try {
10 const resultObject = await ff.pdf.form.detect(
11 new File(
12 [fs.readFileSync(__dirname + "/samples/form.pdf")],
13 "form.pdf",
14 {
15 type: "application/pdf",
16 },
17 ),
18 { options: {} },
19 );
20
21 console.log(resultObject);
22 } catch (error) {
23 console.error("Error during PDF form detect:", error);
24 throw error;
25 }
26 })();

Get all form field descriptions and locations from the response as follows

The reponse is a list of field descriptions and locations you can use to build a mapping with your data or to fill the document.

1[
2 {
3 "name":"Producer Name",
4 "required":false,
5 "readOnly":false,
6 "locations":[
7 {
8 "x":78.6,
9 "y":660.36,
10 "width":120.96000000000001,
11 "height":13.559999999999945
12 }
13 ],
14 "type":"PDFTextField",
15 "defaultValue":"",
16 "isPassword":false,
17 "isRichFormatted":false,
18 "isScrollable":true,
19 "isCombed":false,
20 "isMultiline":false,
21 "isFileSelector":false
22 },
23 {
24 "name":"Insured Name",
25 "required":false,
26 "readOnly":false,
27 "locations":[
28 {
29 "x":300.96,
30 "y":660.36,
31 "width":125.16000000000003,
32 "height":13.559999999999945
33 }
34 ],
35 "type":"PDFTextField",
36 "defaultValue":"",
37 "isPassword":false,
38 "isRichFormatted":false,
39 "isScrollable":true,
40 "isCombed":false,
41 "isMultiline":false,
42 "isFileSelector":false
43 },
44 {
45 "name":"Desired Effective Date",
46 "required":false,
47 "readOnly":false,
48 "locations":[
49 {
50 "x":555.72,
51 "y":660.36,
52 "width":45.83999999999992,
53 "height":13.559999999999945
54 }
55 ],
56 "type":"PDFTextField",
57 "defaultValue":"",
58 "isPassword":false,
59 "isRichFormatted":false,
60 "isScrollable":true,
61 "isCombed":false,
62 "isMultiline":false,
63 "isFileSelector":false
64 },
65 {
66 "name":"Producer Email",
67 "required":false,
68 "readOnly":false,
69 "locations":[
70 {
71 "x":79.32,
72 "y":646.32,
73 "width":120.96000000000001,
74 "height":13.559999999999945
75 }
76 ],
77 "type":"PDFTextField",
78 "defaultValue":"titouan.launay@fileforge.com",
79 "isPassword":false,
80 "isRichFormatted":false,
81 "isScrollable":true,
82 "isCombed":false,
83 "isMultiline":false,
84 "isFileSelector":false
85 },
86 {
87 "name":"Insured Phone",
88 "required":false,
89 "readOnly":false,
90 "locations":[
91 {
92 "x":300.72,
93 "y":646.32,
94 "width":125.15999999999997,
95 "height":13.559999999999945
96 }
97 ],
98 "type":"PDFTextField",
99 "defaultValue":"+1 (415) 999-9999",
100 "isPassword":false,
101 "isRichFormatted":false,
102 "isScrollable":true,
103 "isCombed":false,
104 "isMultiline":false,
105 "isFileSelector":false
106 }
107...
108]