Solved

Replace text in pdf file using itextshape in c#

Posted on 2016-07-26
7
337 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Update 21-May-2015: I temporarily removed the source code to make major changes to the program. Regards, Joe INTRODUCTION This article presents a solution to a question (http://www.experts-exchange.com/Programming/Installation/Q_28396542.html)…
The Adobe PDF proprietary file format is recognized as secure and formulated. But these PDF files are also prone to corruption and any external threat like virus attacks, improper storage can hit PDF file integrity.This type of damages can make cruc…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now