PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

PdfRadioButtonField.cs bug in IndexInOptStrings(v)
https://forum.pdfsharp.net/viewtopic.php?f=3&t=647
Page 1 of 1

Author:  sorcerer7 [ Thu Feb 19, 2009 7:10 am ]
Post subject:  PdfRadioButtonField.cs bug in IndexInOptStrings(v)

Sometimes the value coming in to the method IndexInOptStrings() in PdfRadioButtonField.cs has the slash while other times it does not. This is probably indicative of an issue somewhere else.

However, to get this method to return the correct index of the SelectedIndex for the current RadioButton, I made the following changes.

Code:
    int IndexInOptStrings(string value)
    {
      PdfArray opt = Elements[Keys.Opt] as PdfArray;
      String tmpStr = String.Empty;

      // Sometimes the value comes in with a slash and other times it does not.
      // If the value doesn't start with a slash, add it automatically.
      if (value.Length == 0 || value[0] != '/')
          value = "/" + value;

      if (opt != null)
      {
        int count = opt.Elements.Count;
        for (int idx = 0; idx < count; idx++)
        {
          PdfItem item = opt.Elements[idx];
          if (item is PdfString)
          {
              // item.ToString() never starts with a slash but to  be
              // sure it will be found, add a slash to compare to 'value'
              tmpStr = "/" + item.ToString();
            if (tmpStr == value)
              return idx;
          }
        }
      }
      return -1;
    }


Any ideas what issues if any is this going to create for me anywhere else?


The old method looked like:

Code:
      int IndexInOptStrings(string value)
      {
          PdfArray opt = Elements[Keys.Opt] as PdfArray;
          if (opt != null)
          {
              int count = opt.Elements.Count;
              for (int idx = 0; idx < count; idx++)
              {
                  PdfItem item = opt.Elements[idx];
                  if (item is PdfString)
                  {
                      if (item.ToString() == value)
                          return idx;
                  }
              }
          }
          return -1;
      }


Author:  sorcerer7 [ Fri Feb 20, 2009 4:27 pm ]
Post subject:  Intial value of Key /V is index value

On the AcroFroms I am using with saved default values, the initial value of the /V key on the Opt element of a RadioButton is the index of the value selected. The following line of code from line 61 of PdfRadioButtonFiled initially returns the slashed index value of the set item value from the opt.Elements array.

Code:
string value = Elements.GetString(Keys.V);


Therefore when IndexInOptStrings(value) - (see code in original post) is called on line 62, it is actually passing the value in that it should be returning. However, the IndexInOptStrings() method iterates through the array of opt.Elements which are the the actual values (not the indexes). Consequently, It does not find a match unless the opt.Elements array happens to include integer values and one of them is the same as the index value passed in.

Note: if the values of radio button options are not set, they appear to default to numbers starting with 1. Therefore in this case, the index returned is off by one as long as the value being sought is greater then 0 and within the array of opt.Elements. (see the example after the +++'s below)

In the PDF definition, has the value saved in the /V key ever been the set value associated with the selected option button or has it always been the index value of that set value?

As an example, my radio buttons have three date choices: June 26, July 10 and July 17. The opt.Elemnts array looks like the following:

0 "June 26"
1 "July 10"
2 "July 17" <= this is the defaulted value.

Upon initial entry into the document being processed by PDFSharp, the key /V value for this radio button group is "/2".

As the code currently is, executing the following line of code returns a -1 because "/2" is not found in the array of item values "June 26", "July 10", "July 17"


The fix is obvious to me but, I do not know if all AcroFroms store the key /V as the index versus the set item value. In my above example the question would be do any AcroForms store "July 17" versus "/2" as the value of key /V?

+++++++++++

If I had not set the values of each of the options in the radio button group, the opt.Elemnts array would look like the following:

0 "1"
1 "2"
2 "3"

In this case, the value at index 1 would have matched the value saved in the /V key however, it would be errouneous as the actual defaulted value should have been the value "3" found at the index value of 2.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/