问题
Is is possible to add validation to PDF form fields via iTextSharp.
I currently generate a PDF document, fill editable form fields with values from my database and present the document to the user in a webpage.
PdfReader pdfReader = new PdfReader(template);
PdfStamper pdfStamper = new PdfStamper(pdfReader, writeStream);
AcroFields pdfFormFields = pdfStamper.AcroFields;
pdfFormFields.SetField("field1", myobj.field1value);
...
pdfStamper.Close();
I'd like to be able to dynamically add validation e.g. numeric field min-max values, or custom JavaScript to the field in this document rendering process. I know this is possible when designing the form in Acrobat, but I can't find any methods/fields for accessing these validation fields through iTextsharp.
回答1:
When looking for answers, please consult the documentation: "iText in Action".
Your question is answered by an example:
- Java: http://itextpdf.com/examples/iia.php?id=238
- C#: http://kuujinbo.info/iTextInAction2Ed/index.aspx?ch=Chapter13&ex=AddJavaScriptToForm
In this example, some custom JavaScript including a validate() method is added to a field/button. This is the JavaScript: http://examples.itextpdf.com/resources/js/extra.js
The JS added as Additional Action (AA) or as action to a Submit button is identical to the JS one would write for HTML.
来源:https://stackoverflow.com/questions/13311991/itextsharp-pdf-form-field-validation