PDFsharp & MigraDoc Foundation

Combobox SelectedIndex is not working
Page 1 of 1

Author:  sagar.jumble [ Tue Dec 20, 2016 7:19 am ]
Post subject:  Combobox SelectedIndex is not working

I am trying to edit the a PDF But This has China as default value and when I try to set the selected index and save the file China is shown as value,
Also for a file if it doesn't have any default value it always shows empty value.
Below is my code

PdfDocument pd = PdfReader.Open(sourceFilePath, PdfDocumentOpenMode.Modify);

var pdfFields = pd.AcroForm.Fields;

((PdfComboBoxField)pdfFields[i]).SelectedIndex = 0;
//i is index generated when we iterate pdfFields.

Can anyone please try to edit some PDF using PDF sharp in Code and let me know what am I dping wrong.
I have generated editable PDF from http://www.pdfescape.com

Author:  vice [ Tue Oct 13, 2020 5:00 pm ]
Post subject:  Re: Combobox SelectedIndex is not working

Had the same issue, this ultimately worked for me:

var comboBox = field as PdfComboBoxField;
PdfArray indexElement = (PdfArray)comboBox.Elements["/I"];
indexElement.Elements[0] = new PdfInteger(4);
comboBox.Value = new PdfString("Five");

This will set the value properly for display in Acrobat itself. For some reason, just setting Value normally would work but only in less exacting viewers like Chrome or Edge.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group