EmbedPDF

Text extraction

Each page handle exposes a text service that returns the page’s full plain-text extraction.

interface PageTextService {
  read(): AbortablePromise<PageTextSnapshot>;
}

Reading text#

const page = doc.page(pageObjectNumber);
const { text, charCount } = await page.text.read();
 
console.log(text);      // full page text in display order
console.log(charCount); // PDFium-reported character count

The result is a PageTextSnapshot:

interface PageTextSnapshot {
  text: string;
  charCount: number;
}
  • text is the page’s text in display order, decoded from UTF-16 to a JS string.
  • charCount is PDFium’s character count. It can differ from text.length when the page contains astral-plane characters: PDFium counts UTF-16 code units, and JS strings keep them as surrogate pairs.

The text snapshot is pure content — it’s addressed and cached by the page’s content version on the server, so repeat reads of an unchanged page are fast and CDN-friendly. It deliberately carries no annotation liveness; that lives on annotation reads.

Extracting a whole document#

There’s no document-level text call — iterate the page list and read each page:

const { pages } = await doc.pages.list();
 
const fullText = (
  await Promise.all(pages.map((p) => doc.page(p.pageObjectNumber).text.read()))
)
  .map((snapshot) => snapshot.text)
  .join('\n\n');

Reading text requires the doc.text.copy capability on the cloud. If the caller’s scope doesn’t grant it, read() rejects with a Forbidden EngineError. Check doc.security.effectiveScope before exposing a “copy text” action.

Was this page helpful?

Your feedback goes directly to the documentation team.