0

I have listbox on PDF.

User will select multiple options from listbox.

I need to upload PDF to database.

I am unable to retrieve selected indices from listbox using iTextSharp? I tried with

SetListSelection("listbox", PreviousExport.ToArray) but no luck.

How to retrieve user selected indices from listbox on PDF using itextsharp?

Code from comments:

I am using below code to load listbox .. This is from database

form.SetListOption("ddlNoteStatus", strbuilderExport.ToArray, strbuilderDisplay.ToArray)
stamper.AcroFields.SetField("ddlNoteStatus", "3")

I am able to retrieve other fields from pdf which are not of listbox with below code. But if I use same code for list box only last value selected from list box shows but not all values selected by user

stamper.AcroFields.GetField("txtDateFollow")
Chris Haas
  • 53,986
  • 12
  • 141
  • 274
  • You say you're unable to retrieve but you're also using a `set` method which is for setting a value. Did you try something like this? http://stackoverflow.com/a/3367973/231316 – Chris Haas Apr 25 '14 at 21:14
  • I am using below code to load listbox .. This is from database – user3573785 Apr 25 '14 at 21:25
  • form.SetListOption("ddlNoteStatus", strbuilderExport.ToArray, strbuilderDisplay.ToArray) stamper.AcroFields.SetField("ddlNoteStatus", "3") – user3573785 Apr 25 '14 at 21:25
  • I am able to retrieve other fields from pdf which are not of listbox withe below code . But if i use same code for list box only last value selected from list box shows ... but not all values selected by user .... stamper.AcroFields.GetField("txtDateFollow") – user3573785 Apr 25 '14 at 21:31

1 Answers1

2

Instead of GetField you want to use GetListSelection. To be safe, you might want to always call GetFieldType to determine the type of field that you're looking at. The below code shows this off:

using (var r = new PdfReader(testFile)) {
    var acro = r.AcroFields;
    if(acro.GetFieldType("countries") == AcroFields.FIELD_TYPE_LIST ){
        Console.WriteLine(String.Join(",", acro.GetListSelection("countries").ToArray()));
    }
}

I tested the above code against a PDF that I created using the below code:

var testFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.pdf");

using (var fs = new FileStream(testFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
    using (var doc = new Document()) {
        using (var writer = PdfWriter.GetInstance(doc, fs)) {
            doc.Open();

            doc.Add(new Paragraph("Hello World"));

            var dd = new iTextSharp.text.pdf.TextField(writer, new iTextSharp.text.Rectangle(50, 500, 200, 550), "countries");
            dd.Choices = new string[] { "United States", "Canada", "France" };
            dd.ChoiceExports = new string[] { "US", "CA", "FR" };
            dd.Options = dd.Options | TextField.MULTISELECT;
            dd.ChoiceSelections = new List<int>(new int[] { 0, 2 });
            writer.AddAnnotation(dd.GetListField());
            doc.Close();
        }
    }
}
Chris Haas
  • 53,986
  • 12
  • 141
  • 274
  • sry for late reply.Thanks so much. worked very. YOU saved lot of time. Need to run one more test.. After signing the PDF using acrobat 11 reader,,,,,,,,,,is it possible to retrieve fields .after PDF flattenned. – user3573785 Apr 28 '14 at 14:08
  • Hi Chris, I tried to get information from PDF after signature using acrobat 11.0 sign option. – user3573785 May 07 '14 at 20:32
  • But unable to retrieve information after pdf signed. My requirement,,,,after user signs the document, need to retrieve information from PDF. Is there any option to read the data.... – user3573785 May 07 '14 at 20:33
  • ChoiceSelections is also the only way i could find to set the lookup value of the combobox in codebehind. – Juls Dec 31 '18 at 19:07