Skip to content

PDF Generation Template Editor

Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.

View on GitHub

Text Search and Selection ​

ComPDF for Web offers developers an API for programmatic full-text search, as well as UI for searching and highlighting relevant matches. You can use the method below to search for content in PDFs with ComPDF for Web.

javascript
instance.UI.searchTextFull(value);
NameRequiredTypeDescription
valueyesstringThe Searched Content

If you only need to get the position of the search term, you can obtain it through the following API.

javascript
const results = await documentViewer.search(value, undefined, { startPage: pageNumber, endPage: pageNumber }).getAll();

The API will return an array of search results, each containing the page number, the position of the search term (quads), the matched text (resultStr) and its surrounding text (ambientStr).

json
[{
  "resultCode": 2,
  "pageNumber": 1,
  "pageNum": 1,
  "resultStr": "PDF",
  "ambientStr": " a sample PDF document that showc",
  "resultStrStart": 10,
  "resultStrEnd": 13,
  "quads": [{
    "x1": 155, "y1": 226,
    "x2": 185, "y2": 226,
    "x3": 185, "y3": 215,
    "x4": 155, "y4": 215
  }]
}]