Adding/changing Items of ComboBoxField

Adding/changing Items of ComboBoxField

Forum member · Oct 10, 2022
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

Re: Adding/changing Items of ComboBoxField

Support · Oct 10, 2022
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

Re: Adding/changing Items of ComboBoxField

Forum member · Oct 11, 2022
Hi together! I already thought about this 'detour'. But: It exactly does, what is desired! Thank you for your fast response! Best regards Robert