PdfLoadedDocument loadedDocument = new PdfLoadedDocument( pdfStream, true);
PdfLoadedForm loadedForm = loadedDocument.Form;
for (int i = 0; i < loadedForm.Fields.Count; i++)
{
Console.WriteLine(loadedForm.Fields[i].Name + " "+i);
if (loadedForm.Fields[i] is PdfLoadedButtonField)
{
PdfLoadedButtonField loadedField = loadedForm.Fields[i] as PdfLoadedButtonField;
//Create new field and clone its parameters
PdfButtonField field = new PdfButtonField(loadedField.Page, loadedField.Name);
field.Bounds = loadedField.Bounds;
field.BorderColor = loadedField.BorderColor;
field.BorderStyle = loadedField.BorderStyle;
field.BorderWidth = loadedField.BorderWidth;
PdfImage image = new PdfBitmap(imageStream);
//Add image to the appearance stream of the created field
field.Appearance.Normal.Graphics.DrawImage(image, new RectangleF(0,0, field.Appearance.Normal.Width, field.Appearance.Normal.Height) );
// remove the existing field
loadedForm.Fields.Remove(loadedField);
//Add the new field
loadedForm.Fields.Add(field);
}
}
MemoryStream ms = new MemoryStream();
loadedDocument.Save(ms);
loadedDocument.Close(true); |