Replace text in pdf file using itextshape in c#

karthik80c used Ask the Experts™
Hi Experts,

Am using the below code for replace image and text on pdf, its is good working on the image replace part, but i dont know the how can write the text replace in same code.

private void AddAnImage()
        string qrfile = qrcode();

        using (var inputPdfStream = new FileStream(Server.MapPath("PDF/input.pdf"), FileMode.Open))
        using (var inputImageStream = new FileStream(qrfile, FileMode.Open))
        using (var outputPdfStream = new FileStream(Server.MapPath("PDF/output.pdf"), FileMode.Create))
            PdfReader reader = new PdfReader(inputPdfStream);
            PdfStamper stamper = new PdfStamper(reader, outputPdfStream);
            PdfContentByte pdfContentByte = stamper.GetOverContent(1);
            var image = iTextSharp.text.Image.GetInstance(inputImageStream);
            image.SetAbsolutePosition(50, 75);
            PdfContentByte canvas = stamper.GetOverContent(2);
            ColumnText.ShowTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(DateTime.Now.ToShortDateString()), 0, 0, 0);


Open in new window

Thanks in Advance Experts
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

I have worked with iText with vb previously and I used code similar to below to build up pdf files on the fly.

Dim doc As Document = New Document
Dim txt_JourneyLabel As New Chunk()

txt_JourneyLabel = New Chunk("SOME TEXT HERE", font_courier)
doc.Add(New Paragraph(txt_JourneyLabel))

This may not be the most efficient piece of code ever written but it does work.



Hi Jonathan

i dont think the code posted is to search and replace the Text . Can u please post again for this criteria to match a string and replace them in pdf
You want to execute a text search and replace on the pdf  ?


After a fair bit of googling I found this

The idea (I think) is to
1. Read the pdf file text.
2. Locate the search word x,y coordinates
3. Create a solid block with same color as background and place at the above coordinates
4. Create new text at the coordinates and save as new pdf file.


thank you for your suggestion.i will try this.


Thanks Exactly what we want . Thanks Jonathan

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial