Active6 years, 8 months ago
If you want to change the contents of an existing PDF file and add extra content such as watermarks, pagenumbers, extra headers, PdfStamper is the object you need. I have successfully used the following code to insert an image into an existing pdf file to a given absolute position.
I want to add a text to an existing PDF file using iTextSharp, I found different ways but in all of them the writer and reader are separate pdf files.I want a way so I can open a pdf then write different things in different positions.right now I have this code, but it makes a new file.
Christian Mark5,8411313 gold badges3737 silver badges7676 bronze badges
Mina NMina N9622 gold badges55 silver badges1515 bronze badges
1 Answer
You want to add a text to an existing PDF file using iTextSharp, found different ways but in all of them the writer and reader are separate pdf files.
As the normal way in which iText(Sharp) manipulates a PDF using a PdfStamper, can involve major reorganization of existing PDF elements, iText does not edit a file in place. The other way, using append mode, would allow for editing in place; but such an option is not implemented. A big draw-back of in-place editing is that in case of some program failure, the file in question might remain in an intermediary, unusable state.
That being said, you can save the new file to the path of the original file by first reading the file into memory completely and then starting to create the output with the same path. In case of your sample code that would imply at least moving the PdfReader constructor use before the creation of the output stream:
Alternatively you could create the result file in memory, i.e. in a MemoryStream instead of a FileStream, and, when finished, write the contents of the memory stream to your source file path.
mklmkl60.1k1212 gold badges7373 silver badges164164 bronze badges
Not the answer you're looking for? Browse other questions tagged c#itextsharp or ask your own question.
Active5 years, 9 months ago
I want to do the following with iText:
(1) parse an existing PDF file
(2) add some data to it, on the existing single page of the document (such as a timestamp)
(3) write out the document
I just can't seem to figure out how to do this with iText. In pseudo code I would do this:
But for some reason iText's API is so dauntingly complicated that I can't wrap my head around it. The PdfReader actually holds the document model or something (rather than spitting out a document), and you need a PdfWriter to read pages from it... eh?
Wouter LievensWouter Lievens2,51444 gold badges3535 silver badges5555 bronze badges
4 Answers
iText has more than one way of doing this. The
PdfStamper
class is one option. But I find the easiest method is to create a new PDF document then import individual pages from the existing document into the new PDF.Itextsharp Pdf Documentation
This will read in a PDF from
gutchgutchtemplateInputStream
and write it out to outputStream
. These might be file streams or memory streams or whatever suits your application.5,63833 gold badges2828 silver badges5050 bronze badges
Gutch's code is close, but it'll only work right if:
- There are no annotations (links, fields, etc), no Document Structure/Marked Content, no bookmarks, no document-level script, etc, etc, etc...
- The page size happens to be A.4 (decent odds, but it won't work on any ol' PDF you happen to come across)
- You don't mind losing all the original document metadata (producer, creation date, possibly author/title/keywords), and maybe the document ID. You can't copy the creation date and doc ID unless you do some pretty deep hackery on iText itself).
The Approved Method is to do it the other way around. Open the existing document with a PdfStamper, and use the returned PdfContentByte from getOverContent() to write text (and whatever else you might need) directly to the page. No second document needed.
And you can use a ColumnText to handle layout and such for you... no need to get down and dirty with beginText(),setFontAndSize(),drawText(),drawText()...,endText().
Mark StorerMark Storer14.2k11 gold badge3232 silver badges6868 bronze badges
Modify Existing Pdf Using Itextsharp
This is the most complicated scenario I can imagine: I have a PDF file created with Ilustrator and modified with Acrobat to have AcroFields (AcroForm) that I'm going to fill with data with this Java code, the result of that PDF file with the data in the fields is modified adding a Document.
Actually in this case I'm dynamically generating a background that is added to a PDF that is also dynamically generated with a Document with an unknown amount of data or pages.
I'm using JBoss and this code is inside a JSP file (should work in any JSP webserver).
Note: if you are using IExplorer you must submit a HTTP form with POST method to be able to download the file. If not you are going to see the PDF code in the screen. This does not happen in Chrome or Firefox.
There is another solution much simpler, and solves your problem. It depends the amount of text you want to add.
AlfredAlfred
Ramesh. VeguruRamesh. Veguru