title: How can replace image in Exisitng PDF using itextsharp c# Assembely
body: <p>I have a following code for using replace image on PDF,the code runs without error, but the PDF file does not change image. how can resolve it.so i need your help for adding images in pdf file.   </p>
  
  <pre><code> PdfReader pdf = new PdfReader("PDF\\111x117.pdf");
          PdfStamper stp = new PdfStamper(pdf, new FileStream("PDF\\output.pdf", FileMode.Create));
  
  
          PdfWriter writer = stp.Writer;
          for (int pageNumber = 1; pageNumber &lt;= pdf.NumberOfPages; pageNumber++)
          {
  
              iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance("images\\image1.jpg");
              PdfDictionary pg = pdf.GetPageN(pageNumber);
              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 =
                            (PdfName)PdfReader.GetPdfObject(tg.Get(PdfName.SUBTYPE));
                          if (PdfName.IMAGE.Equals(type))
                          {
                              PdfReader.KillIndirect(obj);
                              iTextSharp.text.Image maskImage = img.ImageMask;
                              if (maskImage != null)
                                  writer.AddDirectImageSimple(maskImage);
                              writer.AddDirectImageSimple(img, (PRIndirectReference)obj);
                              break;
                          }
                      }
                  }
              }
          }
          stp.Close();
  </code></pre>