Solved

Replace text in pdf file using itextshape in c#

Posted on 2016-07-26
7
754 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this video, we show how to perform Bates Numbering/Stamping of PDF documents using Power PDF Advanced, the newest product from the Document Imaging division of Nuance Communications. There are two editions of Power PDF — Standard and Advanced. Th…
We often encounter PDF files that are pure images, that is, they do not have text characters, but instead contain only raster graphics. The most common causes of this are document scanning software and faxing software/services that create image-only…

828 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