Answered by:
Using iText5 to populate image in PDF

Question
-
User1510859543 posted
We have an aspx webforms app that opens an existing PDF document and populates some named text fields with data from a database. It works fine with text values but we want to be able to populate an image into a field but do not know how to do that. A sample of our code is below.
Dim pdfReader As PdfReader = New PdfReader(strSourceFile) Using pdfStamper As PdfStamper = New PdfStamper(pdfReader, New FileStream(newFile, FileMode.Create)) Dim pdfFormFields As AcroFields = pdfStamper.AcroFields pdfFormFields.SetField("DATE", strDate) pdfFormFields.SetField("CUSTOMER NAME", strCustomerFirstLast) pdfStamper.FormFlattening = False End Using pdfReader.Close() pdfReader.Dispose()
Tuesday, July 21, 2020 5:41 PM
Answers
-
User-939850651 posted
Hi dlchase,
According to your description, if you want to fill an image into a field in a PDF file, you could try the following code like this:
Private Shared Sub FillPdfForm() Const pdfTemplate As String = "pdf\form.pdf" Dim newFile = "pdf\FilledCV.PDF" Dim pdfReader = New PdfReader(pdfTemplate) Dim pdfStamper = New PdfStamper(pdfReader, New FileStream(newFile, FileMode.Create)) Dim pdfFormFields = pdfStamper.AcroFields Dim TestImage As String = "pdf\test.jpg" Dim ad As PushbuttonField = pdfFormFields.GetNewPushbuttonFromField("08") ad.Layout = PushbuttonField.LAYOUT_ICON_ONLY ad.ProportionalIcon = True ad.Image = Image.GetInstance(TestImage) pdfFormFields.ReplacePushbuttonField("08", ad.Field) pdfFormFields.SetFieldProperty("01", "textsize", 8F, Nothing) pdfFormFields.SetField("01", "Example") For Each de In pdfReader.AcroFields.Fields pdfFormFields.SetFieldProperty(de.Key, "setfflags", PdfFormField.FF_READ_ONLY, Nothing) Next pdfStamper.FormFlattening = False pdfStamper.Close() End Sub
Hope this can help you.
Best regards,
Xudong Peng
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, July 22, 2020 6:59 AM
All replies
-
User-939850651 posted
Hi dlchase,
According to your description, if you want to fill an image into a field in a PDF file, you could try the following code like this:
Private Shared Sub FillPdfForm() Const pdfTemplate As String = "pdf\form.pdf" Dim newFile = "pdf\FilledCV.PDF" Dim pdfReader = New PdfReader(pdfTemplate) Dim pdfStamper = New PdfStamper(pdfReader, New FileStream(newFile, FileMode.Create)) Dim pdfFormFields = pdfStamper.AcroFields Dim TestImage As String = "pdf\test.jpg" Dim ad As PushbuttonField = pdfFormFields.GetNewPushbuttonFromField("08") ad.Layout = PushbuttonField.LAYOUT_ICON_ONLY ad.ProportionalIcon = True ad.Image = Image.GetInstance(TestImage) pdfFormFields.ReplacePushbuttonField("08", ad.Field) pdfFormFields.SetFieldProperty("01", "textsize", 8F, Nothing) pdfFormFields.SetField("01", "Example") For Each de In pdfReader.AcroFields.Fields pdfFormFields.SetFieldProperty(de.Key, "setfflags", PdfFormField.FF_READ_ONLY, Nothing) Next pdfStamper.FormFlattening = False pdfStamper.Close() End Sub
Hope this can help you.
Best regards,
Xudong Peng
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, July 22, 2020 6:59 AM -
User1510859543 posted
It worked but I took out below 2 lines because I do not know why they were there.
pdfFormFields.SetFieldProperty("01", "textsize", 8F, Nothing)
pdfFormFields.SetField("01", "Example")
p.s. the SetFieldProperty 3rd parameter 8F is invalid.
Wednesday, July 22, 2020 3:41 PM