How do I place a new image on an existing page.

Q: We are trying to insert an image to a pdfform in a particular
position.

From the sample code (http://www.pdftron.com/net/

samplecode.html#AddImage), I know, we can insert an image to a new
page. But now the problem is, the image needs to be inserted into a
existed page in a particular position. our idea is that we give a
filed in that position, and by coding to insert the image in that
position dynamically. Can you please advise how?
----
A: Adding an image (or other content) to an existing PDF page is
exactly the same as adding an image to a blank new page (e.g. see
http://www.pdftron.com/net/faq.html#how_watermark).

The only difference is how you retrieve the page that you pass in the
call to ElementWriter.Begin(page). In case of the exiting PDF page you
would use pdfdoc.GetPage(num) to fetch the page; in case of a new page
you would use pdfdoc.PageCreate() and pdfdoc.PageInsert() to create
and position the page.

Unless you really need to deal with AcroForms it is probably simpler
to place the image directly (without any relevance to forms).

Q: Thank you very much for your reply! We are using Pdf form. So, how
i can insert in image in a particular place (defined by a field) in a
page?
------
A: In that case you would need to create a custom appearance stream
for a form field and associate it with the widget annotation using
annot.SetAppearance(). For example:

static Obj CreateMyAppearance(PDFDoc doc) {
  ElementBuilder build = new ElementBuilder();
  ElementWriter writer = new ElementWriter();
  writer.Begin(doc);

  Image img = Image.Create(doc, "myimage.jpg");
  double w=img.GetImageWidth(), h=img.GetImageHeight();
  Element img_element = build.CreateImage(img, 0, 0, w, h);
  writer.WritePlacedElement(img_element);
  Obj stm = writer.End();

  // Set the bounding box
  stm.PutRect("BBox", 0, 0, w, h);
  stm.PutName("Subtype", "Form");

  build.Dispose();
  writer.Dispose()
  return stm;
}

Elsewhere in the code
....
Field fld = pdfdoc.GetField("ImageField");
Annot image_form = new Annot(fld.GetSDFObj());
image_form.SetAppearance(CreateMyAppearance(pdfdoc));
...

Please keep in mind that because you are setting custom appearances
you should not call pdfdoc. RefreshFieldAppearances() - since this may
overwrite your custom appearance. You can still use
filed.RefreshAppearance() on individual form fields.

You may also want to search this forum for keyowrds such as "image
form field". You will find several relevant entries.