I am trying to programmatically create a number of PDF documents with a watermark on each page using itextsharp (a C# port of Java's itext).
I am able to do this after the document has been created using a PdfStamper. However this seems to involve re-opening the document reading it and then creating a new document with the watermark on each page.
Is there a way of doing this during document creation?
Cheers,
Tim.
After digging into it I found the best way was to add the watermark to each page as it was created. To do this I created a new class and implemented the IPdfPageEvent interface as follows:
This object is registered to handle the events as follows:
In iTextSharp you should be able to programmatically add a watermark e.g.
I used the first solution. I was having trouble getting it to work at first. I getting green underlines under all of my public voids saying that it was going to hide some inherit member.
Basically I realized that I already had added a PagePageEventHelper and I basically just cut out the code for the OnStartPage. ALSO! For some reason I had to make all of my public void's public override void.
Although Tim's solution seems very nice, I have managed to do the same thing (I believe) using the following code (perhaps iTextSharp was improved a bit since then...):
This will add a watermark on all pages of a PDF document that is provided as a byte array.
(You don't need to do it while creating the PDF.)
It seems working for both landscape and portrait and it probably works for documents with mixed orientations.
Cheers! :)
Yes, the Watermark class seems to be no more - odd. However in the process of converting to iTextSharp 5.3, I found a simple way to add a watermark to a new document.