Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Mon Mar 27, 2017 4:32 pm

Hi,

In SpirePDF version 3.7.0.5040, setting the SelectedIndex property of a RadioButtonList to a value other than -1, saving the file with PdfDocument.SaveToFile, and re-opening the document with PdfDocument.LoadFromFile still shows a SelectedIndex of -1. Strangely, the correct value of SelectedIndex only shows after the PDF file is opened and saved in Acrobat.

Here is the code:

Code: Select all
// Create a PdfDocument and add a page.
PdfDocument pdf = new PdfDocument();
pdf.Pages.Add();

// Create a radio button list with two buttons.
PdfRadioButtonListField radioButtonList = new PdfRadioButtonListField(pdf.Pages[0], "RadioButtonList");
PdfRadioButtonListItem radioButton1 = new PdfRadioButtonListItem("RadioButton1");
PdfRadioButtonListItem radioButton2 = new PdfRadioButtonListItem("RadioButton2");
radioButton1.Bounds = new RectangleF(20f, 20f, 20f, 20f);
radioButton2.Bounds = new RectangleF(20f, 20f, 20f, 20f);
radioButtonList.Items.Add(radioButton1);
radioButtonList.Items.Add(radioButton2);
pdf.Form.Fields.Add(radioButtonList);

// Assign a SelectedIndex of '1' to the radio button list
//   (select the second button).
radioButtonList.SelectedIndex = 1;

// Save the PDF.
pdf.SaveToFile(@"C:\SavedPDF.pdf");
pdf.Dispose();
pdf = null;

// Load the PDF file again (without opening or saving in Acrobat).
pdf = new PdfDocument();
pdf.LoadFromFile(@"C:\SavedPDF.pdf");

// Get the selected index again.
PdfFormWidget formWidget = pdf.Form as PdfFormWidget;
PdfRadioButtonListFieldWidget radioButtonListWidget =
        (PdfRadioButtonListFieldWidget)formWidget.FieldsWidget["RadioButtonList"];
int selectedIndex = radioButtonListWidget.SelectedIndex;   // shows as -1


A workaround is to save the selected value in a hidden field prior to saving with PdfDocument.SaveToFile:

Code: Select all
// Store the initial value of the selected index in a hidden field
//   before saving.
PdfTextBoxField hiddenField = new PdfTextBoxField(pdf.Pages[0], "InitialValue");
hiddenField.Text = radioButtonList.SelectedIndex.ToString();
hiddenField.Visible = false;
hiddenField.ReadOnly = true;
pdf.Form.Fields.Add(hiddenField);


Code: Select all
// Return the selected index, or the hidden field value if the selected index
//   is -1.
PdfFormWidget formWidget = pdf.Form as PdfFormWidget;
PdfRadioButtonListFieldWidget radioButtonListWidget =
        (PdfRadioButtonListFieldWidget)formWidget.FieldsWidget["RadioButtonList"];
PdfTextBoxFieldWidget hiddenFieldWidget = (PdfTextBoxFieldWidget)formWidget.FieldsWidget["InitialValue"];
int selectedIndex = (radioButtonListWidget.SelectedIndex != -1) ?
        radioButtonListWidget.SelectedIndex : int.Parse(hiddenFieldWidget.Text);

Scott7261
 
Posts: 7
Joined: Fri Feb 05, 2016 7:47 pm

Tue Mar 28, 2017 2:49 am

Dear Scott7261,

Thanks for your feedback.
I have noticed the issue SelectedIndex value still is -1, and posted it to our Dev team. We will inform you once there is any progress.
So sorry for inconvenience caused.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Tue Apr 11, 2017 8:17 am

Dear Scott7261,

Thanks for waiting.
Now the issue has been fixed in Spire.PDF Pack(Hot Fix) Version:3.9.27. Welcome to have a test.
Looking forward to your feedback.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Thu Apr 13, 2017 7:16 am

Dear Scott7261,

Did you test the hotfix ?
Has the issue been resolved ?

Thanks,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Fri Apr 28, 2017 8:11 pm

Hi Betsy,

Sorry for the late reply. Yes, I ran the test above and the error appears to be fixed. Thank you for your help!

Scott7261
 
Posts: 7
Joined: Fri Feb 05, 2016 7:47 pm

Mon May 01, 2017 2:43 am

Hello Scott,

Thanks for your feedback.
If there are any other questions, plesae contact us.

Sincerely,
Simon
E-iceblue support team
User avatar

Simon.yang
 
Posts: 620
Joined: Wed Jan 11, 2017 2:03 am

Return to Spire.PDF