日期:2014-05-20  浏览次数:20815 次

iText 创建列表框报空指针
public void addListBox() throws Exception{
//判断文档是否已经打开
if(!document.isOpen()) {
document.open();
}

writer = PdfWriter.getInstance(document, new FileOutputStream(filePath));
writer.setViewerPreferences(PdfWriter.PageModeUseOutlines);
Rectangle rect = new Rectangle(300, 300, 300, 300);
int cf =1;
TextField text = new TextField(writer, rect,String.format("choice_%s", cf)); 
String[] LANGUAGES ={"English","French","German"};
String[] EXPORTVALUES = { "EN", "DE", "FR", "ES", "NL" };
try {
  switch(cf) {
  case 1:
    text.setChoices(LANGUAGES);               
    text.setChoiceExports(EXPORTVALUES);             
    text.setChoiceSelection(2);               
    writer.addAnnotation(text.getListField());
    break;
  case 2:
    text.setChoices(LANGUAGES);                           
    text.setBorderColor(BaseColor.GREEN);                      
    text.setBorderStyle(PdfBorderDictionary.STYLE_DASHED);
    text.setOptions(TextField.MULTISELECT);               
    ArrayList<Integer> selections = new ArrayList<Integer>();
    selections.add(0);                                          
    selections.add(2);                                       
    text.setChoiceSelections(selections);                    
    PdfFormField field = text.getListField();                
    writer.addAnnotation(field);                             
    break;
  case 3:
    text.setBorderColor(BaseColor.RED);        
    text.setBackgroundColor(BaseColor.GRAY);           
    text.setChoices(LANGUAGES);                
    text.setChoiceExports(EXPORTVALUES);       
    text.setChoiceSelection(4);