Skip to main content

Crate pdf_textlayer

Crate pdf_textlayer 

Source
Expand description

Add an invisible, searchable text layer to a PDF.

Give the writer a PDF and the OCR boxes for its pages. Each box is drawn at its location in text render mode 3, so the page looks the same and becomes selectable and searchable.

  • Pages without drawable boxes are left as they are.
  • A box that lands on a word the page already contains is skipped, so a page that mixes native text and a scan is not indexed twice.
  • Boxes are given in the upright view of the page. Pages with a /Rotate entry and boxes with their own rotation are placed along the text direction.
use pdf_textlayer::{Options, ParsedPage, TextBox, write_text_layer};

let pages = vec![ParsedPage {
    page_number: 1,
    boxes: vec![TextBox::new("Hello world", 72.0, 100.0, 120.0, 12.0)],
}];

let stats = write_text_layer("scan.pdf", &pages, "searchable.pdf", &Options::default())?;
assert_eq!(stats.items_drawn, 1);

OCR JSON from a supported tool goes through an Adapter:

use pdf_textlayer::{Adapter, Options, make_searchable_pdf};

let stats = make_searchable_pdf("scan.pdf", "parsed.json", "searchable.pdf", Adapter::Liteparse, &Options::default())?;

Re-exports§

pub use adapters::Adapter;

Modules§

adapters
Turn an OCR tool’s JSON into ParsedPages.
cli
The pdf-textlayer command line.

Structs§

Options
Drawing options.
ParsedPage
The OCR boxes of one page.
Stats
What a pass wrote.
TextBox
One piece of OCR text to draw, in PDF points.

Enums§

Error
FontMetrics
Which width table sizes the text.

Functions§

add_text_layer
Draw a text layer onto a PDF held in memory and return the new PDF.
make_searchable_pdf
Read the OCR JSON at source with adapter, draw its boxes onto the PDF at input, and save the result to output.
write_text_layer
Draw a text layer onto the PDF at input and save it to output.