Pagecraft

search

Search a PDF

Find every place a word, a phrase or a pattern appears, and where on the page it is.

Reads
a PDF — application/pdf
Writes
one file — JSON
Tool page
/search-pdf

Example

Find every page a name appears on, and where on the page it is.

Given

  • report.pdf — a PDF of written text

From code

            import { loadDocument, runOperation } from '@pagecraft/core';

const report = await loadDocument({ bytes, name: 'report.pdf' });

const [output] = await runOperation('search', [report], {
  query: 'Priya Nair',
  caseSensitive: false,
});
          

From a shell

            pagecraft search report.pdf --query 'Priya Nair'
          

What comes back

One JSON file, containing “Priya Nair”.

This example is run by the test suite. If it stopped being true, the build would fail.

Options

query--queryRequired

string

Find

The words to look for.

regex--regex

boolean

Regular expression

Treat what was typed as a pattern rather than as the text to find.

Default: false

caseSensitive--case-sensitive

boolean

Match case

Default: false

wholeWord--whole-word

boolean

Whole words only

So that 'cat' does not find 'catalogue'.

Default: false

pages--pages

string, a page selection: '1-3, 7, 9-', 'even', 'last'

Pages

Which pages to search.

Default: 'all'

What it cannot do

  • A scanned page holds a picture of words, and nothing here can read it. Run OCR over it first.
  • A rectangle is worked out by counting characters across the run that holds them, which is how every reader highlights a result: it is right to within a letter, not to the glyph.
  • A phrase is found across a line break, because the break is not part of what was written. It is not found across a page break.