I’m having a little trouble figuring out how to dynamically add items to a ComboBox on a PDF. I’ve tried to use the code I found [here](http://www.aspose.com/Documentation/file-format-components/aspose.pdf.kit-for-.net-and-java/aspose.pdf.kit.formeditor.html). Basically, I’m just running the following code:
Dim MyEditor As New FormEditor(“Source.pdf”, “Output.pdf”)
MyEditor.Items = New String() {“item0”, “item1”, “item2”, “item3”}
The ComboBox is added, but the items are not. I’m not sure what the problem is as this seems to follow the example code. I do find it strange that the ComboBox field does not need to be linked up with the items in some way. Does the FormEditor just assume the most recent Items added correspond to the ComboBox?? Is there something wrong with this code? If so, could someone provide an example of working code that creates a ComboBox and adds items to it?
Also, is it possible to add items to an existing ComboBox, e.g. we have PDFs which have ComboBoxes defined on them, but they are empty. Our application populates the values in the ComboBox dynamically. Can we do this with Aspose?
The good news is that the function of adding items to ComboBox has just been enhanced&simlified in a new version which will be published soon (may be next week).
It is a little complex to do that with the previous versions of Aspose.Pdf.Kit, I would like to show a sample(for previous version):
Dim MyEditor As New FormEditor("Source.pdf", "Output.pdf")
Dim Facade As New FormFieldFacade()
Facade .Items = New String() {"item0", "item1", "item2", "item3"}
Thank you. The sample you provided for adding a new combo works. It seems the Facade object is missing in the example in the documentation.
The AddListItem method works for adding an item to a combo which already has items, but it throws an error if the combo is empty. The error seems to vary between:
System.NullReferenceException: Object reference not set to an instance of an object. at Aspose.Pdf.Kit.FormEditor.AddListItem(String fieldName, String itemName)
and:
System.RuntimeException: List box lack of selections. at xeb116a323308e2f7.xc19484cc31391ae1.xc9985364a680ce5e(String x66ac3558868cc255, String x7446840b0e07b9b0) at Aspose.Pdf.Kit.FormEditor.AddListItem(String fieldName, String itemName)
I've attached a project to demonstrate this. It attempts to add an item to each of the two combo boxes on the form. The first already has an item in it, so it is successful. The second is empty so the add fails.
Is there another way to add items to an empty combo? If not, will this be fixed in the next release?
The problem you mentioned was able to be reproduced. I logged it on our bug system as PDFKITNET-4979. It is expected to fixed in 2 weeks. Before the new release, you can use the following method as workaround:
1. invoke FormEditor.RemoveField() to get rid of the empty combo box field
2. create a new check box field with items by invoking FormEditor.AddField()
Maybe a middle stream is needed, and the position of fields is another possible question.Hope it works.
Hmmm...in order to remove the empty combo and then re-add it, I would need to know all the properties of the original combo, e.g. position, height width, font size, font color, etc. Is there a way to retrive this information for an existing field? I've been looking but haven't been able to find it.
The workaround I proposed has a precondition that you have already known those attributes in advance. If it was not true, I would like to appreciate your patience for the new release.
Actually the bug has been fixed. However, only after careful testing will it be published. A good news is that it will be published soon, surely before the end of this month.
Sure, Aspose.Pdf.Kit supports "Export Values" on comboboxes. Before adding a new combobox, please assign both export values and display values to either FormEditor.ExportItems or FormFieldFacade.ExportItems. Details please refer to the following two examples:
[Exam 1] It will be supported in newest release. String[][] itemsArr = new String[4][]; itemsArr[0] = new String[] {"0", "ExportItem0"}; itemsArr[1] = new String[] {"1", "ExportItem1"}; itemsArr[2] = new String[] {"2", "ExportItem2"}; itemsArr[3] = new String[] {"3", "ExportItem3"}; formEditor.ExportItems = itemsArr; formEditor.AddField(FieldType.ComboBox, "defaultcomboBox2", "ExportItem0", 1, 300, 700, 400, 730); formEditor.Save();
[Exam 2] It has already be supported in existing releases. String[][] itemsArr = new String[4][]; itemsArr[0] = new String[] {"0", "ExportItem0"}; itemsArr[1] = new String[] {"1", "ExportItem1"}; itemsArr[2] = new String[] {"2", "ExportItem2"}; itemsArr[3] = new String[] {"3", "ExportItem3"}; FormFieldFacade facade = new FormFieldFacade(); formEditor.Facade = facade; facade .ExportItems = itemsArr; formEditor.AddField(FieldType.ComboBox, "defaultcomboBox2", "ExportItem0", 1, 300, 700, 400, 730); formEditor.Save();
Sorry, I should have been more specific. I meant, will it support export values when adding items to an existing combo box? The AddListItem method does not allow for an export value to be specified. Is there some way to do that now?
Here's the situation we have: We have a lot of forms that are provided to us by our state government. They are resposible for creating the layout of the forms, but we are responsible for filling in the data and populating combo boxes, etc. With the PDF tool we were previously using, we were able to add items to an existing combo box and specify an export value. Your tool apparently doesn't allow us to do that. In theory, we could remove the combo box and re-add it with the items, but that doesn't work for us because we don't know all of the properties of the existing combo box (position, font, etc.) and I've been told we can't determine that information with your tool. We also have a problem with your tool in that it does not allow us to add items to an empty combo box - as long as it has at least one item it works, but not if it's empty. I've been told that will be fixed in the next release.
I am sorry for my misunderstanding. Now I understood your requirement completely. You are going to add a list item to existing combobox, either having existing items or having empty list. And the list item can be either a Display Value, or a pair of (Export Value, Display Value). Am I right? And if possible, could you please provide some samples for our developing and testing?
Till now even the next release, Aspose.Pdf.Kit cannot support the feature you mentioned. But we recorded this problem into our bug trace system as PDFKITNET-5089. It will be finished in one week.
Yes, that accurately describes our situation. I don't know if I can provide an example other than the sample project I provided above to demonstrate the issue. We simply need to be able to add items to an existing combobox (including an export value), even if the combobox is empty.
It would also be nice if we could select an item in the list using the export value (our current tool does this), but it's not necessary.We are basically using the export value as a lookup code; it's what we actually store in the database, because the display values changes from time to time. But we can code our own lookup if necessary.
Never mind. We have fixed both bugs. Now you can add items to an existing combobox , either including an export value or just including display label, whether the combobox is empty or not. And you can also select it by the export value. These features will be included in the next release, which will be published after more testing.
It is expected to be published before the end of this month. Thanks for your patience & Best regards.
Adding items to an empty combo seems to be working. Adding export values is kind of working, but there's a small issue. The AddListItem method describes the second parameter as:
exportName: A string array denoting a new list item with Export Value, i.e. (Item Label, Export Value).
This would seem to indicate that item label should be be the first item item in the array and the export value should be second. However, the result is reversed.
Dim MyEditor As New FormEditor("..\SourceFile1.pdf", "..\Output.pdf")
Dim NewItem() As String = {"Item1", "Export1"}
MyEditor.AddListItem("cboDistofService", NewItem)
MyEditor.Save()
When I execute the above code, the text "Export1" is displayed in the dropdown list, and the text "Item1" is set as the export value. I assume this is backwards.
Yes, you are right. Sorry for the mistake and thanks for your reminding. Definitely, the first element should be the "export value", and the second "item label".