Adding/changing Items of ComboBoxField

Skip Navigation LinksHome  /  Support  /  Forums  /  DynamicPDF CoreSuite for .NET (v11)  /  Adding/changing Items of ComboBoxField

DynamicPDF CoreSuite for .NET (v11) Forum

Hello together!

I seek to change the item collection of a  ComboBoxField within an existing Document. It is loaded via 'MergeDocument'.
Accoding to the documentation adding items is only possible when using 'ComboBox'. But 'ComboBox' is only 'available' when creating new Documents.
Can you please advice?!

Thanks Robert
Posted by a ceTe Software moderator
Hi Robert,

There is no direct option to modify or add a new item to an existing combo box in a PDF. You can remove the existing combo box and add a new one in that place by adding required items.

Here is a code sample to get items, dimensions and positions of an existing combo box and add new one by modifying the items.

           PdfDocument pdf = new PdfDocument(@"Source PDF file path");
            MergeDocument document = new MergeDocument(pdf);
           
            PdfChoiceField field = (PdfChoiceField)pdf.Form.Fields["cmbox1"];// cmbox1 is existing combo box form field name
           
            string[] arrItems = field.GetItems();
            //Modicy the items in the list as per yur requirements.
            arrItems[0] = "New item";

            //Get the postions of existing Combobox  and add new one using  retrived values.
            int pageNumber = pdf.Form.Fields["cmbox1"].GetOriginalPageNumber() - 1;
            Page page = document.Pages[pageNumber];
            float xPos = pdf.Form.Fields["cmbox1"].GetX(page);
            float yPos = pdf.Form.Fields["cmbox1"].GetY(page);
            float width = pdf.Form.Fields["cmbox1"].Width;
            float height = pdf.Form.Fields["cmbox1"].Height;
            //Remove existing combo box and add a new one.
            document.Form.Fields["cmbox1"].Output = FormFieldOutput.Remove;

            ComboBox comboBoxObj = new ComboBox("cmbox2", xPos, yPos, width, height);
            for (int i = 0; i < arrItems.Length; i++)
            {
                comboBoxObj.Items.Add(arrItems[i]);
            }
           
             page.Elements.Add(comboBoxObj);         
                                   
            document.Draw(@"C:\Temp\Mydocument.pdf");
           

Thanks,
ceTe DynamicPDF Support Team
Hi together!

I already thought about this 'detour'. But: It exactly does, what is desired!
Thank you for your fast response!

Best regards
Robert

All times are US Eastern Standard time. The time now is 11:12 AM.