How to Flatten Annotations in PDF Files with JavaScript in NodeJS

Learn how to us JavaScript to flatten annotations in PDF files by calling Flatten Annotations API Tool by pdfRest.
Share this page

Why Use Flatten Annotations with JavaScript?

The pdfRest Flatten Annotations API Tool is designed to permanently merge annotations into the content layer of a PDF document. This can be particularly useful when you want to ensure that comments, stamps, or filled form fields remain an integral part of the PDF content, for example, when finalizing a contract or an agreement that has been reviewed and annotated by multiple parties. By flattening annotations, you make them uneditable, which can be crucial for archiving and sharing official documents.

This tutorial will show you how to send an API call to Flatten Annotations using JavaScript.

Flatten Annotations with JavaScript Code Example

/**
 * This request demonstrates how to flatten annotations in a PDF.
 */
var axios = require("axios");
var FormData = require("form-data");
var fs = require("fs");

// Create a new form data instance and append the PDF file and parameters to it
var data = new FormData();
data.append("file", fs.createReadStream("/path/to/file"));
data.append("output", "pdfrest_flattened_annotations_pdf");

// define configuration options for axios request
var config = {
  method: "post",
  maxBodyLength: Infinity, // set maximum length of the request body
  url: "https://api.pdfrest.com/flattened-annotations-pdf",
  headers: {
    "Api-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", // Replace with your API key
    ...data.getHeaders(), // set headers for the request
  },
  data: data, // set the data to be sent with the request
};

// send request and handle response or error
axios(config)
  .then(function (response) {
    console.log(JSON.stringify(response.data));
  })
  .catch(function (error) {
    console.log(error);
  });

// If you would like to download the file instead of getting the JSON response, please see the 'get-resource-id-endpoint.js' sample.

The source of the provided code can be found at pdf-rest-api-samples on GitHub.

Breaking Down the Code

The code snippet above demonstrates how to use the pdfRest Flatten Annotations API with JavaScript:

var axios = require("axios");
var FormData = require("form-data");
var fs = require("fs");

This part of the code includes the necessary modules: axios for making HTTP requests, FormData for creating multipart/form-data payloads, and fs for file system operations.

var data = new FormData();
data.append("file", fs.createReadStream("/path/to/file"));
data.append("output", "pdfrest_flattened_annotations_pdf");

Here, a new FormData instance is created, and the PDF file to be flattened is appended to it along with an output parameter that specifies the name of the output file.

var config = {
  method: "post",
  maxBodyLength: Infinity,
  url: "https://api.pdfrest.com/flattened-annotations-pdf",
  headers: {
    "Api-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    ...data.getHeaders(),
  },
  data: data,
};

The config object defines the configuration for the axios request, including the API endpoint URL, the API key for authentication, and the data payload.

axios(config)
  .then(function (response) {
    console.log(JSON.stringify(response.data));
  })
  .catch(function (error) {
    console.log(error);
  });

Finally, the axios request is sent, and the response is handled in the promise's then method, or an error is caught in the catch method.

Beyond the Tutorial

By following the steps above, you've learned how to use JavaScript to call the pdfRest Flatten Annotations API. This allows you to integrate PDF annotation flattening into your JavaScript applications seamlessly. To further explore and demo all of the pdfRest API Tools, you can visit the API Lab. For comprehensive documentation on all available endpoints and parameters, refer to the API Reference documentation.

Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at pdf-rest-api-samples on GitHub.

Generate a self-service API Key now!

Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.

Compare Plans
Contact Us