title: iText 7 - Add and Remove Watermark on a PDF
body: <p>I would like to add and remove a watermark to a PDF using iText 7. I was able to add the watermark, but unable to remove it again. I could only find relevant code/examples related to iText 5. Any pointers appreciated, thanks.</p>
  
  <p>This is how I added the Watermark (using Layers):</p>
  
  <pre><code>            pdfDoc = new PdfDocument(new PdfReader(sourceFile), new PdfWriter(destinationPath));
              var numberOfPages = pdfDoc.GetNumberOfPages();
  
              PageSize ps = pdfDoc.GetDefaultPageSize();
  
              for (var i = 1; i &lt;= numberOfPages; i++)
              {
                  PdfPage page = pdfDoc.GetPage(i);
                  PdfLayer layer = new PdfLayer("watermark", pdfDoc);
                  var canvas = new PdfCanvas(page);
                  var pageSize = page.GetPageSize();
                  var paragraph = new Paragraph(message.WatermarkText).SetFontSize(60);
                  paragraph.SetFontColor(Color.BLACK, 0.2f);
  
                  Canvas canvasModel;
                  canvas.BeginLayer(layer);
                  canvasModel = new Canvas(canvas, pdfDoc, ps);
                  canvasModel.ShowTextAligned(paragraph, pageSize.GetWidth() / 2, pageSize.GetHeight() / 2, pdfDoc.GetPageNumber(page), TextAlignment.CENTER, VerticalAlignment.MIDDLE, 45);
                  canvasModel.SetFontColor(Color.GREEN, 0.2f);
                  canvas.EndLayer();
              }
              pdfDoc.Close();
  </code></pre>
  
  <p>This is what I have tried to remove the watermark. I want to remove it completely, not just set the layer to not display.(any sample code appreciated):</p>
  
  <pre><code>            pdfDoc = new PdfDocument(new PdfReader(sourceFile), new PdfWriter(destinationPath));                     
               IList&lt;PdfLayer&gt; layers = pdfDoc.GetCatalog().GetOCProperties(true).GetLayers();              
  
              for (var i = 0; i &lt;= layers.Count; i++)
              {
                  var t = layers[i].GetPdfObject().Get(PdfName.Name);
  
                  if (t.ToString().Equals("watermark"))
                  {
                          //Not what I want..need to remove the layer
                          layers[i].SetOn(false);
                          //This does not work...
                          //layers.RemoveAt(i);
                      }
                  }
                  pdfDoc.Close();
  </code></pre>