0

I am using iTextSharp for set values in PDF. All text is working perfectly but the image is now showing in PDF. My code is as below :

string pdfTemplate = string.Empty;
pdfTemplate = Server.MapPath("~/Documents/PDF/ESignSummary.pdf");
PdfReader _reader = new PdfReader(pdfTemplate);
using (MemoryStream ms = new MemoryStream())
{
   PdfStamper stamper = new PdfStamper(_reader, ms);
   using (stamper)
   {
     Dictionary<string, string> info = _reader.Info;
     docServices.docSummaryFields(uniqueId, stamper.AcroFields, imagepath, stamper);
   }
}


public void docSummaryFields(long uniqueId, dynamic _accrofield, string imagepath, PdfStamper stamper)
{
   _accrofield.SetField("Image21", imagepath);
}

I have set the Image as Image21. I have search for similar thing on google but is not working in my case. Can you guide me where I'm making mistake?

Harry R
  • 69
  • 1
  • 11

1 Answers1

1

There are no AcroForm form fields for image values, so iText does not support setting fields to image values because that is impossible.

Apparently there once had been a necessity to support something like image fields nonetheless, so Adobe established a work-around: They introduced JavaScript methods that allow to change the appearance (not the value) of buttons interactively and called those methods in push event listeners of special button fields they showed as "image fields" in their PDF form design software.

Thus, to "set" the image of such an "image field", you actually have to set the image as appearance of a button field.

So if the string targetButton contains the name of an "image field" (i.e. a button field) and you loaded the bitmap into the iTextSharp.text.Image image, you can "set" it like this in a PdfStamper stamper:

PushbuttonField button = stamper.AcroFields.GetNewPushbuttonFromField(targetButton);
button.Image = image;
stamper.AcroFields.ReplacePushbuttonField(targetButton, button.Field);

If you eventually have to "get" the image value from such an "image field", you consequentially have to retrieve the image from the appearance of that field. read this answer for details.


Beware! All this refers to AcroForm forms (which according to your question title you have). If you later will have to deal with a PDF with a XFA form (yes, there still are organizations, in particular government ones, which have not realized yet that XFA forms in PDF have been deprecated since 2017), the situation is different, XFA forms do know actual image fields.

mkl
  • 90,588
  • 15
  • 125
  • 265