There are several articles related to this topic, but what I was able to do is inserting an image into a PDF file not a field inside a PDF form.
I am successfully able to insert text into a PDF form using iText Sharp,
The class is as the following:
public class ProcessForm__EmpApp_En
{
public static void ProcessForm(SMDEntities _DB, WebApplicationFirstPhase EL)
{
string __EA_Form_Template = HostingEnvironment.MapPath("~/Content/BaseFiles/OriginalFormTemplate.pdf");
string newEAForm = HostingEnvironment.MapPath("~/Content/CandidateForms/EA_" + EL.CandidateKey + ".pdf");
PdfReader pdfReader = new PdfReader(__EA_Form_Template);
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(newEAForm, FileMode.Create));
AcroFields pdfFormFields = pdfStamper.AcroFields;
// set form pdfFormFields
pdfFormFields.SetField("Txt_DC_01", EL.ApplicationDistributionCenter);
pdfFormFields.SetField("Txt_Edu04_Start_Date", EL.Education4StartDate);
pdfFormFields.SetField("Txt_Edu04_End_Date", EL.Education4EndDate);
pdfFormFields.SetField("Txt_Edu04_Address", EL.Education4Address);
//I have an Image field called "ImageLogo" and I don't know how to insert my logo in it?
}
}
I searched a lot and I found some solution but they are not working. Probably they are outdated? For example, I found an article on Stackoverflow about similar issue and it look like the issue was solved. When I applied the same changes, I got an error.
The article link: iTextSharp Fill Pdf Form Image Field
My code (taken from the above article):
string pdfTemplate = HostingEnvironment.MapPath("~/Content/CandidateForms/OriginalFormTemplate.pdf");
var newFile = HostingEnvironment.MapPath("~/Content/CandidateForms/EA_" + EL.CandidateKey + ".pdf");
var pdfReader = new PdfReader(pdfTemplate);
var pdfStamper = new PdfStamper(pdfReader, new FileStream(newFile, FileMode.Create));
var pdfFormFields = pdfStamper.AcroFields;
string TestImage = HostingEnvironment.MapPath("~/Content/CandidateImages/TestLogo.JPG");
PushbuttonField ad = pdfFormFields.GetNewPushbuttonFromField("ImageLogo");
ad.Layout = PushbuttonField.LAYOUT_ICON_ONLY;
ad.ProportionalIcon = true;
ad.Image = Image.GetInstance(TestImage);
pdfFormFields.ReplacePushbuttonField("ImageLogo", ad.Field);
pdfStamper.Close();
Whenever I try to debug the code, I get the following error: