Multiple Checkboxes having same name,

Posted by: fletchsod.developer on 25 August 2020, 8:17 am EST

  • 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;
    }
  • Replied 25 August 2020, 8:21 am EST

    P.S. - The checkbox field name is "Vehicle_Warranty"
  • Replied 25 August 2020, 6:53 pm EST

    Hello Scott,

    To further investigate this issue we need the following information:

    1: Please share the PDF file with which you are having the issue.
    2: How you are creating the PDF file.
    3: Are you facing the issue when changing the checkboxes values at via code only?

    Regards,
    Prabhat Sharma.
  • Replied 26 August 2020, 12:36 am EST

    Attached is the Buyers Guide PDFBuyersGuide.zip
  • Replied 26 August 2020, 12:47 am EST

    1. See the attached file in previous post

    2. We created PDF using Adobe PDF Professional software

    3. We're facing the issue when assigning "true" to the "Value" property of the CheckBoxField object. It seem it can't differ between 3 seperate checkbox w/ same name "Vehicle_Warranty".
  • Replied 10 September 2020, 7:50 pm EST

    Hello,

    We have escalated this to the developers and will let you know once we get any update on this from their side.
    [Internal Tracking ID: DOC-2289]

    Regards,
    Prabhat Sharma.
  • Replied 21 October 2020, 3:22 am EST

    Hi,

    This has been fixed in build 1.2.94 of GcPdfViewer, which should be publlshed on npmjs in a few days.

    Thanks,
    Dmitry.
  • Marked as Answer

    Replied 16 November 2020, 1:53 pm EST

    Hello,

    As per the developers: The "Vehicle_Warranty" field is a checkbox with two widgets, widgets use the different key in appearance streams to define the "On" state.
    First "AS IS - NO DEALER WARRANTY" use "1"
    Second "DEALER WARRANTY" use "2"

    You can use the following code to handle such a situation:


    foreach (var annotation in doc.AcroForm.Doc.Pages[0].Annotations)
    {
    if (annotation.GetType() == typeof(WidgetAnnotation))
    {
    var widgetAnnotation = annotation as WidgetAnnotation;
    var fieldName = widgetAnnotation.Field.Name;
    if (fieldName == "Vehicle_Warranty")
    {
    var fld = doc.AcroForm.Fields.FindByName("Vehicle_Warranty");
    string onState;
    // check first widget
    onState = fld.Widgets[0].AppearanceStreams.Normal.GetOnState();
    fld.Widgets[0].AppearanceState = onState;
    // check second widget
    // onState = fld.Widgets[1].AppearanceStreams.Normal.GetOnState();
    // fld.Widgets[1].AppearanceState = onState;
    break; }
    }
    }


    Regards,
    Prabhat Sharma.
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels