title: Watermark a pdf document with itext 7 instead of iTextSharp
body: <p>This was my code for itextsharp which worked ok. It displayed "Quote Only" in the middle of each page in a pdf file.</p>
  
  <pre><code> iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(Server.MapPath(@"~\Content\WaterMarkQuoteOnly.png"));
  
                  PdfReader readerOriginalDoc = new PdfReader(File(all, "application/pdf").FileContents);
                  int n = readerOriginalDoc.NumberOfPages;
                  img.SetAbsolutePosition(0, 300);
                  PdfGState _state = new PdfGState()
                  {
                      FillOpacity = 0.1F,
                      StrokeOpacity = 0.1F
                  };
                  using (MemoryStream ms = new MemoryStream())
                  {
                      using (PdfStamper stamper = new PdfStamper(readerOriginalDoc, ms, '\0', true))
                      {
                          for (int i = 1; i &lt;= n; i++)
                          {
                              PdfContentByte content = stamper.GetOverContent(i);
                              content.SaveState();
                              content.SetGState(_state);
                              content.AddImage(img);
                              content.RestoreState();
                          }
                      }
                      //return ms.ToArray();
                      all = ms.GetBuffer();
                  }
  </code></pre>
  
  <p>This is my new itext 7 code, this also displays the watermark but the position is wrong. I was dismayed to see that you cant add an image to the canvas but you have to add ImageData when the position is being set on the image. The image is also way smaller and back to front.</p>
  
  <pre><code>var imagePath = Server.MapPath(@"~\Content\WaterMarkQuoteOnly.png");
  
                  var tranState = new iText.Kernel.Pdf.Extgstate.PdfExtGState();
                  tranState.SetFillOpacity(0.1f);
                  tranState.SetStrokeOpacity(0.1f);
  
                  ImageData myImageData = ImageDataFactory.Create(imagePath, false);
                  Image img = new Image(myImageData);
                  img.SetFixedPosition(0, 300);                                            
  
                  var reader = new PdfReader(new MemoryStream(all));
                  var doc = new PdfDocument(reader);
                  int pages = doc.GetNumberOfPages();
  
                  using (var ms = new MemoryStream())
                  {
                      var writer = new PdfWriter(ms);
                      var newdoc = new PdfDocument(writer);
  
                      for (int i = 1; i &lt;= pages; i++)
                      {
                          //get existing page
                          PdfPage page = doc.GetPage(i);
                          //copy page to new document
                          newdoc.AddPage(page.CopyTo(newdoc)); ;
                          //get our new page
                          PdfPage newpage = newdoc.GetPage(i);
                          Rectangle pageSize = newpage.GetPageSize();
                          //get canvas based on new page
                          var canvas = new PdfCanvas(newpage);
                          //write image data to new page
                          canvas.SaveState().SetExtGState(tranState);
                          canvas.AddImage(myImageData, pageSize, true);
                          canvas.RestoreState();
                      }
                      newdoc.Close();
                      all = ms.GetBuffer();
                      ms.Flush();
                  }
  </code></pre>