iTextSharp 填充 Pdf 表单图像字段
Posted
技术标签:
【中文标题】iTextSharp 填充 Pdf 表单图像字段【英文标题】:iTextSharp Fill Pdf Form Image Field 【发布时间】:2017-06-28 05:59:13 【问题描述】:我使用 Acrobat DC 2015 创建了一个 pdf 表单。我有一个图像字段。我成功填写了文本字段。但我不知道如何填充图像字段。你帮我吗?
private static void FillPdfForm()
// Original File
const string pdfTemplate = @"pdf\form.pdf";
// New file which will be created after fillin PDF
var newFile = @"pdf\FilledCV.PDF";
var pdfReader = new PdfReader(pdfTemplate);
var pdfStamper = new PdfStamper(pdfReader, new FileStream(
newFile, FileMode.Create));
var pdfFormFields = pdfStamper.AcroFields;
// So one of our fields in PDF is FullName I am filling it with my full name
pdfFormFields.SetFieldProperty("01", "textsize", 8f, null);
pdfFormFields.SetField("01", "Example");
// flatten the form to remove editting options, set it to false
// to leave the form open to subsequent manual edits
foreach (var de in pdfReader.AcroFields.Fields)
pdfFormFields.SetFieldProperty(de.Key.ToString(),
"setfflags",
PdfFormField.FF_READ_ONLY,
null);
pdfStamper.FormFlattening = false;
pdfStamper.FormFlattening = false;
pdfStamper.Close();
【问题讨论】:
【参考方案1】:我修好了。
private static void FillPdfForm()
const string pdfTemplate = @"pdf\form.pdf";
var newFile = @"pdf\FilledCV.PDF";
var pdfReader = new PdfReader(pdfTemplate);
var pdfStamper = new PdfStamper(pdfReader, new FileStream(newFile, FileMode.Create));
var pdfFormFields = pdfStamper.AcroFields;
string TestImage = @"pdf\test.jpg";
PushbuttonField ad = 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, null);
pdfFormFields.SetField("01", "Example");
foreach (var de in pdfReader.AcroFields.Fields)
pdfFormFields.SetFieldProperty(de.Key,"setfflags",PdfFormField.FF_READ_ONLY,null);
pdfStamper.FormFlattening = false;
pdfStamper.Close();
【讨论】:
以上是关于iTextSharp 填充 Pdf 表单图像字段的主要内容,如果未能解决你的问题,请参考以下文章