PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 9:52 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Thu Feb 19, 2009 7:10 am 
Offline

Joined: Fri Feb 06, 2009 10:17 pm
Posts: 6
Location: Plano Texas
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;
      }



Top
 Profile  
Reply with quote  
PostPosted: Fri Feb 20, 2009 4:27 pm 
Offline

Joined: Fri Feb 06, 2009 10:17 pm
Posts: 6
Location: Plano Texas
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.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 59 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group