Unable to modify pdf files using lopdf crate

hi, I am using this library to directly modify my pdf files:

I have this code so far:

let mut doc = Document::load("example.pdf")?;
doc.version = "1.4".to_string();
doc.replace_text(1, "[old]", "new")?;
doc.save("modified.pdf")?;

Whilst it does create modified.pdf file however there are no changes made to it, why this may be the case?

It's hard to say without knowing your example.pdf.
I would simply assume that page 1 does not contain the string [old] in a way that lopdf can see it.
Have you used any of the available API functions to extract the text to see what it is?
Maybe consider first adding some new text instead of replacing somewhere, to see if it correctly adds content and writes the file.

I see thanks

This is my code now:

    let mut doc = Document::load("Sample.pdf").unwrap();
    doc.version = "1.4".to_string();
    doc.replace_text(1, "Old", "new").unwrap();
    doc.save("Modified.pdf").unwrap();

and I believe I am following the docs but I don't know what I am doing wrong.

I am able to extract the text though just fine:

    let mut doc = Document::load("Sample.pdf").unwrap();
    doc.version = "1.4".to_string();

    let text = doc.extract_text(&[1]).unwrap();
    println!("{}", text);

This is all that is in my Sample.pdf file:

I never used lopdf myself.
So I took their main example and added the bits of your code as well.
Full code is here: Rust Playground

This works as expected. That's the extent I can help you.

2 Likes