当前位置: 代码迷 >> J2SE >> iText 创办列表框报空指针
  详细解决方案

iText 创办列表框报空指针

热度:744   发布时间:2016-04-23 22:46:36.0
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);