Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

Enforcing Required Fields in a PDF Form - ASP.NET and ActivePDF

RSS
Modified on Thu, Jun 21, 2012, 1:12 PM by Administrator Categorized as Uncategorized

Overview

This article explains how to validate that required field in a PDF form are completed before saving the file.

  • In the ASPX code, the <body> tag's onload attribute fires the setEvent() function which adds a listener to the save event. This listener calls the checkFormCompleted method to verify that all required fields are filled in.

  • In the code-behind code, the document is loaded using the PdfDocumentSettings.EnforceRequired option of the PdfWebControl.CreateDocument method.

Code Solution

ASPX Code

<%@ Register Assembly="APPortalUI" Namespace="APPortalUI.Web.UI" TagPrefix="apPortalUI" %>
<html>
<head>
<script type="text/javascript">
function setEvent() {
    var myApi = new PdfWebControlApi('<%=this.PdfWebControl1.DivID%>');
    
    myApi.addEventListener('save', function () {
        var completed = this.checkFormCompleted();
        if (!completed) {
            alert('Please complete all required fields, which are highlighted in red.');
            return false;
        }
    });
    myApi.addEventListener('saveComplete', function () {
        var sigReq = $('#<%=this.hdnSignatureRequired.ClientID%>').val() == '1';
        var signed = $('#<%=this.hdnSigned.ClientID%>').val() == '1';
        if (sigReq && !signed) {
            $('#divSignature').dialog('open');
        }
        else if (sigReq && signed) {
            document.forms["myform"].submit();
        }
        else {
            window.location.href = '/ThankYou.aspx';
        }
    });
}
</script>
</head>
<body onload="setEvent();">

<input type="button" id="saveForm1" value="Save Form" class="saveForm" />

<apPortalUI:PdfWebControl id="PdfWebControl1" runat="server" height="900px" 
    width="100%" onsavecomplete="PdfWebControl1_SaveComplete" 
    HideSideBar="True" HideTopBar="True" ViewerZoomDefault="ZoomFitWidth100" />

</body>
</html>

=Code-Behind

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsCallback)
    {
        if (IsPostBack)
        {
            SignForm();
        }
        else
            LoadPDF();
    }
}

void LoadPDF()
{
    int userID, formID, userFormID;
    int.TryParse(Request.QueryString["UserID"], out userID);
    int.TryParse(Request.QueryString["FormID"], out formID);
    int.TryParse(Request.QueryString["UserFormID"], out userFormID);
    UserID = userID;

    FormID = formID;
    UserFormID = userFormID;
    CurrentUP = DataAccessSQL.GetPDFUserProfile(UserID);
    CurrentFF = DataAccessSQL.GetFormFields(FormID);

    if (UserFormID > 0)
    {
        CurrentUF = DataAccessSQL.GetUserForm(UserFormID, null, null);
        UserID = CurrentUF.UserID;
        FormID = CurrentUF.SourceFormID;
        hdnSigned.Value = (CurrentUF.Signed ? "1" : "0");
    }
    CurrentSF = DataAccessSQL.GetSourceForm(FormID, null);
    hdnSignatureRequired.Value = (CurrentSF.SignatureReqd ? "1" : "0");
    string filePath = (UserFormID > 0) ? GetUserFormsFilePath(CurrentUF.PDFName) : GetSourceFormsFilePath(CurrentSF.FilePath);
    byte[] pdfData = File.ReadAllBytes(filePath);
    this.PdfWebControl1.CreateDocument(CurrentSF.FormName, pdfData, PdfDocumentSettings.IsReadOnlyExceptFormFields | PdfDocumentSettings.EnforceRequired);

    PrefillPDF(CurrentUP);

}

ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.