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 Jan 21, 2019 7:39 pm

I am hoping I can set a PdfRadioButtonListFieldWidget by its display value instead of index. If I need the index, can I use the text value to get the corresponding index?

rcastlespmg
 
Posts: 2
Joined: Mon Jan 21, 2019 7:29 pm

Tue Jan 22, 2019 7:32 am

Hi rcastlespmg,

Thanks for your inquiry.
Please refer to the following code to have a try.
Code: Select all
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"FormField.pdf");
int index = 0;
PdfFormWidget formWidget = doc.Form as PdfFormWidget;
for (int i = 0; i < formWidget.FieldsWidget.List.Count; i++)
{
    PdfField field = formWidget.FieldsWidget.List[i] as PdfField;
    if (field is PdfRadioButtonListFieldWidget)
    {
        PdfRadioButtonListFieldWidget radioButton = field as PdfRadioButtonListFieldWidget;
        if (radioButton.SelectedValue == "Others")
        {       
            index = radioButton.SelectedIndex;
        }
    }
}
doc.SaveToFile("result.pdf");

If this doesn't meet your requirement, please provide more information along with your pdf file.

Sincerely,
Anna
E-iceblue support team
User avatar

Anna.Zhang
 
Posts: 73
Joined: Thu Sep 27, 2018 3:20 am

Wed Jan 23, 2019 2:56 am

Attached is an example PDF I would like to manipulate. In the zip is the source LibreOffice document.

My goal is to set the radio set named "NumDays" here using the display value, for example by "30 days". I have this limitation as the PDF is going to be generated and the user does not know the index.

If I could get the index by the radio label "30 days" that would work as well.

rcastlespmg
 
Posts: 2
Joined: Mon Jan 21, 2019 7:29 pm

Wed Jan 23, 2019 10:56 am

Hi rcastlespmg,

Thanks for your sharing.
After checking your PDF, I found that the values of the Radio button options are "1, 2, 3 ", not " 0 Days, 30 Days, 90 Days ". So it is impossible to set the state of the buttons according to the display value you mentioned. You need to change the corresponding values of the Radio button options. Please refer to below code to achieve your target.
Code: Select all
string[] DisplayValues = { "0Days", "30Days", "90Days" };
String path = "changed.pdf";
PdfDocument doc = new PdfDocument("choice.pdf");
// fill form
var formWidget = doc.Form as PdfFormWidget;
int j = 0;
for (int i = 0; i < formWidget.FieldsWidget.List.Count; i++)
{
    PdfField field = formWidget.FieldsWidget.List[i] as PdfField;
    if (field is PdfRadioButtonListFieldWidget)
    {
        PdfRadioButtonListFieldWidget radioButton = field as PdfRadioButtonListFieldWidget;
        foreach (PdfRadioButtonWidgetItem item in radioButton.WidgetWidgetItems)
        {
            item.Value = DisplayValues[j];
            j++;
        }                                                     
    }
}
doc.SaveToFile(path);

PdfDocument document = new PdfDocument(path);
int index = 0;
var formWidget2 = document.Form as PdfFormWidget;
for (int i = 0; i < formWidget2.FieldsWidget.List.Count; i++)
{
    PdfField field = formWidget2.FieldsWidget.List[i] as PdfField;
    if (field is PdfRadioButtonListFieldWidget)
    {
        PdfRadioButtonListFieldWidget radioButton = field as PdfRadioButtonListFieldWidget;
        foreach (PdfRadioButtonWidgetItem item in radioButton.WidgetWidgetItems)
        {           
            if (item.Value == "30Days")
            {
                item.Checked = true;
                index = radioButton.SelectedIndex;
            }
        }
    }
}
document.SaveToFile("result.pdf");


Sincerely,
Anna
E-iceblue support team
User avatar

Anna.Zhang
 
Posts: 73
Joined: Thu Sep 27, 2018 3:20 am

Tue Jan 29, 2019 9:19 am

Hi,

Greetings from E-iceblue.
Did you test my code? Did it meet your requirement?
Could you please give us some feedback at your convenience? Thanks in advance.

Sincerely,
Anna
E-iceblue support team
User avatar

Anna.Zhang
 
Posts: 73
Joined: Thu Sep 27, 2018 3:20 am

Return to Spire.PDF

cron