使用itextsharp将图像附加到fdf

问题描述:

我正在重构使用Active PDF Toolkit和Adobe FDFToolkit的PDF表单Web应用程序。我的目标是使用iTextSharp的到:使用itextsharp将图像附加到fdf

  1. 预填充数据的表单字段从数据库
  2. 允许用户通过FDF

附上签名和/或条码图像项目#1不是问题。项目#2是最大的挑战。让我提供一些背景:

这是一个Web应用程序,它呈现PDF格式一次。初始加载后,表单上有两个按键,它们将PDF表单提交给查询字符串中带有操作参数的URL。这些按钮被称为“保存”和“签名”。保存按钮采用FDF字段字典并将其保存到数据库。 Sign按钮为登录用户查找签名,并将签名图像附加到FDF,并将FDF写入HTTP响应。

的FDFToolkit支持使用该方法的图像附着到字段:

FDFSetAP(串bstrFieldName,短whichFace,串bstrFileName,短页次)

iTextSharp的不提供在FdfWriter类的类似方法。我已经考虑继承FdfWriter类并添加我自己的方法来附加图像,但想要在这里看看是否有人遇到同样的问题。

我已经能够使用此方法在字段顶部覆盖图像,但这是在底层PDF中,而不是FDF。

AcroFields.FieldPosition pos = _Stamper.AcroFields.GetFieldPositions("SIGNATUREFIELD").First(); 
Image signature = Image.GetInstance("Signature.gif"); 
image.SetAbsolutePosition(pos.position.Left, pos.position.Bottom); 
image.ScaleToFit(pos.position.Width, pos.position.Height); 
PdfContentByte pcb = _Stamper.GetOverContent(pos.page); 
pcb.AddImage(image); 

谢谢!

我通过使用PdfStamper和使按钮字段将图像放在窗体上。您可以使用按钮字段替换现有的字段,并将按钮设置为READ_ONLY,以便它不能被按下,并且它看起来像静态图像。这将保持您试图添加的图像作为字段注释,而不是将其添加到页面内容。

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); 
}