| 1 |
#[cfg(not(feature = "async"))] |
| 2 |
use lopdf::{Document, Object}; |
| 3 |
|
| 4 |
#[test] |
| 5 |
#[cfg(all(test, not(feature = "async")))] |
| 6 |
fn test_get_object() { |
| 7 |
use self::Object; |
| 8 |
use lopdf::Dictionary as LoDictionary; |
| 9 |
use lopdf::Stream as LoStream; |
| 10 |
|
| 11 |
let mut doc = Document::new(); |
| 12 |
let id = doc.add_object(Object::string_literal("test")); |
| 13 |
let id2 = doc.add_object(Object::Stream(LoStream::new( |
| 14 |
LoDictionary::new(), |
| 15 |
"stream".as_bytes().to_vec(), |
| 16 |
))); |
| 17 |
|
| 18 |
println!("{:?}", id); |
| 19 |
println!("{:?}", id2); |
| 20 |
|
| 21 |
let obj1_exists = doc.get_object(id).is_ok(); |
| 22 |
let obj2_exists = doc.get_object(id2).is_ok(); |
| 23 |
|
| 24 |
assert!(obj1_exists); |
| 25 |
assert!(obj2_exists); |
| 26 |
} |
| 27 |
|
| 28 |
#[cfg(all(test, not(feature = "async")))] |
| 29 |
mod tests_with_parsing { |
| 30 |
use super::*; |
| 31 |
use lopdf::Result; |
| 32 |
|
| 33 |
fn modify_text() -> Result<bool> { |
| 34 |
let mut doc = Document::load("assets/example.pdf")?; |
| 35 |
doc.version = "1.4".to_string(); |
| 36 |
if let Some(Object::Stream(stream)) = doc.objects.get_mut(&(4, 0)) { |
| 37 |
let mut content = stream.decode_content().unwrap(); |
| 38 |
content.operations[3].operands[0] = Object::string_literal("Modified text!"); |
| 39 |
stream.set_content(content.encode().unwrap()); |
| 40 |
} |
| 41 |
|
| 42 |
// Create temporary folder to store file. |
| 43 |
let temp_dir = tempfile::tempdir()?; |
| 44 |
let file_path = temp_dir.path().join("test_3_modify.pdf"); |
| 45 |
doc.save(file_path)?; |
| 46 |
Ok(true) |
| 47 |
} |
| 48 |
|
| 49 |
#[test] |
| 50 |
fn test_modify() { |
| 51 |
assert!(modify_text().unwrap()); |
| 52 |
} |
| 53 |
|
| 54 |
fn replace_text() -> Result<Document> { |
| 55 |
let mut doc = Document::load("assets/example.pdf")?; |
| 56 |
doc.replace_text(1, "Hello World!", "Modified text!", None)?; |
| 57 |
|
| 58 |
// Create temporary folder to store file. |
| 59 |
let temp_dir = tempfile::tempdir()?; |
| 60 |
let file_path = temp_dir.path().join("test_4_unicode_replace.pdf"); |
| 61 |
doc.save(&file_path)?; |
| 62 |
|
| 63 |
let doc = Document::load(file_path)?; |
| 64 |
Ok(doc) |
| 65 |
} |
| 66 |
|
| 67 |
#[test] |
| 68 |
fn test_replace() { |
| 69 |
assert_eq!(replace_text().unwrap().extract_text(&[1]).unwrap(), "Modified text!\n"); |
| 70 |
} |
| 71 |
|
| 72 |
fn replace_unicode_text() -> Result<Document> { |
| 73 |
let mut doc = Document::load("assets/unicode.pdf")?; |
| 74 |
doc.replace_text(1, "😀", "🔧2", Some("🔨"))?; |
| 75 |
|
| 76 |
let temp_dir = tempfile::tempdir()?; |
| 77 |
let file_path = temp_dir.path().join("test_4_unicode_replace.pdf"); |
| 78 |
doc.save(&file_path)?; |
| 79 |
|
| 80 |
let doc = Document::load(file_path)?; |
| 81 |
Ok(doc) |
| 82 |
} |
| 83 |
|
| 84 |
#[test] |
| 85 |
fn test_unicode_replace() { |
| 86 |
let text = replace_unicode_text().unwrap().extract_text(&[1]).unwrap(); |
| 87 |
assert_eq!(text, "🔧🔨\n🔧\n🔨\n"); |
| 88 |
} |
| 89 |
|
| 90 |
fn get_mut() -> Result<bool> { |
| 91 |
let mut doc = Document::load("assets/example.pdf")?; |
| 92 |
let arr = doc |
| 93 |
.get_object_mut((5, 0))? |
| 94 |
.as_dict_mut()? |
| 95 |
.get_mut(b"Contents")? |
| 96 |
.as_array_mut()?; |
| 97 |
arr[0] = arr[0].clone(); |
| 98 |
Ok(true) |
| 99 |
} |
| 100 |
|
| 101 |
#[test] |
| 102 |
fn test_get_mut() { |
| 103 |
assert!(get_mut().unwrap()); |
| 104 |
} |
| 105 |
} |