PDF form-filling
<< Click to Display Table of Contents >> Navigation: Gnostice Document Studio .NET > Going Deeper > Document Viewing > ASP.NET > PDF form-filling |
The ASP.NET Document Viewer supports interactive form filling of PDF forms. The viewer automatically identifies form fields in loaded PDF documents and allows the user to interactively fill them. The viewer provides several options to control and configure the form filling process. Note that for password protected PDF files that impose a restriction on filling the form the fields are shown as read-only and cannot be filled. To enable filling of such PDF files either the correct password has to be provided or the system-wide setting ForceFullPermission must be set to true.
Enable Form Filling
The code snippet below shows how to enable interactive form filling by configuring the ViewerPreferences on the server. For more information about this please see the topic on server-side events.
public MyServerEventsHandler : ServerEventsHandler |
Highlight Color
The highlight color of the form fields can be changed by setting the preference on the client-side. We use the highlightColor property to do this. We can also make the form fields to appear transparent with varying levels of transparency by setting the alpha channel value of the highlightColor property.
$(document).ready(function () { |
Submit Form
After filling in the form field values the user can submit the data to the server. Submitting data to the server can be achieved in multiple ways. PDF forms documents can contain a Submit push button within the document with the action type of the push button set to Submit. When the user clicks on the Submit button the values get submitted to the URL configured in the Submit button. This is one approach of submitting form values to the server. For this to work the PDF form must be created with a properly configured Submit button with the Submit URL set to the server you want to receive the data at.
Another approach is to place a Submit button on the web page containing the Document Viewer and then to use the submitForm JavaScript API on the Document Viewer. This would be the approach to take when the PDF document does not contain a Submit button. The submitForm API accepts an URL and submits the form field values to that URL. You can call the submitForm method in the click event of the Submit button placed on the web page outside the viewer frame. The method also accepts parameters such as fields to submit, etc.
The code snippet below shows the usage of the submitForm API.
$(document).ready(function () {
|
The following table shows the fieldName-fieldValue pairs which are submitted to the server during the submit action for different types of form fields.
Field Type |
FieldName Content |
FieldValue Content |
Text box |
Form field mapping name (if present) or form field name |
Form field value |
Check box |
Form Field export value (if present) or form field name of checked box |
|
Radio button |
Form Field export value (if present) or form field name of selected radio button item |
|
List box (single select) |
Form field export value (if present) or form field display value of selected item |
|
List box (multi select) |
Comma separated list of form field export value (if present) or form field display value of selected items |
|
Combo box (single select) |
Form field export value (if present) or form field display value of selected item |
|
Combo box (multi select) |
Comma separated list of form field export value (if present) or form field display value of selected items |
Reset Form
The Document Viewer provides a JavaScript API for resetting the form fields to their default values. Similar to submit action this can also be achieved in multiple ways. PDF forms documents can contain a Reset push button within the document with the action type of the push button set to "Reset". When the user clicks on the Reset button the fields get reset to their default values. This is one approach of resetting form values.
Another approach is to place a Reset button on the web page containing the Document Viewer and then to use the resetForm JavaScript API on the Document Viewer. This would be the approach to take when the PDF document does not contain a Reset button. You can call the resetForm method in the click event of the Reset button placed on the web page. The method accepts parameters such as fields to reset, etc.
The code snippet below shows the signature and usage of the resetForm API.
$(document).ready(function () {
|
Save
To save form field values back to the document on the server the Document Viewer provides the save JavaScript API and the associated beforeDocumentSave and afterDocumentSave events on the client-side. The code snippet below shows the usage of the save API:
$(document).ready(function () {
|
Once the document is saved on the server an event (afterDocumentSave) is genereated on the server. You can use this to get the saved document. The code snippet below shows how this can be done. For more details see the topic server-side events.
public class MyServerEventsHandler : ServerEventsHandler
|
The print JavaScript method can be used to print the document being viewed and filled. Note that the printing happens on the computer where the document is being viewed. When the print method is invoked the document is first saved and then printed.
$(document).ready(function () {
|
Download
The download and downloadAs JavaScript methods can be used to download the document being viewed and filled as a file. When these methods are invoked the document is first saved and then downloaded.
$(document).ready(function () {
|