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()
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
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
353 Points
1549 Posts
Using iText5 to populate image in PDF
Jul 21, 2020 05:41 PM|dlchase|LINK
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.
Contributor
2080 Points
664 Posts
Re: Using iText5 to populate image in PDF
Jul 22, 2020 06:59 AM|XuDong Peng|LINK
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:
Hope this can help you.
Best regards,
Xudong Peng
Member
353 Points
1549 Posts
Re: Using iText5 to populate image in PDF
Jul 22, 2020 03:41 PM|dlchase|LINK
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.