0

It doesnt throw any error but it just wont put the image on pdf form. Can anyone figure out what am i missing here

_pdfDocument = System.IO.Path.GetTempPath() & "TICKET_INFO.pdf"
_pdfDocumentOutput = System.IO.Path.GetTempPath() & "TICKET_INFO_OUTPUT.pdf"
SaveFromResources(_pdfDocument, My.Resources.template)

Using reader As New PdfReader(_pdfDocument)
    Using stamper As New PdfStamper(reader, New IO.FileStream(_pdfDocumentOutput, IO.FileMode.Create))

        Dim pdfForm As AcroFields = stamper.AcroFields

        Dim test As System.Drawing.Image = Image.FromFile("123.jpg")
        Dim logo As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(test, System.Drawing.Imaging.ImageFormat.Png)

        Dim fieldPosition As AcroFields.FieldPosition = stamper.AcroFields.GetFieldPositions("Description")(0)
        Dim imageField As New PushbuttonField(stamper.Writer, fieldPosition.position, "Description")
        imageField.Image = logo

        pdfForm.SetField("DateMade", "123")

        pdfForm.ReplacePushbuttonField("Description", imageField.Field)

        stamper.FormFlattening = True

    End Using
End Using

Field DateMade is shown on output file but the image is not.

Edit: not possible to change tag to itextsharp from mobile app

According to : Stackoverflow - itext

The "official" way to do this, is to have a Button field as placeholder for the image, and to replace the "icon" of the button like this:

Edit: Field type is button and name is button2

enter image description here

Line changed to :

pdfForm.ReplacePushbuttonField("Button2", imageField.Field)

Still not showing the image.

PyDeveloper
  • 309
  • 6
  • 24
  • What kind of field is "Description" originally? Text or button? – mkl Jun 27 '18 at 22:44
  • @Mkl its text field – PyDeveloper Jun 28 '18 at 05:02
  • You can't use the tag iTextSharp because that tag was removed a couple of years ago when we changed the name iTextSharp to iText for .NET. Please stop saying iTextSharp; say iText for .NET instead. – Bruno Lowagie Jun 28 '18 at 05:33
  • @BrunoLowagie Ok. By dll name i thought it was the same. – PyDeveloper Jun 28 '18 at 05:47
  • 1
    If your DLL refers to iTextSharp, you are probably using an old version of iText. The current version is iText 7: https://developers.itextpdf.com/itext7/download-and-install-information/NET You are probably using iText 5, but we only make *maintenance* releases of iText 5 anymore. iText 5 is being phased out. You should no longer use it. – Bruno Lowagie Jun 28 '18 at 05:51
  • @BrunoLowagie Changing the reference dll require a lot of modifications to my code. Is it possible for you to reference me to .net example using itext7 as example above ? Thanks in advance. P.S Most examples i found on internet was on java – PyDeveloper Jun 28 '18 at 06:08
  • 1
    This is the link to the iText 7 jump-start tutorial for .NET: https://developers.itextpdf.com/content/itext-7-jump-start-tutorial-net/itext-7-jump-start-tutorial-net-version (Note: if your field is a text field, it doesn't make sense to "replace the pushbutton field." A text field *is not a* pushbutton! That should be trivial, so it feels kind of awkward that I have to explain this, but apparently you didn't know. – Bruno Lowagie Jun 28 '18 at 06:32
  • @BrunoLowagie Can you please check the edited post. Do i need to set additional property on button ? – PyDeveloper Jun 28 '18 at 07:18
  • I don't have the time to answer. If it's urgent, I suggest that you contact iText Group for commercial support. – Bruno Lowagie Jun 28 '18 at 08:51

1 Answers1

0

The code snipper shown above solved my problem.

Keep in mind that on pdf form you need to add field type button to accomplish this

Dim _pdfDocument = "template.pdf"
        Dim _pdfDocumentOutput = "template_out.pdf"

        Using reader As New PdfReader(_pdfDocument)
            Using stamper As New PdfStamper(reader, New IO.FileStream(_pdfDocumentOutput, IO.FileMode.Create))

                Dim pdfForm As AcroFields = stamper.AcroFields

                Dim Signature As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(Image.FromFile("123.jpeg"),
                                                                                      Imaging.ImageFormat.Png)

                Dim ad As PushbuttonField = stamper.AcroFields.GetNewPushbuttonFromField("Image-1")
                ad.Layout = PushbuttonField.LAYOUT_ICON_ONLY
                ad.ProportionalIcon = True
                ad.Image = Signature
                ad.BackgroundColor = iTextSharp.text.BaseColor.WHITE
                pdfForm.ReplacePushbuttonField("Image-1", ad.Field)
                pdfForm.SetField("Description-1", "DESC !")

                stamper.FormFlattening = True

            End Using
        End Using
PyDeveloper
  • 309
  • 6
  • 24