locked
Trying to insert an image into a pdf RRS feed

  • Question

  • I need to insert an image based on a generated barcode file this as an example.  The problem I'm having is when using the iTextSharp library I can normally fill in text such as

    PdfReader pdfReader = new PdfReader(oldFile);
    PdfStamper pdfStamper = new PdfStamper(pdfReader, outFile);
    AcroFields fields = pdfStamper.AcroFields;
    fields.SetField("topmostSubform[0].Page1[0].BARCODE[0]", "X974005-1");

    though there's one field where in pdf if I click onto it it prompts me for an image to insert into field, but I can't seem to programmatically accomplish this.  Based on some google searches and stumbling upon a stackoverflow page, I inserted the following code expecting it to work as desired:

    string fieldName = "topmostSubform[0].Page1[0].BARCODE[0]";
    string imageFile = "test-barcode.jpg";
    AcroFields.FieldPosition fieldPosition = pdfStamper.AcroFields.GetFieldPositions(fieldName)[0];
    PushbuttonField imageField = new PushbuttonField(pdfStamper.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;
    pdfStamper.AcroFields.RemoveField(fieldName);               
    pdfStamper.AddAnnotation(imageField.Field, fieldPosition.page);

    The problem I am having is while it removes the existing field as intended, when I open the newly created PDF file I don't see this new push button field with the intended image file but rather as a blank but when I perform this through debug mode I can see that it's at least picking up the correct dimensions of the image file, so I don't know what I'm doing wrong here.  

    Please advise, thanks.

    Wednesday, March 27, 2013 1:33 PM

Answers

  • Questions related to iTextSharp should be posted on their forums.  These forums are for .NET technologies.
    • Marked as answer by jfalberg Wednesday, March 27, 2013 4:41 PM
    Wednesday, March 27, 2013 2:18 PM

All replies

  • Questions related to iTextSharp should be posted on their forums.  These forums are for .NET technologies.
    • Marked as answer by jfalberg Wednesday, March 27, 2013 4:41 PM
    Wednesday, March 27, 2013 2:18 PM
  • use imageField.Image = Image.FormFile( );
    Wednesday, March 27, 2013 4:37 PM
  • Reposted in stackoverflow and got a quick answer.  Thanks CoolDadTx

    http://stackoverflow.com/questions/15661513/trying-to-insert-an-image-into-a-pdf-in-c-sharp

    Wednesday, March 27, 2013 4:42 PM