如何在现有pdf文件中将图像设置为pdf字段?
我正在使用iTextSharp对象.
设置文本字段工作正常.没问题.
pdfFormFields.SetField("Firstname","Mujeeb");
请帮忙.
解决方法
删除“文本”字段,并将其替换为相同大小和位置的“按钮”字段.如果将按钮设置为READ_ONLY,则无法按下它,它看起来像静态图像.这会将您尝试添加的图像保留为字段注释,而不是将其添加到页面内容中.
void ConvertTextFieldToImage(string inputFile,string fieldName,string imageFile,string outputFile) { using (PdfStamper stamper = new PdfStamper(new PdfReader(inputFile),File.Create(outputFile))) { AcroFields.FieldPosition fieldPosition = stamper.AcroFields.GetFieldPositions(fieldName)[0]; PushbuttonField imageField = new PushbuttonField(stamper.Writer,fieldPosition.position,fieldName); imageField.Layout = PushbuttonField.LAYOUT_ICON_ONLY; imageField.Image = iTextSharp.text.Image.GetInstance(imageFile); imageField.ScaleIcon = PushbuttonField.SCALE_ICON_ALWAYS; imageField.ProportionalIcon = false; imageField.Options = BaseField.READ_ONLY; stamper.AcroFields.RemoveField(fieldName); stamper.AddAnnotation(imageField.Field,fieldPosition.page); stamper.Close(); } }