Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to replace a text in a PDF file with itextsharp?

Tags:

c#

itextsharp

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?

like image 932
André Miranda Avatar asked Apr 27 '09 20:04

André Miranda


People also ask

How do I replace text in a PDF document?

On the PDF file, press “Ctrl+F” on your keyboard and input the text you would like to be replaced. Then type in new text in the input field of Replace to modify the current one to this new text. Click on “Replace” to start replacing PDF texts.

How can I replace words in a PDF online?

Locate the tool to Replace Text PDF and apply the needed changes to the document. Select Replace Text PDF from the toolbar to get started. Click the downward arrow icon next to the DONE button and select save, send, share, download, or print to get your PDFs. Click DONE to finish editing your document.

What is the use of iTextSharp DLL?

What is ITextSharp? iTextSharp is a free and open source assembly that helps to convert page output or HTML content in a PDF file. Now add that DLL in the application.

What is phrase in iTextSharp?

A Phrase is a series of Chunk s. A Phrase has a main Font , but some chunks within the phrase can have a Font that differs from the main Font . All the Chunk s in a Phrase have the same leading .


2 Answers

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

like image 103
John B Avatar answered Oct 05 '22 22:10

John B


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.

like image 42
Ian Gilham Avatar answered Oct 05 '22 21:10

Ian Gilham