How to Set and Fill PDF Form Field Values using Node.js

Cloudmersive
2 min readSep 27, 2023

--

If a PDF already has form fields laid out, we can retrieve AND set the contents of those fields through our web application with two quick API calls.

First, we need to make an API call to get the form fields/values from the PDF document, and then we need to make an API call that allows us to fill those fields with custom text input.

Before we use code examples to structure each of those individual requests, we first need to install the Node.js SDK, which we can do by either running this command:

npm install cloudmersive-convert-api-client --save

Or by adding this snippet to our package.json:

  "dependencies": {
"cloudmersive-convert-api-client": "^2.6.3"
}

With installation out of the way, we can first retrieve a PDF document’s available fields/values using the following code examples:

var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');
var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;

// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';



var apiInstance = new CloudmersiveConvertApiClient.EditPdfApi();

var inputFile = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | Input file to perform the operation on.


var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.editPdfGetFormFields(inputFile, callback);

And we can then use the below code examples to request to fill those fields with customized information:

var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');
var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;

// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';



var apiInstance = new CloudmersiveConvertApiClient.EditPdfApi();

var fieldValues = new CloudmersiveConvertApiClient.SetPdfFormFieldsRequest(); // SetPdfFormFieldsRequest |


var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.editPdfSetFormFields(fieldValues, callback);

Our request object should look a bit like our response object, but containing the file bytes string this time:

{
"FieldValues": [
{
"FieldName": "string",
"TextValue": "string",
"CheckboxValue": true,
"ComboBoxSelectedIndex": 0
}
],
"InputFileBytes": "string"
}

We can authorize both of our API requests with the same free API key. We can get one by registering a free account on the Cloudmersive website; this will allow us to make up to 800 API calls per month (each API consumes 1 call) with no commitments.

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.