Concept · Reading PDFs
Extract Text from PDF
Programmatically, Explained
Last updated: 2026-06-29
PDF text extraction walks the page's content stream, finds every glyph the author placed, maps each glyph code back to a Unicode character using the font's built-in mapping table, and returns the result as a plain string. One call. No OCR, no guesswork. This guide explains how it works, when it works, and when it does not.
The simplest analogy: a typed letter vs a photograph
Imagine two documents on your desk. The first is a letter someone typed on a computer and printed. You could copy the words off it instantly because you know they are real letters arranged in a known order. The second is a photograph of a different letter. To get the words out you would have to read them by eye, or scan and run them through character recognition software.
A PDF with a text layer is the typed letter: the characters are there, encoded in the file. A scanned PDF is the photograph: the page is pixels and there are no characters to read, only shapes that look like characters. rust-pdf extracts text from the first kind. For the second kind you would need OCR, which is a different tool entirely.
PDF with a text layer
Extraction is instant and exact.
The author's software encoded every character into the file when the PDF was created. rust-pdf reads that encoding and returns a Unicode string. Works for any language, any font, including CJK.
Scanned PDF (image only)
No text layer, nothing to extract.
The page is a raster image. There are no encoded characters, only pixels. rust-pdf will return an empty string. You would need a separate OCR tool to recover the text from the image.
How it works: glyphs back to Unicode
Every PDF page contains a content stream: a sequence of drawing instructions. Text instructions say which font to use, where to place the cursor, and which glyph codes to paint. Extraction reverses that process in three steps.
Walk the content stream
The extractor scans each page's content stream looking for text operators: Tj, TJ, Tf, Tm, T* and their relatives. Each operator is processed in order, the same order the PDF renderer would use.
Map glyph codes to Unicode
Each font carries a ToUnicode CMap that translates the glyph codes written in the stream into Unicode code points. For Type0 composite fonts (the standard for CJK and modern Unicode PDFs) glyph codes are two bytes wide, giving full Unicode coverage.
Infer spaces and line breaks
The extractor infers spaces from large negative TJ kerning adjustments that represent a gap wider than a normal inter-glyph space, and line breaks from Tm matrix changes and T* operators that advance the cursor vertically to a new line.
The output is a plain Unicode string. No rendering, no image decoding, no network calls. Extraction speed is proportional to the number of characters in the document, not to the page count or visual complexity.
Real text vs OCR: what each one can and cannot do
Text extraction and OCR solve different problems. Knowing which one you need saves time and avoids surprises.
Text layer present
rust-pdf handles this- Exact Unicode output, no character errors
- Works with any language including CJK
- Handles subset fonts and Identity-H encoding
- Fast: milliseconds per megabyte
- No external dependencies or API calls
Scanned image, no text layer
Needs OCR -- out of scopeThe page is pixels. There are no characters in the PDF structure for rust-pdf to read. OCR must analyse the image, recognise letter shapes, and guess at the text. rust-pdf does not do this. You would need a separate OCR library or service.
What people use PDF text extraction for
Once the text is out of the file, the possibilities are the same as for any other plain text.
Full-text search and indexing
Feed extracted text into Elasticsearch, PostgreSQL full-text search, or any search engine to make your document library instantly searchable without storing a separate copy of each file.
RAG and LLM pipelines
Chunk the extracted text, embed it, and store it in a vector database. Your retrieval-augmented generation pipeline can then answer questions grounded in your PDF corpus without sending the binary file to the model.
Data pipelines
Extract structured information such as invoice totals, contract dates, or table values from a batch of PDFs, parse the resulting text, and load it into a database or data warehouse automatically.
Content migration
Move legacy document archives into a CMS, knowledge base, or modern format. Bulk-extract text from thousands of PDFs as the first step in a migration, then clean and restructure as needed.
Compliance and redaction discovery
Scan document text for sensitive patterns such as PII, account numbers, or regulated terms before sharing files externally. Knowing what the text says lets you decide what to redact and where.
Classification and summarisation
Route incoming documents to the right team, flag contracts that need legal review, or generate a one-paragraph summary of a long report by running the extracted text through a classifier or language model.
Extract text in one call
One call returns the document's text as a Unicode string.
# pip install rustpdf
import rustpdf
text = rustpdf.extract_text(open("document.pdf", "rb").read())
print(text)
// dotnet add package RustPdf
using RustPdf;
string text = Pdf.ExtractText(File.ReadAllBytes("document.pdf"));
Console.WriteLine(text);
// go get github.com/rustpdf/rustpdf-go@latest
data, _ := os.ReadFile("document.pdf")
text, _ := rustpdf.ExtractText(data)
fmt.Println(text)
// npm install rustpdf
const { extractText } = require("rustpdf");
const fs = require("fs");
const text = extractText(fs.readFileSync("document.pdf"));
console.log(text);
The same function is available in all eight language bindings: Python, C#/.NET, Go, Node.js, PHP, Ruby, Delphi and Swift. Full reference in the documentation.
PDF text extraction FAQ
How do I extract text from a PDF?
Pass the PDF bytes to rustpdf.extract_text() in Python, Pdf.ExtractText() in C#, rustpdf.ExtractText() in Go, or extractText() in Node. The library walks every page's content stream, maps each shown glyph code back to Unicode using the font's ToUnicode CMap, infers spaces from large negative TJ adjustments, and infers line breaks from Tm and T* vertical position changes. The result is a plain Unicode string.
Does it work with CJK / Unicode?
Yes. rust-pdf handles Type0 composite fonts with Identity-H encoding, which is the standard encoding for CJK and any Unicode-rich PDF produced by modern tools. Glyph codes in those fonts are two bytes wide, and the ToUnicode CMap maps each pair to one or more Unicode code points. Subset fonts are also handled correctly: the library reads the embedded subset program's CMap, not any external font data.
Is this OCR?
No. OCR (optical character recognition) analyses a raster image to guess what letters are shown. PDF text extraction reads the actual text that the PDF author encoded in the file. It is exact, instant, and language-agnostic. rust-pdf does not perform OCR and does not attempt to interpret images.
What about scanned PDFs?
A scanned PDF is a photograph of a page wrapped in a PDF container. If no text layer was added (for example by a scanner's built-in OCR), there is nothing for a text extractor to read: the content is pixels, not characters. rust-pdf will return an empty or near-empty string for such files. To get text out of a scan you would need a separate OCR step, which rust-pdf does not provide.
Can I use it for search or LLM/RAG pipelines?
Yes. Extracting text with rust-pdf and feeding the result into a full-text search index or an LLM context window is one of the primary use cases. The extraction is fast enough to process large document collections in bulk. The output is clean Unicode suitable for tokenisation, chunking, and embedding without further normalisation.
Extract PDF text in your language
One Rust core, eight language bindings, one function call. Prototype for free, license the corporate features when you ship.