Hitesh Babu
Hitesh Babu

Reputation: 23

Altering PDF through iTextSharp to Replace some existing text with a new text

Please tell me if is there any function in iTextSharp that dose replace(”xx”,”yy”) function in the pdf file without altering remaining parts of the file.

Upvotes: 0

Views: 8843

Answers (2)

Hitesh Babu
Hitesh Babu

Reputation: 23

I found a way to do this through PDF form Fields.

    String formFile = Server.MapPath("~/") + "source.pdf";

    String newFile = Server.MapPath("~/") + "sink.pdf";

    PdfReader reader = new PdfReader(formFile);

    PdfStamper stamper = new PdfStamper(reader, new FileStream(newFile, FileMode.Create));

    AcroFields fields = stamper.AcroFields;

    // set form fields

    fields.SetField("{TO}", "John Doe");

    fields.SetField("{FROM}", "2 Milky Way, London");

    stamper.FormFlattening = true;

    stamper.Close();

Upvotes: 2

John Koerner
John Koerner

Reputation: 38077

Short answer: NO you cannot do that with iText.

Longer Answer: PDF is a display format, so when the PDF is rendered many decisions are made about page and character layout and positioning. Chapter 6 of iText in Action has a great description in the introduction of why it is not a trivial task. You can read Chapter 6 for free from the publisher's website.

Upvotes: 2

Related Questions