C# tutorial: PDF List and Combo boxes


PDF List and Combo Boxes

In the previous page, you learnt to add radio buttons, check boxes, and text fields to a PDF document. In this tutorial, you will learn to add more fields, List and Combo boxes, to the PDF document.

List and Combo Boxes are called choice fields. A choice field contains many text items. You can select one or many items from the choice field. A List is referred to a scrollable list box. The end user can select one or more items from a fixed list. A Combo Box is drop down list. You can select only one item from the Combo Box as its value. The Combo Box can be editable. You can type a value other than the predefined items. The image below shows different types of choice fields. There are one single select list box, one multiple select list box, and one combo box.

pdf list and combo boxes

You can use the TextField class to create List and Combo Boxes. Its Choices property specifies the choice field's items to be presented to the user. The ChoiceExports property are the actual values of the choice field. Like HTML, values of select list are used when the list is submitted to server. Instead of using getTextField() to obtain a PdfFormField instance, you now have to use the methods getListField( )or getComboField().

//create a document object
var doc = new Document(PageSize.A4);
//get the current directory
string path = Environment.CurrentDirectory;
//create PdfWriter object
PdfWriter writer=PdfWriter.GetInstance(doc, new FileStream(path + "/pdfdoc.pdf", FileMode.Create));
//define arrays of list items
String[] languages= { "English","French","Khmer","Chinese","Japanese"};
String[] exports = { "En", "Fr", "Kh", "Ch", "Ja" };
//open the document for writing
doc.Open();
//single selction list
TextField slistbox= new TextField(writer,new Rectangle(36,750,150,800),"slistbox");
slistbox.Choices = languages;
slistbox.ChoiceExports = exports;
slistbox.ChoiceSelection = 1;

//multiple selection listbox
TextField mlistbox = new TextField(writer, new Rectangle(36, 690, 150, 740), "mlistbox");
mlistbox.Choices = languages;
mlistbox.ChoiceExports = exports;
mlistbox.Options = TextField.MULTISELECT;
List<int> selections=new List<int>();
selections.Add(0);
selections.Add(2);
mlistbox.ChoiceSelections=selections;

//combobox
TextField combobox = new TextField(writer, new Rectangle(36, 630, 150,650), "combobox");
combobox.Choices = languages;
combobox.ChoiceExports = exports;
combobox.ChoiceSelection = 0;
//make combobox editable
//combobox.Options = TextField.EDIT;

//add widgets to writer
writer.AddAnnotation(slistbox.GetListField());
writer.AddAnnotation(mlistbox.GetListField());
writer.AddAnnotation(combobox.GetComboField());
doc.Close();
//view the result pdf file
System.Diagnostics.Process.Start(path + "/pdfdoc.pdf");

 


Comments

lorretadt comment

 lorretadt

rasteredge provide the fast speed c# add annotations to pdf and add text box in pdf document on http://www.rasteredge.com/how-to/csharp-imaging/pdf-annotate-text-box/




2016-05-03
lorretadt comment

 lorretadt

you may test c# add editable text box to pdf on rasteredge and download this high quality software on rasteredge, get the free trail and vip service on page http://www.rasteredge.com/how-to/csharp-imaging/pdf-annotate-text/


2016-05-03
lorretadt comment

 lorretadt

c# add editable text box to pdf
on this page http://www.rasteredge.com/how-to/csharp-imaging/pdf-text-edit-insert/


2016-04-19



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.