Rahul Sharma
Rahul Sharma

Reputation: 9

PDF Checkbox Error : Value cannot be null. Parameter name: value

I am trying to check a checkbox in PDFform using PDFsharp. I am using below code

PdfCheckBoxField chkbox = (PdfCheckBoxField)(pdf.AcroForm.Fields["chkbox"]);
chk.ReadOnly = false;                   
chk.Checked = true;
chk.ReadOnly = true;

I am getting below error on line chk.Checked = true;

ArgumentNullException was unhandled Value cannot be null. Parameter name: value

Upvotes: 0

Views: 1887

Answers (4)

Ruan Kruger
Ruan Kruger

Reputation: 86

Anyone still having this issue, this is probably the solution you're looking for. After spending over 4 hours on a single checkbox, this is all that worked. Please read the comments in the code snippet for extra clarification.

// I'm assuming you already have all the fields in the form
string fqName = "the_fields_name";
PdfAcroField field = fields[fqName];
PdfCheckBoxField cbField;
if ( (cbField = field as PdfCheckBoxField) != null ) { 
    var chcked = bool.Parse(fieldValues[fqName]); // Whatever logic you have to set the checked/unchecked value goes here
    cbField.ReadOnly = false;
    cbField.Elements.SetName("/V", chcked ? cbField.Name : cbField.UncheckedName);
    cbField.Elements.SetName("/AS", chcked ? cbField.Name : cbField.UncheckedName); // This is what the setter does. I use cbField.Name as in the pdf that's what the checkbox sets when checked(As seen in image). Your PDF you could be able to use cbField.CheckedValue
    // cbField.Checked = chcked; // This doesn't work because in Checked's setter the GetNonOffValue() function returns null; 
    cbField.ReadOnly = true;
}

enter image description here

Upvotes: 0

Martin Novotný
Martin Novotný

Reputation: 1

I encountered a similar problem. The error was in the created form checkbox field. I had different value set as export value. Use default value.

enter image description here

Upvotes: -1

Ben
Ben

Reputation: 891

This little gem came from looking at the PDFSharp source code. This is how to set the value for checkboxes with the same name. I can't tell exactly if this was the original problem posted, but based on the error, and my own frustration, I came up with this solution.

        //how to handle checking multiple checkboxes with same name
        var ck = form.Fields["chkbox"];
        if (ck.HasKids)
        {
            foreach (var item in ck.Fields.Elements.Items) {
                //assumes you want to "check" the checkbox.  Use "/Off" if you want to uncheck.
                //"/Yes" is defined in your pdf document as the checked value.  May vary depending on original pdf creator.
                ((PdfDictionary)(((PdfReference)(item)).Value)).Elements.SetName(PdfAcroField.Keys.V, "/Yes");
                ((PdfDictionary)(((PdfReference)(item)).Value)).Elements.SetName(PdfAnnotation.Keys.AS, "/Yes");
            }
        }
        else {
            ((PdfCheckBoxField)(form.Fields["chkbox"])).Checked = true;
        }

Upvotes: 2

Karl Gjertsen
Karl Gjertsen

Reputation: 4918

You are reading the object into 'chkbox', but setting 'chk':

PdfCheckBoxField chkbox = (PdfCheckBoxField)(pdf.AcroForm.Fields["chkbox"]);
chkbox.ReadOnly = false;                   
chkbox.Checked = true;
chkbox.ReadOnly = true;

I'm not sure why it isn't failing on the first line.

Upvotes: 2

Related Questions