Skip to main content

TextExtractor

Struct TextExtractor 

Source
pub struct TextExtractor<'doc> { /* private fields */ }
Expand description

Text extractor that processes content streams.

This structure maintains the graphics state stack and font information while processing operators to extract positioned text.

The extractor can work in two modes:

  • Span mode (default): Extracts complete text strings as PDF provides them (PDF spec compliant)
  • Character mode: Extracts individual characters (for special use cases)

Implementations§

Source§

impl<'doc> TextExtractor<'doc>

Source

pub fn new() -> Self

Create a new text extractor with default configuration.

§Examples
use pdf_oxide::extractors::TextExtractor;

let extractor = TextExtractor::new();
Source

pub fn with_config(config: TextExtractionConfig) -> Self

Create a new text extractor with custom configuration.

§Arguments
  • config - Configuration for text extraction heuristics
§Examples
use pdf_oxide::extractors::{TextExtractor, TextExtractionConfig};

// Use custom space threshold
let config = TextExtractionConfig::with_space_threshold(-80.0);
let extractor = TextExtractor::with_config(config);
Source

pub fn set_page_index(&mut self, page_index: u32)

Stamp this extractor with the page index it is processing.

Used so spans (and the lookup keys for /ActualText) carry the correct McidScope::Page(page_index) when the extractor is not currently inside a Form XObject.

Source

pub fn with_merging_config(self, merging_config: SpanMergingConfig) -> Self

Create a new text extractor with custom merging configuration.

This allows fine-tuning how adjacent spans are merged and when spaces are inserted, useful for documents with unusual spacing patterns.

§Arguments
  • merging_config - Configuration for span merging thresholds
§Examples
use pdf_oxide::extractors::{TextExtractor, SpanMergingConfig};

// Use aggressive space insertion for dense layouts
let config = SpanMergingConfig::aggressive();
let extractor = TextExtractor::new().with_merging_config(config);
Source

pub fn set_resources(&mut self, resources: Object)

Set the resources dictionary for this extractor.

This allows the extractor to access XObjects and fonts during extraction.

Source

pub fn set_document(&mut self, document: &'doc PdfDocument)

Set the document reference for loading XObjects.

Source

pub fn take_mc_actualtext_mcids(&mut self) -> HashSet<u32>

Take ownership of the set of MCIDs whose marked-content sequence carried an inline /ActualText property on this extraction.

The set is observed by the BDC handler; this method drains it out so the document layer can stash it on a per-page side channel for the struct-tree-scope ActualText applier.

Source

pub fn set_excluded_layers(&mut self, layers: HashSet<String>)

Set layer names (Optional Content Groups) to exclude from extraction.

Content within BDC/EMC scopes tagged “OC” whose OCG /Name matches one of the provided names will be suppressed during text extraction.

Source

pub fn set_excluded_inks(&mut self, inks: HashSet<String>)

Set ink / separation names to exclude from extraction.

When the fill color space is a Separation or DeviceN whose ink name(s) intersect with any of the provided names, subsequent text is suppressed until the color space changes to a non-excluded one.

DeviceN behavior: For DeviceN color spaces (e.g. [/DeviceN [/Cyan /SpotGold] ...]), text is suppressed if ANY ink in the array matches — even process colors sharing the DeviceN definition. This is because tint values are not evaluated during extraction.

Source

pub fn set_document_ptr(&mut self, doc: &'doc PdfDocument)

Convenience wrapper: identical to set_document.

Source

pub fn prepare_for_span_extraction(&mut self)

Prepare for span extraction mode (same setup as extract_text_spans preamble).

Source

pub fn execute_operator_public(&mut self, op: Operator) -> Result<()>

Public wrapper for execute_operator (normally private).

Source

pub fn flush_public(&mut self) -> Result<()>

Public wrapper for flush_tj_span_buffer (normally private).

Source

pub fn add_font(&mut self, name: String, font: FontInfo)

Add a font to the extractor.

Fonts must be added before processing content streams that reference them.

§Arguments
  • name - The font resource name (e.g., “F1”, “TT1”)
  • font - The font information
§Examples
let mut extractor = TextExtractor::new();
extractor.add_font("F1".to_string(), font);
Source

pub fn get_font_set(&self) -> Vec<(String, Arc<FontInfo>)>

Return the current font set for caching purposes.

Source

pub fn share_truetype_cmaps(&mut self)

Share TrueType cmap tables between fonts with matching base font names. When a CIDFontType2 Identity-H font has no truetype_cmap, borrow from another font on the same page with the same base font name (ignoring subset prefix).

Source

pub fn extract_text_spans( &mut self, content_stream: &[u8], ) -> Result<Vec<TextSpan>>

Extract text from a content stream.

Parses the content stream and executes operators to extract positioned characters with Unicode mappings and font information.

§Arguments
  • content_stream - The raw content stream data (should be decoded first)
§Returns

A vector of TextChar structures containing positioned characters.

§Errors

Returns an error if the content stream cannot be parsed.

§Examples
let mut extractor = TextExtractor::new();
let chars = extractor.extract(content_data)?;
println!("Extracted {} characters", chars.len());

Extract text as complete spans (PDF spec compliant).

This is the recommended method for text extraction. It extracts complete text strings as the PDF provides them via Tj/TJ operators, following the PDF specification ISO 32000-1:2008.

§Benefits
  • Avoids overlapping character issues
  • Preserves PDF’s text positioning intent
  • More robust for complex layouts
  • Matches industry best practices
§Arguments
  • content_stream - The PDF content stream data
§Returns

Vector of TextSpan objects in reading order

Source

pub fn extract(&mut self, content_stream: &[u8]) -> Result<Vec<TextChar>>

Extract individual characters from a PDF content stream.

This is a low-level method that extracts characters one by one. For most use cases, prefer using extract_text_spans() which groups characters into text spans according to PDF semantics.

Source

pub fn extract_owned(&mut self, content_stream: &[u8]) -> Result<Vec<TextChar>>

Same extraction as Self::extract, but hands the buffer over instead of copying it. Every TextChar owns a font_name String, so extract’s clone re-allocates once per glyph — measurable on long documents. Leaves self.chars empty, so callers that read char_count/chars afterwards must keep using Self::extract.

Source

pub fn char_count(&self) -> usize

Get the number of extracted characters.

Source

pub fn clear(&mut self)

Clear all extracted characters.

Trait Implementations§

Source§

impl<'doc> Debug for TextExtractor<'doc>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'doc> Default for TextExtractor<'doc>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<'doc> !Freeze for TextExtractor<'doc>

§

impl<'doc> RefUnwindSafe for TextExtractor<'doc>

§

impl<'doc> Send for TextExtractor<'doc>

§

impl<'doc> Sync for TextExtractor<'doc>

§

impl<'doc> Unpin for TextExtractor<'doc>

§

impl<'doc> UnsafeUnpin for TextExtractor<'doc>

§

impl<'doc> UnwindSafe for TextExtractor<'doc>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<R, P> ReadPrimitive<R> for P
where R: Read + ReadEndian<P>, P: Default,

Source§

fn read_from_little_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_little_endian().
Source§

fn read_from_big_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_big_endian().
Source§

fn read_from_native_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_native_endian().
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<U, T> ToOwnedObj<U> for T
where U: FromObjRef<T>,

Source§

fn to_owned_obj(&self, data: FontData<'_>) -> U

Convert this type into T, using the provided data to resolve any offsets.
Source§

impl<U, T> ToOwnedTable<U> for T
where U: FromTableRef<T>,

Source§

fn to_owned_table(&self) -> U

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Ungil for T
where T: Send,