Skip to main content

Module text_edit

Module text_edit 

Source
Expand description

Async I/O wrappers via Tokio’s blocking pool. Layout-aware text replacement engine (find & replace by match id). Layout-aware text editing: find → stage → commit (Phase 1B engine).

This is the match-based replacement engine designed in docs/TEXT_REPLACE_ENGINE_DESIGN.md. Unlike crate::text_replace it never skips silently, preserves the page’s /Contents stream structure, detects text in Form XObjects, refuses signed documents by default and reports every staged edit.

use lopdf::Document;
use pdf_manip::text_edit::{begin_text_edit, DocumentRevision, ReplaceOptions, TextQuery};

let bytes = std::fs::read("in.pdf").unwrap();
let mut doc = Document::load_mem(&bytes).unwrap();
let revision = DocumentRevision::from_source_bytes(&bytes);

let mut session = begin_text_edit(&mut doc, revision).unwrap();
let matches = session.find_text(TextQuery::exact("Acme B.V.")).unwrap();
session
    .stage_replace(&matches[0].id, "Example B.V.", ReplaceOptions::default())
    .unwrap();
let report = session.commit().unwrap();
assert_eq!(report.replacements_applied, 1);

Structs§

CommitError
A failed commit: the first fatal cause plus the validation outcome of every staged edit. The document was not modified.
ContainerInfo
Container identity of a match or a modified stream.
Diagnostic
A coded, human-readable observation attached to matches and results.
DocumentRevision
Identifies one exact revision of a document during text editing.
MatchId
Opaque, serializable locator for one text match.
MatchSpan
One text-showing operator touched by a match.
MatchStyle
Visual style at the start of a match.
ReplaceOptions
Options for one staged replacement.
SignatureSummary
A digital signature found in the document.
TextEditSession
An in-progress text-edit transaction (design §2).
TextMatch
One found occurrence.
TextQuery
A text search query. Matching operates on the decoded visual text of the logical reading sequence per container (design §10 / Phase 1B narrowing).
TextReplacementReport
Exhaustive commit report: matches_found == replacements_applied + replacements_failed always holds (no silent skips).
TextReplacementResult
Per-edit result (design §4.4).

Enums§

CommitPolicy
Transaction policy (design §10.4). Strictest-wins across the staged edits: the commit runs BestEffort only when every staged edit requested it.
ContainerKind
Where a match lives.
FitPolicy
What the engine may do when replacement text does not fit (design §4.3). Phase 1B implements only FitPolicy::Exact.
FontFallback
Font fallback policy. Fallback use is never silent: it is always visible in the per-edit result (font_substituted).
RegionRelation
How a region rectangle selects matches (design §10.3).
ReplacementStatus
Outcome of one staged edit.
SignaturePolicy
Digital-signature policy (design §6).
StaleReason
Why a MatchId no longer resolves (design §3). No fuzzy relocation is ever attempted.
TaggedTextPolicy
Tagged-text policy (design §7). Phase 1: reject /ActualText conflicts.
TextEditError
Typed errors of the text-edit engine.
UnsupportedContainer
Container kinds the Phase 1B engine detects but cannot edit.
UnsupportedReason
Why a found match cannot be edited in this phase.
WritingDirection
Writing direction of matched text. Phase 1 supports LTR only.

Functions§

begin_text_edit
Start a text-edit session on a document.
replace_text
Convenience: find + stage + commit in one call (design §4).