Introduction

This example illustrates how to use the JavaScript PdfWebControlApi Class to detect and interact with events in a PdfWebControl.

The following examples uses a ASP.NET page which as a PdfWebControl on it and a single code behind page.

ASP.NET Code

CopyXML
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Save.aspx.vb" Inherits="_Default" %>

<%@ Register Assembly="RadPdf" Namespace="RadPdf.Web.UI" TagPrefix="radPdf" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body style="margin:0px">
    <form id="form1" runat="server">
    <div>
        <radPdf:PdfWebControl ID="PdfWebControl1" RunAt="server" Height="600px" Width="850px" 
          OnClientLoad="PdfAttachListeners();" />

        <script type="text/javascript">
            function PdfAttachListeners()
            {
                //attach listeners
                (new PdfWebControlApi("<%=Me.PdfWebControl1.ClientID%>")).addEventListener("saveComplete", function(){window.alert("Save complete event raised for " + this.getKey());});
                (new PdfWebControlApi("<%=Me.PdfWebControl1.ClientID%>")).addEventListener("print", function(){window.alert("The print start event was raised. We are going to cancel it.");return false;});
            }
        </script>
    </div>
    </form>
</body>
</html>

Code Behind

CopyVB.NET
Option Explicit On
Option Strict On

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then

            Me.PdfWebControl1.CreateDocument("test", System.IO.File.ReadAllBytes("C:\fw4.pdf"))

        End If
    End Sub
End Class

Remarks

Notice the value of property "OnClientLoad" for PdfWebControl1.

CopyJavaScript
attachPdfListeners();

This code will setup the PdfWebControlApi listeners when the PdfWebControl has loaded.

Note: Listeners can not be attached to a PdfWebControl before it is completely loaded. In most browsers, this event is fired after the page as loaded. This means that events listeners which you attempt to add inline or in the page's load event will likely not be properly attached. In most cases, it is recommended that you use the OnClientLoad property to prevent attempting to access the PdfWebControlApi before it is ready or other JavaScript runtime exceptions.

See Also