title: Remove mask image"Watermark" from PDF itextsharp
body: <p>I need to remove a mask image from a pdf document. The same image is used as "watermark" on all pages.
  Is this possible with iTextSharp,and if yes I would a appreciate any code examples.</p>
  
  <p>The reason I need to remove the watermark is because I need to OCR the pdf but the text under the "watermark" is getting corrupted.</p>
  
  <p>I have search the web but I have managed to find anything that works.</p>
  
  <p>Thank you,
  Dimitris</p>
  
  <p>with this code I can replace the image but If I don't replace  the image...then the image do disapear but I get an error when I open the pdf in adobe Reades</p>
  
  <pre><code>        string pdfTempFilename = "1_tmp.pdf";            
          FileStream os = new FileStream(pdfTempFilename, FileMode.Create);
          PdfReader pdf = new PdfReader(@"D:\import\pdf.pdf");
          PdfStamper stp = new PdfStamper(pdf, os);
          PdfWriter writer = stp.Writer;
          iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance("mask.gif");
          PdfDictionary pg = pdf.GetPageN(1);
          PdfDictionary res = (PdfDictionary)PdfReader.GetPdfObject(pg.Get(PdfName.RESOURCES));
  
          PdfDictionary xobj = (PdfDictionary)PdfReader.GetPdfObject(res.Get(PdfName.XOBJECT));                                                                       
  
          if (xobj != null) {
  
              foreach (PdfName name in xobj.Keys){            
                  PdfObject obj = xobj.Get(name);
                  if (obj.IsIndirect()) {
                      PdfDictionary tg = (PdfDictionary)PdfReader.GetPdfObject(obj);
                      PdfName type = tg.GetAsName(PdfName.SUBTYPE);
  
  
                      PdfObject ind = tg.Get(PdfName.MASK);
  
                      if (PdfName.IMAGE.Equals(type) &amp;&amp; ind !=null)
                      {
                          PdfReader.KillIndirect(obj);                           
  
                          writer.AddDirectImageSimple(img, (PRIndirectReference)obj);                            
                          break;
                      }
                  }
              }
         }
         stp.Close();
  </code></pre>
  
  <p>`</p>