Solved

Replace text in pdf file using itextshape in c#

Posted on 2016-07-26
7
605 Views
Last Modified: 2016-07-28
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.AddImage(image);
            PdfContentByte canvas = stamper.GetOverContent(2);
            ColumnText.ShowTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(DateTime.Now.ToShortDateString()), 0, 0, 0);
            stamper.Close();
        }

    }

Open in new window


Thanks in Advance Experts
0
Comment
Question by:karthik80c
  • 4
  • 3
7 Comments
 
LVL 7

Expert Comment

by:Jonathan Kelly
ID: 41729251
Hi,

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.

Rgds,
Jonathan
0
 

Author Comment

by:karthik80c
ID: 41729285
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
0
 
LVL 7

Expert Comment

by:Jonathan Kelly
ID: 41729297
You want to execute a text search and replace on the pdf  ?
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:karthik80c
ID: 41729401
yes
0
 
LVL 7

Accepted Solution

by:
Jonathan Kelly earned 500 total points
ID: 41729803
After a fair bit of googling I found this

http://www.vbforums.com/showthread.php?681178-RESOLVED-quot-replace-quot-Words-in-PDF-file-using-iTextSharp

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.
0
 

Author Comment

by:karthik80c
ID: 41730642
thank you for your suggestion.i will try this.
0
 

Author Closing Comment

by:karthik80c
ID: 41732459
Thanks Exactly what we want . Thanks Jonathan
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C# Desktop Application 3 36
VB.NET 2008 - Windows Protected Your PC 7 16
C# Gridview 1 33
SQL Query Help Top 1 and Distinct? 6 26
In a previous article published here at Experts Exchange, Signature Image with Transparent Background (http://www.experts-exchange.com/Web_Development/Document_Imaging/A_12380-Signature-Image-with-Transparent-Background.html), I explained how to cre…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Sometimes we receive PDF files that are in the wrong orientation. They may be sideways or even upside down. This most commonly happens with scanned or faxed documents. It is possible to rotate the view of these PDFs with the free Adobe Reader produc…

822 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question