How to PDF text search API in JavaScript and PDF.co Web API

The tutorial below will demonstrate how to PDF text search API in JavaScript

This sample source code below will demonstrate you how to PDF text search API in JavaScript. Want to PDF text search API in your JavaScript app? PDF.co Web API is designed for it. 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 PDF text search API in your JavaScript application. In your JavaScript project or application you may simply copy & paste the code and then run your app! You can use these JavaScript sample examples in one or many applications.

PDF.co Web API free trial version is available on our website. JavaScript and other programming languages are supported.

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

PdfTextSearchFromUrlAsyncApi.js
      
var https = require("https"); // Use "npm install request" command to install. var request = require("request"); // 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-to-text/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 = ""; // Search string. const SearchString = '[4-9][0-9].[0-9][0-9]'; // Regular expression to find numbers in format dd.dd and between 40.00 to 99.99 // Enable regular expressions (Regex) const RegexSearch = true; // Prepare URL for PDF text search API call. // See documentation: https://app.pdf.co/documentation/api/1.0/pdf/find.html var queryPath = `/v1/pdf/find?password=${Password}&pages=${Pages}&url=${SourceFileUrl}&searchString=${SearchString}&regexSearch=${RegexSearch}&async=True`; 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) { console.log(`Job #${data.jobId} has been created!`); checkIfJobIsCompleted(data.jobId, data.url); } else { // Service reported error console.log(data.message); } }); }).on("error", (e) => { // Request error console.log(e); }); function checkIfJobIsCompleted(jobId, resultFileUrl) { let queryPath = `/v1/job/check?jobid=${jobId}`; let reqOptions = { host: "api.pdf.co", path: encodeURI(queryPath), method: "GET", headers: { "x-api-key": API_KEY } }; https.get(reqOptions, (response) => { response.on("data", (d) => { response.setEncoding("utf8"); // Parse JSON response let data = JSON.parse(d); console.log(`Checking Job #${jobId}, Status: ${data.status}, Time: ${new Date().toLocaleString()}`); if (data.status == "working") { // Check again after 3 seconds setTimeout(function () { checkIfJobIsCompleted(jobId, resultFileUrl); }, 3000); } else if (data.status == "success") { request({ method: 'GET', uri: resultFileUrl, gzip: true }, function (error, response, body) { // Parse JSON response let respJsonFileArray = JSON.parse(body); respJsonFileArray.forEach((element) => { console.log("Found text " + element["text"] + " at coordinates " + element["left"] + ", " + element["top"]); }, this); }); } else { console.log(`Operation ended with status: "${data.status}".`); } }) }); }

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: