Populate form combo box programmatically

Product: iOS SDK

Product Version: 9.0.1

Please give a brief summary of your issue: I need to populate a combo (or maybe list?) box with options that are defined outside of the PDF.

Please describe your issue and provide steps to reproduce it:
Let’s say I have a list of users I received from my backend: Person 1, Person 2, and Person 3.
When I open the form via PTDOcuementController, I want to grab the “User” field on the form and set it’s options to be those user names.

I already know how to grab the field. I’ve been successful in pre-populating other fields with strings and dates. But I can’t figure out how to add options to that combo/list (I think it’s a combo). Our web team’s SDK simply has an “options” property that is an array that they can populate. I don’t see the same property on the iOS SDK for PTField. Do I have to cast it in some way? I did find a PTComboBoxWidget that has “options”, but not sure how to swizzle it to my needs. Am I just going down the wrong rabbit hole here?

Hello, I’m Ron, an automated tech support bot :robot:

While you wait for one of our customer support representatives to get back to you, please check out some of these documentation pages:

Guides:APIs:Forums:

Hi Randy,

You can add a new option to an annotation (choiceAnnot) with a field of type e_ptchoice like this:

let comboWidget = PTComboBoxWidget(ann: choiceAnnot)
comboWidget.addOption("My New Option")
choiceAnnot.refreshAppearance()
pdfViewCtrl.update(withAnnot: choiceAnnot, page_num: choiceAnnotPageNumber)

Best regards,

James

Sorry, I think I’ll need a little more hand holding on this one.

I’m getting the field by name: let docField = self.document?.getField(“Users”)

That returns to me a PTField object. I know that in the PDF file, this is a combo box. But I don’t know how to connect the dots here. How do I get from “PTField” to “PTComobBoxWidget” so that I can manually set the options in there?

Hi James,

Randy and I have been exploring the option you provided and we’re really close! We’re now able to add the new options and the added options correctly show up when touching the combo box widget. However, when you select one, the selection is not reflected in the PDF. If you select one of the original items in the list, that is updated correctly. Here’s the code we’ve cooked up:

        let field = <This is the dropdown that we're adding options to>

        let itr: PTPageIterator = self.document!.getPageIterator(1)
        var pageNumber: Int32 = 1
        while itr.hasNext()
        {
            let page: PTPage = itr.current()
            let annotationCount = page.getNumAnnots()
            for i in 0..<annotationCount
            {
                let annot: PTAnnot = page.getAnnot(i)
                if !annot.isValid() || annot.getType() != e_ptWidget
                {
                    continue
                }

                if let dropDown = PTComboBoxWidget(annot: annot)
                {
                    if dropDown.getField().getName() == field.getName()
                    {
                        let sortedUsers = DadoApplication.shared.currentCompanyUsers.sorted { $0.1 < $1.1 }
                        dropDown.replaceOptions(sortedUsers.map { $0.value.getDisplayName() })
                        annot.refreshAppearance()
                        pdfViewCtrl.update(with: annot, page_num: pageNumber)
                    }
                }
            }

            itr.next()
            pageNumber += 1
        }

There are some other combinations of things that we’ve tried as well (refreshing the appearance on the field instead of the annotation, pdfViewCtrl.update(with: field), etc), no luck.

As one other data point, if I “Export” the PDF, the combo box has the correct, updated, options in it.

Any ideas?

Hi @caclimber and @Randy_Haid,

Apologies for the delay in response. I just responded to the support ticket that Randy submitted with another approach for getting the widget from the field:

PTField *field = [doc GetField:@"User"];
PTComboBoxWidget *widget = [[PTComboBoxWidget alloc] initWithD:[field GetSDFObj]];
[widget AddOption:@"Test"];

The iteration approach you found is also a good way to do this but is likely to be less efficient.

For the issue with the choice not being persisted you may need to call Update on the PTComboBoxWidget object instead of the PTAnnot:

pdfViewCtrl.update(with: dropdown, page_num: pageNumber)

Does that work?

Also I just realized I sent the code snippet in Objective-C but it will also work in Swift:

let field: PTField = doc.getField("User")
let widget = PTComboBoxWidget(d: field.getSDFObj())

This appears to work for me, thank you!! :smiley:

1 Like