4

I have a pdf template without AcroFields and i need to replace text in it. The text is formated like this ((aFieldToReplace)), but there are also tables that need filled up with a n-numbered rows.

Is there any good tutorial, resource or sample to find?

Is there a way to replace a text in a PDF file with itextsharp? has more or less the same question but the answer ignores the "no Acrofield" part of the question.

EDIT:
To make it even harder, i have multiple templates that i can use. The templates have all there own formatting-style (font, color,...)

EDIT 2:
The purpose is to create a report with some data in a database. The data in a database is coming from several forms in a ASP.NET MVC application.
The report could have several layouts depending on the chosen template.

Templates should be addable dynamically, so i can't create the layout from scratch. I really need to get the layout from a template.

Community
  • 1
  • 1
Frederiek
  • 1,605
  • 2
  • 17
  • 32
  • Is there a particular reason to do this without Acrofields? – Jonathan Oct 19 '11 at 12:22
  • @Jonathan Becuse the templates where made in and i need to append tables so Acrofields aren't a option for that, isn't it? – Frederiek Oct 19 '11 at 12:41
  • The tables could be done if it's permissible for them to start on a new page every time – copying pages between PDFs is easy, as is adding entirely new pages. Of course you'd still need to find a placeholder. – millimoose Oct 19 '11 at 12:58
  • @Inerdia It is not really a option. There are lot's of tablles and they have only 4 to 5 rows so they take between a 4th or a 3th from a page. So that would be wasteful to let them each start on a new page. – Frederiek Oct 19 '11 at 13:02
  • We're back to “nearly impossible” then. – millimoose Oct 19 '11 at 13:11
  • PDF templates with Acrofields can be very versatile, and you can still append content to the documents without using the Acrofields. – Jonathan Oct 19 '11 at 13:12
  • If the templates aren't to complicated, you could recreate the templates in html and then use itextsharp to parse them into PDFs. – Jonathan Oct 19 '11 at 13:24
  • @Jonathan The templates aren't that complicated, but i have 5 different now, and it should be possible to add new templates by a webmaster (not a programmer) There are different companies who have there own template, and in the future there need to be more companies involved – Frederiek Oct 19 '11 at 13:25
  • @Frederiek a webmaster should be able to do general html, they could even probably do it with microsoft word and save as html. If you document how they templates are structured and what the keywords are for placeholders. You shouldn't have any problem letting someone else maintain it. How are the current templates being generated? – Jonathan Oct 19 '11 at 13:32
  • @Jonathan with word. The website is able to generate word files from a template, but porting those files to pdf was out off the question because we needed to install word on the server. We are now looking for a way to have create the same files from a pdf template. [See this post](http://stackoverflow.com/questions/7792355/how-should-i-export-data-to-office-documents-and-pdf-in-asp-net-mvc-3) – Frederiek Oct 19 '11 at 13:42
  • @Frederiek The way it's sounding with the current pdf templates, I have to agree with Inerdia. Flat PDF templates with placeholders won't allow you to achieve what you are looking for. I think HTML templates generated from word might be the way you should go, but I'm not the one dictating requirements. You may need have a powwow with your boss and reevaluate the scope of the project. – Jonathan Oct 19 '11 at 14:01
  • @Jonathan well the boss said, we can try the html template angle. thx for the help anyway – Frederiek Oct 19 '11 at 14:21
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/4386/discussion-between-frederiek-and-jonathan) – Frederiek Oct 19 '11 at 14:21

2 Answers2

1

Quoting the excellent iText in Action:

In a PDF document, every character or glyph on a PDF page has its fixed position, regardless of the application that’s used to view the document. […] Suppose you want to replace the word “edit” with the word “manipulate” in a sentence, you’d have to reflow the text. You’d have to reposition all the characters that follow that word. Maybe you’d even have to move a portion of the text to the next page. That’s not trivial, if not impossible. […] Don’t expect any tool to be able to edit a PDF file the same way you’d edit a Word document.

PDF is a document display format. If you want templating you'll probably have to use something else.

millimoose
  • 39,073
  • 9
  • 82
  • 134
  • I read it. I've shown my boss but he said "try anyway", so that is what i'm doing. – Frederiek Oct 19 '11 at 12:42
  • Your boss is an idiot. The best you can do is paint over the placeholders, assuming you can find them. (Without reimplementing something like Adobe Reader's "reflow" feature, which means reimplementing large chunks of PDF rendering.) – millimoose Oct 19 '11 at 12:46
  • I've edited the original question. Maybe it makes more sense now – Frederiek Oct 19 '11 at 12:49
1

@Frederiek:

If you can spend a bit of money, this will do exactly what you want. Check out the demo, it's quite cool. It can reflow the text, replace images, etc. Quite nice.

http://www.iceni.com/infixServer.htm

Let me know if that works for you.

PRB
  • 1,031
  • 11
  • 27
  • 1
    Oh almost forgot the other option I wanted to share. I did this for a client once. Using this tool: http://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/ you can decompress the PDF, use C# to search and replace, then recompress it like this: pdftk mydoc.pdf output mydoc.clear.pdf uncompress and then pdftk mydoc.clear.pdf output mydoc.pdf compress – PRB Jan 07 '12 at 22:29