Combobox Form Field

Hi,


I am using code provided in below link to add ComboBox control in PDF document.


I need to add index for each option added to the ComboBox. Is there any property available for adding indexes as well with the option ?

I have tried setting indexes to the following properties: zindex , AlternateName, Contents

When i am trying to read this field again through the code , i am able to retrieve the text of the option selected but not index.

Could you please look into this and let me know?

Thanks
Nikhil



Hi Nikhil,


Thanks for contacting support.

I need to add index for each option added to the ComboBox. Is there any property available for adding indexes as well with the option ?

By default each option added in the ComboBox has a value. If you add first options its index will be 1 and if you add another option in the comboBox its index will be 2 and so on. We don’t have any property/method to set the index of the option as it is set by default.

You are mixing Zindex with Index. Zindex is different. It gets or sets a int value that indicates the Z-order of the graph. A graph with larger ZIndex will be placed over the graph with smaller ZIndex. ZIndex can be negative. Graph with negative ZIndex will be placed behind the text in the page.

If you still face any issue, please share your sample code.

Best Regards,

Hi Fahad,


Thanks for reverting on this.

Here’s the code i have written:

if (dtCollection != null && dtCollection.Rows.Count > 0)
{
for (int k = 0; k < dtCollection.Rows.Count; k++)
{
combo.AddOption(Convert.ToString(dtCollection.Rows[k][1]));
}
}


dtCollection is a DataTable which has following data:

0 - Unknown
1 - Growth
2 - Core

So if it is not possible to add these indexes along with options, could you please provide any other alternative to this if possible or any other property of ComboBox would be okay too.

Thanks


Hi Nikhil,


Thanks for sharing further details.

Please see following sample code snippet for getting the index of the selected option of the combo box and also it shows how you can get all options of the combo box.

C#
Document pdfDocument = new Document(dataDir + “ComboBox_out.pdf”);

ComboBoxField comboField = pdfDocument.Form[“field_1”] as ComboBoxField;

Console.WriteLine(comboField.Selected);
// list all options of the combo box field
foreach (Option option in comboField.Options)
{
Console.WriteLine(option.Name);
}

If you still face any issue, please feel free to contact us.

Best Regards,

Hi Fahad,


Thanks for sharing this example. I tested this code at my end.

First of all my scenario here is this:

Step1) Generating PDF file with controls and populating data to those controls.
Step2) Reading generated PDF back into system.

Data that i want to bind to ComboBox control is similar to this:

10 - Red
11 - Blue
15 - Green

By now i know that when we bind this data to ComboBox i would have Red at index 1, Blue at 2 and Green at 3…

While i was testing your sample, i noticed that for Name as well as for value property Red, Green and Blue is being returned. Ideally value field should have returned 10,11,15…

Console.WriteLine(option.Name);
Console.WriteLine(option.Value);

Having said this, Could you please tell me which method of ComboBox field i should use in Step1 to generate
ComboBox with this so that i can use values:

10 - Red
11 - Blue
15 - Green

Note: i am using addOption field to create dropdown which has only 2 overloads and both does not allow you to add value and that is the reason i cannot add value

Is there any alternative to this solution other than ComboBox?




HI Nikhil,


Thanks for sharing further details.

I am afraid currently Aspose.Pdf for .NET doesn’t support adding different name and value of a combobox option. When you use AddOption(“Red”) it adds label and value same. However, I have logged an enhancement ticket PDFNET-42348 in our issue tracking system. We will further look into it and will update you within this forum thread as soon as this feature is implemented.

Please check below code snippet to get the index of the options added in the Combo box.

C#

<span style=“background-color: rgb(255, 255, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>Document pdfDocument = <span class=“kwrd” style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>new<span style=“background-color: rgb(255, 255, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”> Document(dataDir + “ComboBox_out.pdf”);
ComboBoxField comboField = pdfDocument.Form[“field_1”] as ComboBoxField;
Console.WriteLine(comboField.Selected);
// list all options of the combo box field
foreach (Option option in comboField.Options)
{
Console.WriteLine(option.Name);
Console.WriteLine(option.Index); // -1 means an empty option
}

If you need further assistance, please feel free to contact us.

We are sorry for the inconvenience.

Best Regards,