title: Insert an image into an existing pdf document using iText Sharp
body: <p>I need to insert an image into an existing pdf at a specific location. I tried the answer at <a href="https://stackoverflow.com/questions/583629/how-can-i-insert-an-image-with-itextsharp-in-an-existing-pdf">this</a> question. But whatever different ways I do the image is being inserted at (0,0) position (bottom left corner). I tried another approach where instead of using stream I used Document class in iTextSharp as shown <a href="http://www.c-sharpcorner.com/Blogs/12317/add-image-in-pdf-using-itextsharp.aspx" rel="nofollow noreferrer">here</a>. Now I am able to place the image at the desired position but this method is creating a new document with just this image. Most of the articles I searched are using PdfReader and PdfStamper so I think this is the recommended way. Any help is appreciated. Posting below code for both the methods I tried.</p>
  
  <p>PdfStamper method</p>
  
  <pre><code>   private void AddImage(string filePath)
      {
          string imageURL = @"ImagePath\Image.jpg";
  
          using (Stream inputPdfStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
          using (Stream inputImageStream = new FileStream(imageURL, FileMode.Open, FileAccess.Read))
          using (Stream outputPdfStream = new FileStream(@"ResultingPdfPath\Abcd.pdf", FileMode.Create, FileAccess.ReadWrite))
          {
              Image image = Image.GetInstance(inputImageStream);
              image.ScaleToFit(100, 100);
  
              var reader = new PdfReader(inputPdfStream);
              var stamper = new PdfStamper(reader, outputPdfStream);
  
              PdfContentByte content = stamper.GetUnderContent(1);
              image.SetAbsolutePosition(100f, 150f);
              content.AddImage(image);
  
              stamper.Close();
  
              reader.Close();
          }
      }
  </code></pre>
  
  <p>Document class method</p>
  
  <pre><code>    private void TestMessage(string filePath)
      {
          string imageURL = @"ImagePath\Image.jpg";
  
          Document doc = new Document(PageSize.A4);
  
          PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(filePath, FileMode.Open));
  
          doc.Open();
  
          iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageURL);
          jpg.ScaleToFit(140f, 120f);
          jpg.SetAbsolutePosition(100, 100);
          jpg.SpacingBefore = 10f;
          jpg.SpacingAfter = 1f;
          jpg.Alignment = Element.ALIGN_LEFT;
          doc.Add(jpg);
          doc.Close();
      }
  </code></pre>
  
  <p>Let me know if you need further information.</p>