6

I'm using itextsharp to generate the PDFs, but I need to change some text dynamically. I know that it's possible to change if there's any AcroField, but my PDF doen's have any of it. It just has some pure texts and I need to change some of them.

Does anyone know how to do it?

Matthias
  • 15,919
  • 5
  • 39
  • 84
André Miranda
  • 6,420
  • 20
  • 70
  • 94

3 Answers3

6

Actually, I have a blog post on how to do it! But like IanGilham said, it depends on whether you have control over the original PDF. The basic idea is you setup a form on the page and replace the form fields with the text you want. (You can style the form so it doesn't look like a form)

If you don't have control over the PDF, let me know how to do it!

Here is a link to the full post:

Using a template to programmatically create PDFs with C# and iTextSharp

Cyril Gandon
  • 16,830
  • 14
  • 78
  • 122
John B
  • 20,062
  • 35
  • 120
  • 170
  • 2
    Unless I'm missing something, that blog post doesn't show how to modify text in a PDF. – Cocowalla May 14 '11 at 13:01
  • Yes, you're missing the entirety of the post! If you look at the code, it is replacing text that is referenced by their keys in a form. – John B Jun 15 '11 at 17:21
  • @JohnBubriski Do you perhaps have a example of a not form replace text? – Frederiek Oct 19 '11 at 10:34
  • @Frederiek Off hand I don't. Do you have an existing PDF that you need to work on? What is your scenario? – John B Oct 24 '11 at 18:44
  • @Frederiek I just remembered something dirty I've done in the past. You can "white out" an area of the PDF with a big white square. Then you can write/draw whatever content you want on top of the white area. This only works if your PDF is a standard color, and if your content is easy to inject in. I did this with a custom address at the bottom of a PDF, so it didn't matter if it was 100% precise. Hope that helps, but let me know about your situation. – John B Oct 24 '11 at 18:46
  • @JohnBubriski we are ok now, but thanks for the help anyway (+1) – Frederiek Oct 25 '11 at 07:03
2

I haven't used itextsharp, but I have been using PDFNet SDK to explore the content of a large pile of PDFs for localisation over the last few weeks.

I would say that what you require is absolutely achievable, but how difficult it is will depend entirely on how much control you have over the quality of the files. In my case, the files can be constructed from any combination of images, text in any random order, tables, forms, paths, single pixel graphics and scanned pages, some of which are composed from hundreds of smaller images. Let's just say we're having fun with it.

In the PDFTron way of doing things, you would have to implement a viewer (sample available), and add some code over a text selection. Given the complexities of the format, it may be necessary to implement a simple editor in a secondary dialog with the ability to expand the selection to the next line (or whatever other fundamental object is used to make up text). The string could then be edited and applied by copying the entire page of the document into a new page, replacing the selected elements with your new string. You would probably have to do some mathematics to get this to work well though, as just about everything in PDF is located on the page by means of an affine transform.

Good luck. I'm sure there are people on here with some experience of itextsharp and PDF in general.

Ian Gilham
  • 1,916
  • 3
  • 20
  • 31
-1

This question comes up from time to time on the mailing list. The same answer is given time and time again - NO. See this thread for the official answer from the person who created iText.

This question should be a FAQ on the itextsharp tag wiki.

Community
  • 1
  • 1
kuujinbo
  • 9,272
  • 3
  • 44
  • 57