title: Itextsharp 7 - Scaled and Centered Image as watermark
body: <p>I started using itextsharp 7 a few days ago, i used to work with itextsharp 5 for years .</p>
  
  <p>I don't manage to add a scaled image at the center of the page as watermark with itext7.</p>
  
  <p>My code with itextsharp 5 :</p>
  
  <pre><code>using (PdfStamper pdfStamper = new PdfStamper(pdfReader, memoryStream))
  {
      for (int pageIndex = 1; pageIndex &lt;= pdfReader.NumberOfPages; pageIndex++)
      {
          pdfStamper.FormFlattening = false;
  
          iTextSharp.text.Rectangle pageRectangle = pdfReader.GetPageSizeWithRotation(pageIndex);
          PdfContentByte pdfData = pdfStamper.GetOverContent(pageIndex);
  
          PdfGState graphicsState = new PdfGState();
          graphicsState.FillOpacity = 0.4F;
          pdfData.SetGState(graphicsState);
  
          pdfData.BeginText();
  
          Image imageWM = Image.GetInstance(image_WM_Path);
          float width = pageRectangle.Width;
          float height = pageRectangle.Height;
          //scale image
          imageWM.ScaleToFit(width / 3, height / 3);
          //center image
          imageWM.SetAbsolutePosition(width / 2 - imageWM.ScaledWidth / 2, height / 2 - imageWM.ScaledHeight / 2);
  
          pdfData.AddImage(imageWM);
          pdfData.EndText();                       
      }
      pdfStamper.Close();
      return memoryStream.ToArray();
  }
  </code></pre>
  
  <p>Here is with itextsharp 7 (code based on the itext 7 examples):</p>
  
  <pre><code>   PdfDocument pdfDoc = new PdfDocument(new PdfReader(sourceFile), new PdfWriter(destinationPath));
  Document document = new Document(pdfDoc);
  PdfCanvas over;
  PdfExtGState gs1 = new PdfExtGState();
  gs1.SetFillOpacity(0.5f);
  int n = pdfDoc.GetNumberOfPages();
  Rectangle pagesize;
  float x, y;
  
  ImageData img = ImageDataFactory.Create(image_WM_Path);
  float w = img.GetWidth();
  float h = img.GetHeight();
  
  for (int i = 1; i &lt;= n; i++)
  {
      PdfPage pdfPage = pdfDoc.GetPage(i);
      pagesize = pdfDoc.GetPage(i).GetPageSize();
      pdfPage.SetIgnorePageRotationForContent(true);
  
      x = (pagesize.GetLeft() + pagesize.GetRight()) / 2;
      y = (pagesize.GetTop() + pagesize.GetBottom()) / 2;
      over = new PdfCanvas(pdfDoc.GetPage(i));
  
      over.SaveState();
      over.SetExtGState(gs1);
  
      over.AddImage(img, w, 0, 0, h, x - (w / 2), y - (h / 2), true);
  
      over.RestoreState();
  }
  document.Close();
  pdfDoc.Close();
  </code></pre>
  
  <p>The image is centered but i dont manage to scale it with the AddImage method.</p>
  
  <p>Maybe it is easily done but i am struggling with this.</p>
  
  <p>Any help appreciated.</p>