-1

I'm working to refactor a PDF form web application that is using the Active PDF Toolkit and the FDFToolkit from Adobe. My goal is to use iTextSharp to:

  1. Pre-populate the form fields with data from the database
  2. Allow the user to attach a signature and/or barcode image via FDF

Item #1 is not the problem. Item #2 is the biggest challenge. Let me provide some background:

This is a web application which renders the PDF form once. After the initial load, there are 2 key buttons on the form which submit the PDF form to a URL with an action parameter in the query string. These buttons are called "Save" and "Sign". The Save button takes the FDF field dictionary and saves it to the database. The Sign button looks up the signature for the logged-in user and attaches the signature image to the FDF and writes the FDF to the HTTP Response.

The FDFToolkit supports attaching an image to a field using this method:

FDFSetAP(string bstrFieldName, short whichFace, string bstrFileName, short pageNum)

iTextSharp does not offer a comparable method in the FdfWriter class. I've considered subclassing the FdfWriter class and adding my own method to attach an image, but wanted to reach out here to see if anyone has had the same problem.

I have been able to overlay an image on top of a field using this method, but this is in the underlying PDF and not the 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);

Thanks!

4

1 回答 1

1

我通过使用 PdfStamper 和制作按钮字段将图像放在表单上。您可以将现有字段替换为 Pushbutton 字段并将 Pushbutton 设置为 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);
}
于 2013-03-20T00:04:10.737 回答