2

I have a PDF file with XFA forms, that I can successfully populate through a dynamically generated XML file.

Now I am trying to insert an image (a hand-made JPG signature file), for which I tried multiple ways, with "partial" luck.

I tried this: How can i set an image to a pdf field in existing pdf file?

And this: How can I insert an image with iTextSharp in an existing PDF?

I meant "partial" luck because the image does show in Foxit Reader, but doesn't show in Acrobat Pro.

Any help will be much, much appreciated.

EDIT:

This is the code I'm using to replace a button field with an image.

private void InsertSignatureIntoBOL(string inputFile, string fieldName, byte[] imageFile, string outputFile)
{
    using (PdfStamper stamper = new PdfStamper(new PdfReader(inputFile), File.Create(outputFile)))
    {
        AcroFields.FieldPosition fieldPosition = stamper.AcroFields.GetFieldPositions(fieldName)[0];

        PushbuttonField imageField = new PushbuttonField(stamper.Writer, fieldPosition.position, fieldName);
        imageField.Layout = PushbuttonField.LAYOUT_ICON_ONLY;
        imageField.Image = iTextSharp.text.Image.GetInstance(imageFile);
        imageField.ScaleIcon = PushbuttonField.SCALE_ICON_ALWAYS;
        imageField.ProportionalIcon = false;
        imageField.Options = BaseField.READ_ONLY;

        stamper.AcroFields.RemoveField(fieldName);
        stamper.AddAnnotation(imageField.Field, fieldPosition.page);

        stamper.Close();
    }
}

I also tried this code to add an image to an absolute position"

    var pdfContentByte = stamper.GetOverContent(1);
    iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(Convert.FromBase64String(SignatureHiddenField.Value));
    image.SetAbsolutePosition(100, 100);
    pdfContentByte.AddImage(image, false);

The only way I can make the images show up in Acrobat Pro is by flattening the form, but I also fill XFA fields in the same form and, when flattened, the XFA fields are shown as empty. As I was mentioning, it works wonderfully in Foxit Phantom, but my main interest is with Acrobat Pro.

Any help would be much, much appreciated.

Community
  • 1
  • 1
  • If your document is a pure xfa document, changes of the pdf page content should not be visible when the PDF is shown. The answers you refer to at least partially work with the PDF page content. This, it is not surprising their results aren't shown by acrobat. To be sure, though, please show the relevant code and a sample PDF. – mkl Jun 01 '13 at 19:13
  • Added some code. Any help would be much, much appreciated. Thank you. –  Jun 03 '13 at 16:39
  • Well, the code adding to the over content definitively adds to the regular page content which is ignored in case of xfa forms. I'm not so sure about the annotations... I'm afraid, though, you'll have to change the xfa XML in the PDF for your requirements. – mkl Jun 03 '13 at 20:23
  • I appreciate your help. I ended up modifying the XFA XML as you suggested. Much appreciated! –  Jun 14 '13 at 18:03

1 Answers1

0

I ended up modifying the XDP file (in Adobe LiveCycle) in order to add an image field. Then, I populated that field with the Base64 encoded string representing the image. Many thanks.