有几篇与此主题相关的文章,但是我能做的是将图像插入PDF文件而不是PDF表单内的字段。
我可以使用iText Sharp成功地将文本插入PDF表单,
该类如下:
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?
}
}
我进行了很多搜索,发现了一些解决方案,但是它们无法正常工作。他们可能已经过时了吗?例如,我在Stackoverflow上找到了一篇有关类似问题的文章,看来该问题已解决。当我应用相同的更改时,出现错误。
文章链接:iTextSharp Fill Pdf Form Image Field
我的代码(摘自上述文章)
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();
每当我尝试调试代码时,都会出现以下错误: