Posted 25 August 2020, 8:17 am EST
Here is the code below. Ignore the “_formService.SendDpValuesToPdf” part in the code, this is what pass in value from the database to “fieldResult”.
“chkField.Value” is the CheckBoxField we use for checking/unchecking the checkbox on the PDF document.
The issue we’re having is there are 3 checkboxes with same fieldName (in the code is “var fieldName = widgetAnnotation.Field.Name”). If I check the 3rd checkbox, all 3 checkbox got checked & there is no way to leave the first 2 checkboxes unchecked.
private void CreatePdfTemplate(GcPdfDocument pdfDocument, FormWriter formWriter) { var pdfFilename = $"{Path.GetFileNameWithoutExtension(formWriter.Forms.Description)}.pdf"; formWriter.PdfTemplateLocation = pdfFilename; formWriter.Forms.Description = $"{formWriter.Company.AcctNo}/PDF/{pdfFilename}"; var pdfFileStream = _amazonManagerService.GetPdfTemplate(formWriter.Forms.Description); var fullFilePath = @"X:\zz\Testcase.pdf"; using (var fileStream = new FileStream(fullFilePath, FileMode.Open, FileAccess.Read)) { fileStream.CopyTo(pdfFileStream); } pdfDocument.Load(pdfFileStream); var pdfFieldNames = pdfDocument.AcroForm.Fields.Select(v => v.Name).ToList(); 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; CheckBoxField chkField; 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 ((chkField = widgetAnnotation.Field as CheckBoxField) != null) { chkField.Value = EvaluatePdfCheckboxField(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); } private bool EvaluatePdfCheckboxField(string value) { if (string.IsNullOrEmpty(value)) return false; value = value.ToLower(); if (value == "yes") return true; else if (value == "1" || value == "2" || value == "3") { return true; } return false; }