How to add text and images to PDF in JavaScript with Web API

The tutorial shows how to add text and images to PDF in JavaScript

On this page you will learn from code samples for programming in JavaScript.Writing of the code to add text and images to PDF in JavaScript can be done by developers of any level using Web API. Web API is the Web API with a set of tools for documents manipulation, data conversion, data extraction, splitting and merging of documents. Includes image recognition, built-in OCR, barcode generation and barcode decoders to decode bar codes from scans, pictures and pdf. It can be used to add text and images to PDF using JavaScript.

Fast application programming interfaces of Web API for JavaScript plus the instruction and the code below will help you quickly learn how to add text and images to PDF. Just copy and paste the code into your JavaScript application’s code and follow the instruction. Implementing JavaScript application typically includes multiple stages of the software development so even if the functionality works please test it with your data and the production environment.

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

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

var https = require("https"); var path = require("path"); var fs = require("fs"); // `request` module is required for file upload. // Use "npm install request" command to install. var request = require("request"); // The authentication key (API Key). // Get your own by registering at //const API_KEY = "***********************************"; const API_KEY = "hirenpatel2236@gmail.com_f5859c8ae9a7bca8"; // Direct URL of source PDF file. const SourceFileUrl = ""; // Search string. const SearchString = 'Notes'; // Prepare URL for PDF text search API call. // See documentation: var queryFindText = ``; let reqOptionsFindText = { uri: queryFindText, headers: { "x-api-key": API_KEY }, formData: { url: SourceFileUrl, searchString: SearchString } }; // Send request request.get(reqOptionsFindText, function (error, responseFindText, bodyFindText) { if (error) { return console.error("Error: ", error); } // Parse JSON response let dataFindText = JSON.parse(bodyFindText); if (dataFindText.body.length > 0) { var element = dataFindText.body[0]; console.log("Found text " + element["text"] + " at coordinates " + element["left"] + ", " + element["top"]); // 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 = +element["left"]; const Y = +element["top"] + 25; const Text = "Some notes will go here... Some notes will go here.... Some notes will go here....."; const FontName = "Times New Roman"; const FontSize = 12; 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: "", 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 PDF 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); }); } else { console.error("No result found."); } });

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



Get 60 Day Free Trial or Visit Web API Home Page

Explore Web API Documentation

Get Web API Free Training


Get Your Free API Key

Explore Web API Documentation

Related Samples: