1use log::warn;
2
3use crate::{Dictionary, Object, ObjectId, Stream, parser};
4use crate::{
5 Error, Result,
6 content::{Content, Operation},
7 encodings::Encoding,
8 lopdf_document::Document,
9 lopdf_error::ParseError,
10 object::Object::Name,
11 xref::{Xref, XrefEntry, XrefType},
12};
13use std::{
14 collections::BTreeMap,
15 io::{Cursor, Read},
16};
17
18impl Content<Vec<Operation>> {
19 pub fn decode(data: &[u8]) -> Result<Self> {
21 parser::content(data).ok_or(ParseError::InvalidContentStream.into())
22 }
23
24 pub fn decode_strict(data: &[u8]) -> Result<Self> {
26 parser::content_strict(data).map_err(|e| e.into())
27 }
28}
29
30impl Stream {
31 pub fn decode_content(&self) -> Result<Content<Vec<Operation>>> {
33 Content::decode(&self.content)
34 }
35}
36
37impl Document {
38 pub fn get_and_decode_page_content(
40 &self,
41 page_id: ObjectId,
42 ) -> Result<Content<Vec<Operation>>> {
43 let content_data = self.get_page_content(page_id)?;
44 Content::decode(&content_data)
45 }
46
47 pub fn add_to_page_content(
49 &mut self,
50 page_id: ObjectId,
51 content: Content<Vec<Operation>>,
52 ) -> Result<()> {
53 let content_data = Content::encode(&content)?;
54 self.add_page_contents(page_id, content_data)?;
55 Ok(())
56 }
57
58 pub fn extract_text(&self, page_numbers: &[u32]) -> Result<String> {
59 let text_fragments = self.extract_text_chunks(page_numbers);
60 let mut text = String::new();
61 for maybe_text_fragment in text_fragments.into_iter() {
62 let text_fragment = maybe_text_fragment?;
63 text.push_str(&text_fragment);
64 }
65
66 Ok(text)
67 }
68
69 pub fn extract_text_chunks(&self, page_numbers: &[u32]) -> Vec<Result<String>> {
70 let pages: BTreeMap<u32, (u32, u16)> = self.get_pages();
71 page_numbers
72 .iter()
73 .flat_map(|page_number| {
74 let result = self.extract_text_chunks_from_page(&pages, *page_number);
75 match result {
76 Ok(text_chunks) => text_chunks,
77 Err(err) => vec![Err(err)],
78 }
79 })
80 .collect()
81 }
82
83 fn extract_text_chunks_from_page(
84 &self,
85 pages: &BTreeMap<u32, (u32, u16)>,
86 page_number: u32,
87 ) -> Result<Vec<Result<String>>> {
88 let mut collected_chunks_and_errs: Vec<std::result::Result<String, Error>> = Vec::new();
89
90 let page_id = *pages
91 .get(&page_number)
92 .ok_or(Error::PageNumberNotFound(page_number))?;
93 let fonts = self.get_page_fonts(page_id)?;
94 let encodings: BTreeMap<Vec<u8>, Encoding> = fonts
95 .into_iter()
96 .filter_map(|(name, font)| match font.get_font_encoding(self) {
97 Ok(it) => Some((name, it)),
98 Err(err) => {
99 collected_chunks_and_errs.push(Err(err));
100 None
101 }
102 })
103 .collect();
104 let content_data = self.get_page_content(page_id)?;
105 let content = Content::decode(&content_data)?;
106
107 let mut current_encoding = None;
109 let mut current_text = String::new();
110 for operation in &content.operations {
111 match operation.operator.as_ref() {
112 "Tf" => {
113 let current_font = operation
114 .operands
115 .first()
116 .ok_or_else(|| Error::Syntax("missing font operand".to_string()))?
117 .as_name();
118 current_encoding = match current_font {
119 Ok(font) => encodings.get(font),
120 Err(err) => {
121 collected_chunks_and_errs.push(Err(err));
122 None
123 }
124 };
125
126 if !current_text.is_empty() {
127 collected_chunks_and_errs.push(Ok(current_text));
128 current_text = String::new();
129 }
130 }
131 "Tj" | "TJ" => match current_encoding {
132 Some(encoding) => {
133 let res = collect_text(&mut current_text, encoding, &operation.operands);
134 if let Err(err) = res {
135 collected_chunks_and_errs.push(Err(err));
136 }
137 }
138 None => warn!("Could not decode extracted text"),
139 },
140 "'" => match current_encoding {
144 Some(encoding) => {
145 if !current_text.ends_with('\n') {
146 current_text.push('\n');
147 }
148 let res = collect_text(&mut current_text, encoding, &operation.operands);
149 if let Err(err) = res {
150 collected_chunks_and_errs.push(Err(err));
151 }
152 }
153 None => warn!("Could not decode extracted text"),
154 },
155 "\"" => match current_encoding {
161 Some(encoding) => {
162 if !current_text.ends_with('\n') {
163 current_text.push('\n');
164 }
165 if let Some(string_operand) = operation.operands.get(2) {
166 let res = collect_text(
167 &mut current_text,
168 encoding,
169 std::slice::from_ref(string_operand),
170 );
171 if let Err(err) = res {
172 collected_chunks_and_errs.push(Err(err));
173 }
174 }
175 }
176 None => warn!("Could not decode extracted text"),
177 },
178 "T*" if !current_text.ends_with('\n') => current_text.push('\n'),
183 "T*" => {}
184 "ET" if !current_text.ends_with('\n') => current_text.push('\n'),
185 "ET" => {}
186 _ => {}
187 }
188 }
189 if !current_text.is_empty() {
190 collected_chunks_and_errs.push(Ok(current_text));
191 }
192
193 Ok(collected_chunks_and_errs)
194 }
195
196 pub fn replace_text(
197 &mut self,
198 page_number: u32,
199 text: &str,
200 other_text: &str,
201 default_str: Option<&str>,
202 ) -> Result<()> {
203 let page = page_number.saturating_sub(1) as usize;
204 let page_id = self
205 .page_iter()
206 .nth(page)
207 .ok_or(Error::PageNumberNotFound(page_number))?;
208 let encodings: BTreeMap<Vec<u8>, Encoding> = self
209 .get_page_fonts(page_id)?
210 .into_iter()
211 .map(|(name, font)| font.get_font_encoding(self).map(|it| (name, it)))
212 .collect::<Result<BTreeMap<Vec<u8>, Encoding>>>()?;
213 let content_data = self.get_page_content(page_id)?;
214 let mut content = Content::decode(&content_data)?;
215 let mut current_encoding = None;
216 for operation in &mut content.operations {
217 match operation.operator.as_ref() {
218 "Tf" => {
219 let current_font = operation
220 .operands
221 .first()
222 .ok_or_else(|| Error::Syntax("missing font operand".to_string()))?
223 .as_name()?;
224 current_encoding = encodings.get(current_font);
225 }
226 "Tj" | "TJ" => match current_encoding {
227 Some(encoding) => try_to_replace_encoded_text(
228 operation,
229 encoding,
230 text,
231 other_text,
232 default_str.unwrap_or(""),
233 )?,
234 None => {
235 warn!(
236 "Could not decode extracted text, some of the occurances might not be properly replaced"
237 )
238 }
239 },
240 _ => {}
241 }
242 }
243 let modified_content = content.encode()?;
244 self.change_page_content(page_id, modified_content)
245 }
246
247 pub fn replace_partial_text(
248 &mut self,
249 page_number: u32,
250 search_text: &str,
251 replacement_text: &str,
252 default_char: Option<&str>,
253 ) -> Result<usize> {
254 let page = page_number.saturating_sub(1) as usize;
255 let page_id = self
256 .page_iter()
257 .nth(page)
258 .ok_or(Error::PageNumberNotFound(page_number))?;
259
260 let encodings: BTreeMap<Vec<u8>, Encoding> = self
261 .get_page_fonts(page_id)?
262 .into_iter()
263 .map(|(name, font)| font.get_font_encoding(self).map(|it| (name, it)))
264 .collect::<Result<BTreeMap<Vec<u8>, Encoding>>>()?;
265
266 let content_data = self.get_page_content(page_id)?;
267 let mut content = Content::decode(&content_data)?;
268 let mut current_encoding = None;
269 let mut replacement_count = 0;
270
271 for operation in &mut content.operations {
272 match operation.operator.as_ref() {
273 "Tf" => {
274 let current_font = operation
275 .operands
276 .first()
277 .ok_or_else(|| Error::Syntax("missing font operand".to_string()))?
278 .as_name()?;
279 current_encoding = encodings.get(current_font);
280 }
281 "Tj" | "TJ" => {
282 if let Some(encoding) = current_encoding {
283 replacement_count += replace_partial_in_operation(
284 operation,
285 encoding,
286 search_text,
287 replacement_text,
288 default_char.unwrap_or("?"),
289 )?;
290 } else {
291 warn!("No encoding found for text operation");
292 }
293 }
294 _ => {}
295 }
296 }
297
298 if replacement_count > 0 {
299 let modified_content = content.encode()?;
300 self.change_page_content(page_id, modified_content)?;
301 }
302
303 Ok(replacement_count)
304 }
305
306 pub fn insert_image(
307 &mut self,
308 page_id: ObjectId,
309 img_object: Stream,
310 position: (f32, f32),
311 size: (f32, f32),
312 ) -> Result<()> {
313 let img_id = self.add_object(img_object);
314 let img_name = format!("X{}", img_id.0);
315
316 self.add_xobject(page_id, img_name.as_bytes(), img_id)?;
317
318 let mut content = self.get_and_decode_page_content(page_id)?;
319 content.operations.push(Operation::new("q", vec![]));
320 content.operations.push(Operation::new(
321 "cm",
322 vec![
323 size.0.into(),
324 0.into(),
325 0.into(),
326 size.1.into(),
327 position.0.into(),
328 position.1.into(),
329 ],
330 ));
331 content.operations.push(Operation::new(
332 "Do",
333 vec![Name(img_name.as_bytes().to_vec())],
334 ));
335 content.operations.push(Operation::new("Q", vec![]));
336
337 self.change_page_content(page_id, content.encode()?)
338 }
339
340 pub fn insert_form_object(&mut self, page_id: ObjectId, form_obj: Stream) -> Result<()> {
341 let form_id = self.add_object(form_obj);
342 let form_name = format!("X{}", form_id.0);
343
344 let mut content = self.get_and_decode_page_content(page_id)?;
345 content.operations.insert(0, Operation::new("q", vec![]));
346 content.operations.push(Operation::new("Q", vec![]));
347 content.operations.push(Operation::new(
348 "Do",
349 vec![Name(form_name.as_bytes().to_vec())],
350 ));
351 let modified_content = content.encode()?;
352 self.add_xobject(page_id, form_name, form_id)?;
353
354 self.change_page_content(page_id, modified_content)
355 }
356}
357fn collect_text(text: &mut String, encoding: &Encoding, operands: &[Object]) -> Result<()> {
358 for operand in operands.iter() {
359 match operand {
360 Object::String(bytes, _) => {
361 encoding.write_to_string(bytes, text)?;
362 }
363 Object::Array(arr) => {
364 collect_text(text, encoding, arr)?;
365 text.push(' ');
366 }
367 Object::Integer(i) if *i < -100 => {
368 text.push(' ');
369 }
370 _ => {}
371 }
372 }
373 Ok(())
374}
375pub fn substr(s: &str, start: usize, len: usize) -> &str {
376 let mut indices = s.char_indices();
377
378 for _ in 0..start {
379 if indices.next().is_none() {
380 return "";
381 }
382 }
383
384 let Some((start_idx, _)) = indices.next() else {
385 return "";
386 };
387
388 let end_idx = indices
389 .nth(len.saturating_sub(1))
390 .map(|(idx, _)| idx)
391 .unwrap_or(s.len());
392
393 &s[start_idx..end_idx]
394}
395pub fn substring(s: &str, start: usize) -> &str {
396 s.char_indices()
397 .nth(start)
398 .map(|(idx, _)| &s[idx..])
399 .unwrap_or("")
400}
401
402fn encode(encoding: &Encoding, txt: &str, default_str: &str) -> Vec<u8> {
403 if txt.chars().count() > 1 {
404 let mut cur = 0;
405 let mut result = Vec::new();
406 while cur < txt.chars().count() {
407 let c = substr(txt, cur, 1);
408 result.extend_from_slice(&encode(encoding, c, default_str));
409 cur += 1;
410 }
411 result
412 } else {
413 let encoded_bytes = Document::encode_text(encoding, txt);
414 if !encoded_bytes.is_empty() {
415 encoded_bytes
416 } else {
417 Document::encode_text(encoding, default_str)
418 }
419 }
420}
421fn try_to_replace_encoded_text(
422 operation: &mut Operation,
423 encoding: &Encoding,
424 text_to_replace: &str,
425 replacement: &str,
426 default_str: &str,
427) -> Result<()> {
428 for operand in &mut operation.operands {
429 match operand {
430 Object::String(bytes, _) => {
431 let decoded_text = Document::decode_text(encoding, bytes)?;
432 if decoded_text == text_to_replace {
433 let encoded_bytes = encode(encoding, replacement, default_str);
434 *bytes = encoded_bytes;
435 }
436 }
437 Object::Array(arr) => {
438 let mut str_collected = String::new();
439 collect_text(&mut str_collected, encoding, arr)?;
440 if str_collected == text_to_replace {
441 let encoded_replacement = encode(encoding, replacement, default_str);
452 let mut placed = false;
453 for item in arr.iter_mut() {
454 if let Object::String(bytes, _f) = item {
455 if placed {
456 *bytes = Vec::new();
457 } else {
458 bytes.clone_from(&encoded_replacement);
459 placed = true;
460 }
461 }
462 }
463 }
464 }
465 _ => {}
466 }
467 }
468
469 Ok(())
470}
471
472fn replace_partial_in_operation(
473 operation: &mut Operation,
474 encoding: &Encoding,
475 search_text: &str,
476 replacement_text: &str,
477 default_char: &str,
478) -> Result<usize> {
479 let mut replacement_count = 0;
480
481 for operand in &mut operation.operands {
482 match operand {
483 Object::String(bytes, _) => {
484 let decoded_text = Document::decode_text(encoding, bytes)?;
485 if decoded_text.contains(search_text) {
486 let new_text = decoded_text.replace(search_text, replacement_text);
487 let encoded_bytes = encode_with_fallback(encoding, &new_text, default_char);
488 *bytes = encoded_bytes;
489 replacement_count += decoded_text.matches(search_text).count();
490 }
491 }
492 Object::Array(arr) => {
493 replacement_count += replace_partial_in_array(
494 arr,
495 encoding,
496 search_text,
497 replacement_text,
498 default_char,
499 )?;
500 }
501 _ => {}
502 }
503 }
504
505 Ok(replacement_count)
506}
507
508fn replace_partial_in_array(
509 arr: &mut [Object],
510 encoding: &Encoding,
511 search_text: &str,
512 replacement_text: &str,
513 default_char: &str,
514) -> Result<usize> {
515 let mut replacement_count = 0;
516
517 for item in arr.iter_mut() {
518 if let Object::String(bytes, _) = item {
519 let decoded_text = Document::decode_text(encoding, bytes)?;
520 if decoded_text.contains(search_text) {
521 let new_text = decoded_text.replace(search_text, replacement_text);
522 let encoded_bytes = encode_with_fallback(encoding, &new_text, default_char);
523 *bytes = encoded_bytes;
524 replacement_count += decoded_text.matches(search_text).count();
525 }
526 }
527 }
528
529 Ok(replacement_count)
530}
531
532fn encode_with_fallback(encoding: &Encoding, text: &str, default_char: &str) -> Vec<u8> {
533 let encoded = Document::encode_text(encoding, text);
534 if !encoded.is_empty() {
535 return encoded;
536 }
537
538 encode(encoding, text, default_char)
539}
540
541pub fn decode_xref_stream(mut stream: Stream) -> Result<(Xref, Dictionary)> {
543 if stream.is_compressed() {
544 stream.decompress()?;
545 }
546 let mut dict = stream.dict;
547 let mut reader = Cursor::new(stream.content);
548 let size = dict
549 .get(b"Size")
550 .and_then(Object::as_i64)
551 .map_err(|_| ParseError::InvalidXref)?;
552 let mut xref = Xref::new(size as u32, XrefType::CrossReferenceStream);
553 {
554 let section_indice = dict
555 .get(b"Index")
556 .and_then(parse_integer_array)
557 .unwrap_or_else(|_| vec![0, size]);
558 let field_widths = dict
559 .get(b"W")
560 .and_then(parse_integer_array)
561 .map_err(|_| ParseError::InvalidXref)?;
562
563 if field_widths.len() < 3
564 || field_widths[0].is_negative()
565 || field_widths[1].is_negative()
566 || field_widths[2].is_negative()
567 {
568 return Err(ParseError::InvalidXref.into());
569 }
570
571 let mut bytes1 = vec![0_u8; field_widths[0] as usize];
572 let mut bytes2 = vec![0_u8; field_widths[1] as usize];
573 let mut bytes3 = vec![0_u8; field_widths[2] as usize];
574
575 for i in 0..section_indice.len() / 2 {
576 let start = section_indice[2 * i];
577 let count = section_indice[2 * i + 1];
578
579 for j in 0..count {
580 let entry_type = if !bytes1.is_empty() {
581 read_big_endian_integer(&mut reader, bytes1.as_mut_slice())?
582 } else {
583 1
584 };
585 match entry_type {
586 0 => {
587 read_big_endian_integer(&mut reader, bytes2.as_mut_slice())?;
589 read_big_endian_integer(&mut reader, bytes3.as_mut_slice())?;
590 }
591 1 => {
592 let offset = read_big_endian_integer(&mut reader, bytes2.as_mut_slice())?;
594 let generation = if !bytes3.is_empty() {
595 read_big_endian_integer(&mut reader, bytes3.as_mut_slice())?
596 } else {
597 0
598 } as u16;
599 xref.insert((start + j) as u32, XrefEntry::Normal { offset, generation });
600 }
601 2 => {
602 let container =
604 read_big_endian_integer(&mut reader, bytes2.as_mut_slice())?;
605 let index =
606 read_big_endian_integer(&mut reader, bytes3.as_mut_slice())? as u16;
607 xref.insert(
608 (start + j) as u32,
609 XrefEntry::Compressed { container, index },
610 );
611 }
612 _ => {}
613 }
614 }
615 }
616 }
617 dict.remove(b"Length");
618 dict.remove(b"W");
619 dict.remove(b"Index");
620 Ok((xref, dict))
621}
622
623fn read_big_endian_integer(reader: &mut Cursor<Vec<u8>>, buffer: &mut [u8]) -> Result<u32> {
624 reader.read_exact(buffer)?;
625 let mut value = 0;
626 for &mut byte in buffer {
627 value = (value << 8) + u32::from(byte);
628 }
629 Ok(value)
630}
631
632fn parse_integer_array(array: &Object) -> Result<Vec<i64>> {
633 let array = array.as_array()?;
634 let mut out = Vec::with_capacity(array.len());
635
636 for n in array {
637 out.push(n.as_i64()?);
638 }
639
640 Ok(out)
641}
642
643#[cfg(test)]
644mod tests {
645 #[cfg(not(feature = "async"))]
646 #[test]
647 fn load_and_save() {
648 use crate::Document;
649 use crate::creator::tests::{create_document, save_document};
650
651 use std::fs::File;
653 use std::io::Cursor;
654 let temp_dir = tempfile::tempdir().unwrap();
656 let file_path = temp_dir.path().join("test_1_load_and_save.pdf");
657
658 let mut doc = create_document();
659
660 save_document(&file_path, &mut doc);
661
662 let in_file = File::open(file_path).unwrap();
663 let mut in_doc = Document::load_from(in_file).unwrap();
664
665 let out_buf = Vec::new();
666 let mut memory_cursor = Cursor::new(out_buf);
667 in_doc.save_to(&mut memory_cursor).unwrap();
668 assert!(!memory_cursor.get_ref().is_empty());
670 }
671
672 #[test]
673 fn extract_text_chunks() {
674 use crate::creator::tests::create_document_with_texts;
675
676 let text1 = "Hello world!";
677 let text2 = "Ferris is the best!";
678 let doc = create_document_with_texts(&[text1, text2]);
679 let extracted_texts = doc.extract_text_chunks(&[1, 2]);
680 assert_eq!(extracted_texts.len(), 2);
681 assert_eq!(
682 [
683 extracted_texts[0].as_ref().unwrap().trim(),
684 extracted_texts[1].as_ref().unwrap().trim()
685 ],
686 [text1, text2]
687 );
688 }
689
690 #[test]
691 fn extract_text_concatenates_text_from_multiple_pages() {
692 use crate::creator::tests::create_document_with_texts;
693
694 let text1 = "Hello world!";
695 let text2 = "Ferris is the best!";
696 let doc = create_document_with_texts(&[text1, text2]);
697 let extracted_text = doc.extract_text(&[1, 2]);
698 assert_eq!(extracted_text.unwrap(), format!("{text1}\n{text2}\n"));
699 }
700
701 #[test]
702 fn test_replace_partial_text() {
703 use crate::creator::tests::create_document_with_texts;
704
705 let mut doc = create_document_with_texts(&["Hello World! Hello Universe!"]);
706 let replacements = doc.replace_partial_text(1, "Hello", "Hi", None).unwrap();
707 assert_eq!(replacements, 2); let extracted_text = doc.extract_text(&[1]).unwrap();
710 assert!(extracted_text.contains("Hi World! Hi Universe!"));
711 }
712
713 #[test]
717 fn extract_text_handles_apostrophe_show_text_op() {
718 use crate::Object;
719 use crate::content::Operation;
720 use crate::creator::tests::create_document_with_operations;
721
722 let doc = create_document_with_operations(vec![
723 Operation::new("BT", vec![]),
724 Operation::new("Tf", vec!["F1".into(), 12.into()]),
725 Operation::new("Td", vec![100.into(), 700.into()]),
726 Operation::new("Tj", vec![Object::string_literal("first")]),
727 Operation::new("'", vec![Object::string_literal("second")]),
728 Operation::new("'", vec![Object::string_literal("third")]),
729 Operation::new("ET", vec![]),
730 ]);
731
732 let text = doc.extract_text(&[1]).unwrap();
733 assert!(text.contains("first"), "Tj string lost: {text:?}");
734 assert!(text.contains("second"), "first ' string lost: {text:?}");
735 assert!(text.contains("third"), "second ' string lost: {text:?}");
736 }
737
738 #[test]
743 fn extract_text_handles_quote_show_text_op() {
744 use crate::Object;
745 use crate::content::Operation;
746 use crate::creator::tests::create_document_with_operations;
747
748 let doc = create_document_with_operations(vec![
749 Operation::new("BT", vec![]),
750 Operation::new("Tf", vec!["F1".into(), 12.into()]),
751 Operation::new("Td", vec![100.into(), 700.into()]),
752 Operation::new(
753 "\"",
754 vec![0.into(), 0.into(), Object::string_literal("from-quote-op")],
755 ),
756 Operation::new("ET", vec![]),
757 ]);
758
759 let text = doc.extract_text(&[1]).unwrap();
760 assert!(
761 text.contains("from-quote-op"),
762 "\" string operand lost: {text:?}"
763 );
764 }
765
766 #[test]
771 fn extract_text_preserves_line_breaks_for_t_star() {
772 use crate::Object;
773 use crate::content::Operation;
774 use crate::creator::tests::create_document_with_operations;
775
776 let doc = create_document_with_operations(vec![
777 Operation::new("BT", vec![]),
778 Operation::new("Tf", vec!["F1".into(), 12.into()]),
779 Operation::new("Td", vec![100.into(), 700.into()]),
780 Operation::new("Tj", vec![Object::string_literal("line-one")]),
781 Operation::new("T*", vec![]),
782 Operation::new("Tj", vec![Object::string_literal("line-two")]),
783 Operation::new("ET", vec![]),
784 ]);
785
786 let text = doc.extract_text(&[1]).unwrap();
787 let one = text.find("line-one").expect("line-one missing");
788 let two = text.find("line-two").expect("line-two missing");
789 assert!(one < two, "order wrong: {text:?}");
790 let between = &text[one + "line-one".len()..two];
791 assert!(
792 between.contains('\n'),
793 "T* did not insert a line break between Tj strings: between={between:?}"
794 );
795 }
796}