2015-10-02 46 views
0

我正在嘗試將javascript驗證添加到現有的Acroform字段通過iText in java。到目前爲止,我已經編寫了下面的代碼,但沒有將操作分配給表單字段。有什麼我失蹤?向iText的pdf中現有的acroform字段添加javascript驗證

PdfReader reader = new PdfReader(uri); 
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest)); 
    PdfWriter writer = stamper.getWriter(); 

    AcroFields acroFields = reader.getAcroFields(); 
    AcroFields.Item dateField = acroFields.getFieldItem("SignDateField"); 
    PdfAction pdfAction = PdfAction.javaScript("app.alert('hello');", writer); 

    PdfDictionary widgetRefDict = (PdfDictionary) PdfReader.getPdfObject(dateField.getWidgetRef(0)); 
    PdfDictionary actionDict = widgetRefDict.getAsDict(PdfName.AA); 
    if (actionDict == null) { 
     actionDict = new PdfDictionary(); 
    } 
    actionDict.put(PdfName.V, pdfAction); 

    stamper.close(); 
    reader.close(); 

回答

2

當有,在註釋詞典沒有AA項,您要創建一個新的字典。但是你並沒有將這個新字典添加到註釋字典中。

// ... 
if (actionDict == null) { 
    actionDict = new PdfDictionary(); 
    // add the newly created action dict 
    widgetRefDict.put(PdfName.AA, actionDict); 
} 
// ... 
+0

這就是我所錯過的,它現在的作品。當action添加到actionDict後,這需要添加。謝謝! – dchar