2

I have been using iTextsharp to insert watermark in to a pdf & remove them. Thanks for the original posts:

Removing Watermark from a PDF using iTextSharp

Create/Read Advance PDF Report using iTextSharp in C# .NET: Part I

I am able to add the watermark successfully as described in the above posts , however removing the watermark doesn't seem to work correctly. Below is the code block that actually removes the watermark from the PRStream.

if (content.IndexOf("/OC") >= 0 && content.IndexOf(watermarkText) >= 0) {
  //Remove it by giving it zero length and zero data
  stream.Put(PdfName.LENGTH, new PdfNumber(0));
  stream.SetData(new byte[0]);
}

In my case, I have 8 pages with in the PDF with watermarks I've just added the above code to remove, which is being executed but the PdfReader is not getting updated in the PRStream.

Has anyone successfully removed a watermark layer using iTextSharp?

Community
  • 1
  • 1
ShanVeer
  • 21
  • 2
  • Big question first, do you actually need to do this? As one of the authors of an answer that you linked to I can say that my code actually does work in that very controlled scenario, elsewhere is a maybe. You can absolutely manipulate PDFs with iText but the moment you start touching raw dictionaries it is assumed you have a copy of the PDF spec right next to you. Anyway, are you adding "watermarks" using the same code in the post? In the above code is the `stream.Put()` line actually being hit? – Chris Haas Oct 09 '14 at 13:46
  • Hello Chris, thanks for your reply. Yes I need to be able to remove the watermarks I've added. Adding the watermarks exactly the same way as described in the posts. Yes stream.put is being hit as the content string contains both "/OC" & "waterMarkText". But as I said, the prstream is not being refreshed neither the pdfReader. BTW I have tried to remove the layer(by key). But the whole document content is being erased/removed except the watermark. When I was removing the layer I was expecting it to work the other way round by removing the watermark rather than actual content. Many Thanks - SV – ShanVeer Oct 09 '14 at 15:19

0 Answers0