How to add text and images to PDF in JavaScript and PDF.co Web API

This tutorial will show how to add text and images to PDF in JavaScript

These sample source codes on this page below are demonstrating how to add text and images to PDF in JavaScript. PDF.co Web API can add text and images to PDF. It can be used from JavaScript. PDF.co Web API is the flexible Web API that includes full set of functions from e-signature requests to data extraction, OCR, images recognition, pdf splitting and pdf splitting. Can also generate barcodes and read barcodes from images, scans and pdf.

This code snippet below for PDF.co Web API works best when you need to quickly add text and images to PDF in your JavaScript application. Just copy and paste the code into your JavaScript application’s code and follow the instruction. Use of PDF.co Web API in JavaScript is also explained in the documentation included along with the product.

Our website provides trial version of PDF.co Web API for free. It also includes documentation and source code samples.

Try PDF.co Web API today:  60 Day Free Trial (on-premise) or  Web API (on-demand version)

AddTextToExistingPDF.js
      
var https = require("https"); var path = require("path"); var fs = require("fs"); // The authentication key (API Key). // Get your own by registering at https://app.pdf.co/documentation/api const API_KEY = "***********************************"; // Direct URL of source PDF file. const SourceFileUrl = "https://bytescout-com.s3.amazonaws.com/files/demo-files/cloud-api/pdf-edit/sample.pdf"; // Comma-separated list of page indices (or ranges) to process. Leave empty for all pages. Example: '0,2-5,7-'. const Pages = ""; // PDF document password. Leave empty for unprotected documents. const Password = ""; // Destination PDF file name const DestinationFile = "./result.pdf"; // Text annotation params const Type = "annotation"; const X = 400; const Y = 600; const Text = "APPROVED"; const FontName = "Times New Roman"; const FontSize = 24; const Color = "FF0000"; // * Add Text * // Prepare request to `PDF Edit` API endpoint var queryPath = `/v1/pdf/edit/add?name=${path.basename(DestinationFile)}&password=${Password}&pages=${Pages}&url=${SourceFileUrl}&type=${Type}&x=${X}&y=${Y}&text=${Text}&fontname=${FontName}&size=${FontSize}&color=${Color}`; var reqOptions = { host: "api.pdf.co", path: encodeURI(queryPath), headers: { "x-api-key": API_KEY } }; // Send request https.get(reqOptions, (response) => { response.on("data", (d) => { // Parse JSON response var data = JSON.parse(d); if (data.error == false) { // Download the output file var file = fs.createWriteStream(DestinationFile); https.get(data.url, (response2) => { response2.pipe(file).on("close", () => { console.log(`Generated PDF file saved to '${DestinationFile}' file.`); }); }); } else { // Service reported error console.log(data.message); } }); }).on("error", (e) => { // Request error console.error(e); });

Try PDF.co Web API today:  60 Day Free Trial (on-premise) or  Web API (on-demand version)

VIDEO

ON-PREMISE VERSION INFORMATION

Get 60 Day Free Trial or Visit PDF.co Web API Home Page

Explore PDF.co Web API Documentation

Get PDF.co Web API Free Training

WEB API

Get Your Free API Key

Explore Web API Documentation

Related Samples: