/// <summary> /// Sets the fields in the pdf stamper with the given object data. /// Based on the framework, it's easier to use an anonymous-object to fill the pdf. /// </summary> /// <param name="pdfStamper">The pdf stamper to use.</param> /// <param name="data">The data to set.</param> public static void SetFields(this PdfStamper pdfStamper, object data) { if (pdfStamper.IsXfaCompliant()) { pdfStamper.AcroFields.Xfa.FillXfaForm(data.ToXfaCompliantXml()); } else { foreach (var fieldName in pdfStamper.GetFieldNames()) { var valToSet = data.GetPropertyValue <object>(fieldName); var isImageData = valToSet.IsNotNull() && valToSet.GetType() == typeof(byte[]); if (isImageData && pdfStamper.GetFieldPosition(fieldName, out var fieldPos)) { var image = Image.GetInstance((byte[])valToSet); pdfStamper.GetOverContent(fieldPos.page).AddImage(image.SetImageMetrics(fieldPos)); } else { pdfStamper.AcroFields.SetField(fieldName, valToSet.ToStringSafe()); } } } }
/// <summary> /// Sets each field in the pdfStamper to read only. /// </summary> /// <param name="pdfStamper">The pdf stamper to use.</param> public static void SetReadOnly(this PdfStamper pdfStamper) { if (pdfStamper.IsXfaCompliant()) { pdfStamper.AcroFields.Xfa.DomDocument.GetElementsByTagName("field") .Cast <XmlElement>() .ToList() .ForEach(element => element.SetAttribute("access", "readOnly")); } else { pdfStamper.FormFlattening = true; } }