pdf_oxide 0.3.76

The fastest Rust PDF library — 0.8ms mean, 5× faster than the industry leaders, 100% pass rate on 3,830 real-world PDFs. Text extraction, Markdown/HTML conversion, PDF creation and editing.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
//! Text search implementation with regex support.
//!
//! Provides text search functionality that tracks positions, allowing
//! matches to be highlighted or processed with their bounding boxes.

use crate::document::PdfDocument;
use crate::error::{Error, Result};
use crate::geometry::Rect;
use crate::layout::TextSpan;
use regex::{Regex, RegexBuilder};

/// A search result with position information.
#[derive(Debug, Clone, serde::Serialize)]
pub struct SearchResult {
    /// Page number (0-indexed) where the match was found
    pub page: usize,
    /// The matched text
    pub text: String,
    /// Bounding box of the match on the page
    pub bbox: Rect,
    /// Start index in the extracted text
    pub start_index: usize,
    /// End index in the extracted text
    pub end_index: usize,
    /// Individual bounding boxes for each span that makes up the match
    /// (useful for matches spanning multiple lines)
    pub span_boxes: Vec<Rect>,
}

/// Options for text search.
#[derive(Debug, Clone, Default, serde::Deserialize)]
pub struct SearchOptions {
    /// Case insensitive search
    pub case_insensitive: bool,
    /// Treat pattern as literal text (not regex)
    pub literal: bool,
    /// Match whole words only
    pub whole_word: bool,
    /// Maximum number of results (0 = unlimited)
    pub max_results: usize,
    /// Page range to search (None = all pages)
    pub page_range: Option<(usize, usize)>,
}

impl SearchOptions {
    /// Create new default search options.
    pub fn new() -> Self {
        Self::default()
    }

    /// Enable case-insensitive search.
    pub fn case_insensitive() -> Self {
        Self {
            case_insensitive: true,
            ..Default::default()
        }
    }

    /// Set case sensitivity.
    pub fn with_case_insensitive(mut self, value: bool) -> Self {
        self.case_insensitive = value;
        self
    }

    /// Treat pattern as literal text (escape regex special characters).
    pub fn with_literal(mut self, value: bool) -> Self {
        self.literal = value;
        self
    }

    /// Match whole words only.
    pub fn with_whole_word(mut self, value: bool) -> Self {
        self.whole_word = value;
        self
    }

    /// Limit the number of results.
    pub fn with_max_results(mut self, max: usize) -> Self {
        self.max_results = max;
        self
    }

    /// Search only within a page range (inclusive).
    pub fn with_page_range(mut self, start: usize, end: usize) -> Self {
        self.page_range = Some((start, end));
        self
    }
}

/// Lightweight per-page search index: page text plus per-span bounding
/// boxes, no font names or per-character widths.
///
/// `compute_match_bbox` only ever needs whole-span boxes, so search never
/// touches the rest of a [`TextSpan`] — caching this projection instead of
/// the full spans is cheap enough to retain for every page across repeated
/// `search()` calls on the same document (see `PdfDocument::search_index`).
pub(crate) struct SearchPageIndex {
    full_text: String,
    span_positions: Vec<(usize, usize, usize)>,
    span_boxes: Vec<Rect>,
}

impl SearchPageIndex {
    pub(crate) fn from_spans(spans: &[TextSpan]) -> Self {
        let (full_text, span_positions) = TextSearcher::build_text_with_positions(spans);
        let span_boxes = spans.iter().map(|s| s.bbox).collect();
        Self {
            full_text,
            span_positions,
            span_boxes,
        }
    }
}

/// Text searcher for PDF documents.
pub struct TextSearcher;

impl TextSearcher {
    /// Search for text in a PDF document.
    ///
    /// # Arguments
    ///
    /// * `doc` - The PDF document to search
    /// * `pattern` - The regex pattern to search for
    /// * `options` - Search options
    ///
    /// # Returns
    ///
    /// Vector of search results with positions.
    pub fn search(
        doc: &PdfDocument,
        pattern: &str,
        options: &SearchOptions,
    ) -> Result<Vec<SearchResult>> {
        // Build the regex pattern
        let regex = Self::build_regex(pattern, options)?;

        // Determine page range
        let page_count = doc.page_count()?;
        let (start_page, end_page) = options
            .page_range
            .unwrap_or((0, page_count.saturating_sub(1)));

        let end_page = end_page.min(page_count.saturating_sub(1));

        let mut results = Vec::new();

        for page in start_page..=end_page {
            let page_results = Self::search_page(doc, page, &regex, options)?;
            results.extend(page_results);

            // Check result limit
            if options.max_results > 0 && results.len() >= options.max_results {
                results.truncate(options.max_results);
                break;
            }
        }

        Ok(results)
    }

    /// Search for text on a specific page.
    pub fn search_page(
        doc: &PdfDocument,
        page: usize,
        regex: &Regex,
        options: &SearchOptions,
    ) -> Result<Vec<SearchResult>> {
        // Reuse the page's cached search index (page text + span boxes) if a
        // prior search() built it, instead of re-extracting the page.
        let index = doc.search_page_index(page)?;

        let mut results = Vec::new();

        for mat in regex.find_iter(&index.full_text) {
            let start = mat.start();
            let end = mat.end();
            let matched_text = mat.as_str().to_string();

            // Find the spans that contain this match
            let (bbox, span_boxes) =
                Self::compute_match_bbox(start, end, &index.span_boxes, &index.span_positions);

            results.push(SearchResult {
                page,
                text: matched_text,
                bbox,
                start_index: start,
                end_index: end,
                span_boxes,
            });

            // Check result limit
            if options.max_results > 0 && results.len() >= options.max_results {
                break;
            }
        }

        Ok(results)
    }

    /// Build regex from pattern and options.
    fn build_regex(pattern: &str, options: &SearchOptions) -> Result<Regex> {
        let mut pattern_str = if options.literal {
            regex::escape(pattern)
        } else {
            pattern.to_string()
        };

        if options.whole_word {
            pattern_str = format!(r"\b{}\b", pattern_str);
        }

        RegexBuilder::new(&pattern_str)
            .case_insensitive(options.case_insensitive)
            .build()
            .map_err(|e| Error::InvalidPdf(format!("Invalid regex pattern: {}", e)))
    }

    /// Build concatenated text with position tracking.
    ///
    /// Returns the full text and a vector of (start_pos, end_pos, span_index)
    /// for each span.
    fn build_text_with_positions(spans: &[TextSpan]) -> (String, Vec<(usize, usize, usize)>) {
        let mut full_text = String::new();
        let mut positions = Vec::new();

        for (idx, span) in spans.iter().enumerate() {
            let start = full_text.len();
            full_text.push_str(&span.text);
            let end = full_text.len();
            positions.push((start, end, idx));

            // Add space between spans if needed
            if idx < spans.len() - 1 && !span.text.ends_with(' ') {
                full_text.push(' ');
            }
        }

        (full_text, positions)
    }

    /// Compute the bounding box for a match spanning potentially multiple spans.
    fn compute_match_bbox(
        match_start: usize,
        match_end: usize,
        span_boxes: &[Rect],
        span_positions: &[(usize, usize, usize)],
    ) -> (Rect, Vec<Rect>) {
        let mut matched_boxes = Vec::new();
        let mut combined_bbox: Option<Rect> = None;

        for &(span_start, span_end, span_idx) in span_positions {
            // Check if this span overlaps with the match
            if span_start < match_end && span_end > match_start {
                let bbox = span_boxes[span_idx];

                // For simplicity, use the whole span's bbox
                // A more sophisticated implementation would compute character-level boxes
                matched_boxes.push(bbox);

                if let Some(ref mut combined) = combined_bbox {
                    // Expand bbox to include this span
                    *combined = combined.union(&bbox);
                } else {
                    combined_bbox = Some(bbox);
                }
            }
        }

        (combined_bbox.unwrap_or_else(|| Rect::new(0.0, 0.0, 0.0, 0.0)), matched_boxes)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_search_options_default() {
        let opts = SearchOptions::default();
        assert!(!opts.case_insensitive);
        assert!(!opts.literal);
        assert!(!opts.whole_word);
        assert_eq!(opts.max_results, 0);
        assert!(opts.page_range.is_none());
    }

    #[test]
    fn test_search_options_builder() {
        let opts = SearchOptions::new()
            .with_case_insensitive(true)
            .with_literal(true)
            .with_whole_word(true)
            .with_max_results(10)
            .with_page_range(0, 5);

        assert!(opts.case_insensitive);
        assert!(opts.literal);
        assert!(opts.whole_word);
        assert_eq!(opts.max_results, 10);
        assert_eq!(opts.page_range, Some((0, 5)));
    }

    #[test]
    fn test_build_regex_simple() {
        let opts = SearchOptions::default();
        let regex = TextSearcher::build_regex("hello", &opts).unwrap();
        assert!(regex.is_match("hello world"));
        assert!(!regex.is_match("HELLO world"));
    }

    #[test]
    fn test_build_regex_case_insensitive() {
        let opts = SearchOptions::case_insensitive();
        let regex = TextSearcher::build_regex("hello", &opts).unwrap();
        assert!(regex.is_match("hello world"));
        assert!(regex.is_match("HELLO world"));
        assert!(regex.is_match("HeLLo world"));
    }

    #[test]
    fn test_build_regex_literal() {
        let opts = SearchOptions::new().with_literal(true);
        let regex = TextSearcher::build_regex("a.b", &opts).unwrap();
        assert!(regex.is_match("a.b"));
        assert!(!regex.is_match("axb")); // Without literal, . would match any char
    }

    #[test]
    fn test_build_regex_whole_word() {
        let opts = SearchOptions::new().with_whole_word(true);
        let regex = TextSearcher::build_regex("cat", &opts).unwrap();
        assert!(regex.is_match("the cat sat"));
        assert!(!regex.is_match("category"));
        assert!(!regex.is_match("concatenate"));
    }

    #[test]
    fn test_build_text_with_positions() {
        let spans = vec![
            TextSpan {
                provenance: None,
                text_rise: 0.0,
                artifact_type: None,
                text: "Hello".to_string(),
                bbox: Rect::new(0.0, 0.0, 50.0, 12.0),
                font_name: "Arial".to_string(),
                font_size: 12.0,
                font_weight: crate::layout::FontWeight::Normal,
                is_italic: false,
                is_monospace: false,
                color: crate::layout::Color {
                    r: 0.0,
                    g: 0.0,
                    b: 0.0,
                },
                mcid: None,
                mcid_scope: None,
                sequence: 0,
                split_boundary_before: false,
                offset_semantic: false,
                char_spacing: 0.0,
                word_spacing: 0.0,
                horizontal_scaling: 100.0,
                primary_detected: false,
                char_widths: vec![],
                char_x_offsets: Vec::new(),
                heading_level: None,
                rotation_degrees: 0.0,
                wmode: 0,
                rtl_draw_logical: false,
            },
            TextSpan {
                provenance: None,
                text_rise: 0.0,
                artifact_type: None,
                text: "World".to_string(),
                bbox: Rect::new(55.0, 0.0, 105.0, 12.0),
                font_name: "Arial".to_string(),
                font_size: 12.0,
                font_weight: crate::layout::FontWeight::Normal,
                is_italic: false,
                is_monospace: false,
                color: crate::layout::Color {
                    r: 0.0,
                    g: 0.0,
                    b: 0.0,
                },
                mcid: None,
                mcid_scope: None,
                sequence: 1,
                split_boundary_before: false,
                offset_semantic: false,
                char_spacing: 0.0,
                word_spacing: 0.0,
                horizontal_scaling: 100.0,
                primary_detected: false,
                char_widths: vec![],
                char_x_offsets: Vec::new(),
                heading_level: None,
                rotation_degrees: 0.0,
                wmode: 0,
                rtl_draw_logical: false,
            },
        ];

        let (text, positions) = TextSearcher::build_text_with_positions(&spans);

        assert_eq!(text, "Hello World");
        assert_eq!(positions.len(), 2);
        assert_eq!(positions[0], (0, 5, 0)); // "Hello" at 0-5
        assert_eq!(positions[1], (6, 11, 1)); // "World" at 6-11 (after space)
    }
}