2

In code below for iTextSharp in C#, will a null value for field value be acceptable i.e. can the second parameter be null or it must be a non-null string? I am setting the values for various acrofields in this code. I could not find any suitable documentation on this method.

   foreach (var kvp in fieldsValuesCollection)
     {
       acroFields.SetField(kvp.Key, kvp.Value);
     }

UPDATE 1:

I finally found the answer and it is that the second parameter to 'SetField' method cannot be null ( must be a non-null string). I ran some sample code downloaded from this url: https://web.archive.org/web/20211020001747/https://www.4guysfromrolla.com/articles/030211-1.aspx and ran it locally on my machine, which resulted in an exception shown below. The only change I made to the downloaded code was to set all text type acrofields to null instead of a non-null string and it threw an error.

So developers should be careful when setting acrofield value else they could end up with a hard-to-find bug since nowhere in the API docs for iTextSharp does it mention this fact.

enter image description here

The code I used to test is as below.

  foreach (var fieldName in formFieldMap.Keys)
    {
        if (formFields.GetFieldType(fieldName) == 4)
        {
            formFields.SetField(fieldName, null);
        }
        else
        {
            formFields.SetField(fieldName, formFieldMap[fieldName]);
        }
    }
Sunil
  • 20,653
  • 28
  • 112
  • 197

0 Answers0