pdfDocument.Load(pdfFileStream);
var pdfFieldNames = pdfDocument.AcroForm.Fields.Select(v => v.Name).ToList();
var checkboxList = new Dictionary<string, int>();
var radioList = new Dictionary<string, int>();
// Previously, document.AcroForm.Fields was being used to retrieve PDF fields. However, this caused issues
// with multiple fields having the same name (e.g. "P Req Signature"), in that all but one of those fields
// would be ignored. Looking, instead, at each page's annotations seems to be the solution.
foreach (var page in pdfDocument.AcroForm.Doc.Pages)
{
foreach (var annotation in page.Annotations)
{
if (annotation.GetType() == typeof(WidgetAnnotation))
{
var widgetAnnotation = annotation as WidgetAnnotation;
var fieldName = widgetAnnotation.Field.Name;
TextField txtField;
CheckBoxField chkField;
RadioButtonField rdbField;
bool isString = true; // Do we really need this?
widgetAnnotation.Border.Style = BorderStyle.None;
if (pdfFieldNames.Contains(fieldName))
{
var fieldResult = _formService.SendDpValuesToPdf(fieldName, formWriter, ref isString);
if (fieldResult.Value != null)
{
if ((txtField = widgetAnnotation.Field as TextField) != null)
{
//if (fieldValue.StartsWith(_signaturePrefix) && fieldValue.EndsWith(_signatureSuffix))
if (fieldResult.Value.StartsWith(@"\"))
{
// This is for DocuSign.
// The text layout used to render input fields when flattening the document:
var _inputTl = new gcText::TextLayout();
_inputTl.ParagraphAlignment = gcText::ParagraphAlignment.Center;
// The text format used for input fields:
var _inputTf = new gcText::TextFormat();
_inputTf.Font = StandardFonts.Courier; // Isn't it suppose to be Helvetica instead of "Courier New"?
_inputTf.ForeColor = dotnetDrawing::Color.Transparent;
_inputTf.FontSize = 4;
_inputTf.FontBold = false;
_inputTl.Clear();
_inputTl.Append(fieldResult.Value, _inputTf);
_inputTl.MaxHeight = widgetAnnotation.Rect.Height;
_inputTl.PerformLayout(true);
widgetAnnotation.Page.Graphics.DrawTextLayout(_inputTl, widgetAnnotation.Rect.Location);
}
else
{
//widgetAnnotation.Field.Value = fieldValue;
txtField.Value = fieldResult.Value;
txtField.RichText = false;
}
}
else if ((chkField = widgetAnnotation.Field as CheckBoxField) != null)
{
// TODO - Change checkbox style from "check" mark to "cross" mark.
// - https://www.grapecity.com/documents-api-pdf/docs/readme/version3.1.0.508.html
// (Only version 3.1.0.508 & up support this).
// (Added WidgetAnnotation.CheckStyle property. It specifies the style of check mark used if the WidgetAnnotation is linked to CheckBoxField or RadioButtonField).
// - https://www.grapecity.com/documents-api-pdf/docs/online/GrapeCity.Documents.Pdf~GrapeCity.Documents.Pdf.Annotations.CheckStyle.html
// -
//chkField.???? = CheckStyle.Cross;
chkField.Value = EvaluatePdfCheckboxField(chkField, checkboxList, fieldResult.Value);
}
else if ((rdbField = widgetAnnotation.Field as RadioButtonField) != null)
{
rdbField.Value = EvaluatePdfRadiobuttonField(rdbField, radioList, fieldResult.Value);
}
}
}
}
}
}
//var foo1 = ((CheckBoxField)((WidgetAnnotation)pdfDocument.AcroForm.Doc.Pages[0].Annotations[12]).Field).Value;
//var foo2 = ((CheckBoxField)((WidgetAnnotation)pdfDocument.AcroForm.Doc.Pages[0].Annotations[13]).Field).Value;
//var foo3 = ((CheckBoxField)((WidgetAnnotation)pdfDocument.AcroForm.Doc.Pages[0].Annotations[14]).Field).Value;
SetPdfFieldReadOnly(pdfDocument.AcroForm.Fields);