RAD PDF - Interactive Demonstrations

In this example, we demonstrate two easy server side integrations with RAD PDF to detect when a user (or your own custom button) saves the document:
  • Integration Provider (MVC or Web Forms) - a custom PdfIntegrationProvider implementation, server side code can listen for a document to be saved.
  • WebControl Event (Web Forms only) - a built in Client Callback raises events attached to RAD PDF's Saved event.
This basic sample code writes an event to the Windows Event Application Log (which can be read using the Event Viewer available in Administrative Tools on any Windows machine) when a document is saved.

Typically, only one of these two methods would be used.

RAD PDF Sample Source Files

Default.aspx

<%@ Page Language="C#" CodeFile="Default.aspx.cs" 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>RAD PDF Sample</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <radPdf:PdfWebControl id="PdfWebControl1" runat="server" height="600px" width="100%"
             OnSaved="Saved" />
    </div>
    </form>
</body>
</html>

Default.aspx.cs

using System;
using System.Diagnostics;
using RadPdf.Integration;

partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) 
        {
            //Get PDF as byte array from file (or database, browser upload, remote storage, etc)
            byte[] pdfData = System.IO.File.ReadAllBytes(@"C:\demo.pdf");
            
            //Load PDF byte array into RAD PDF
            this.PdfWebControl1.CreateDocument("Document Name", pdfData);
        }
    }

    protected void Saved(object sender, DocumentSavedEventArgs e)
    {
        //Check what raised the Saved event
        switch(e.SaveType)
        {
            //When we are saving or downloading
            case DocumentSaveType.Save:
            case DocumentSaveType.Download:

                //Get saved PDF
                byte[] pdfData = e.DocumentData;

                //If desired, we could save the modified PDF to a file, database, send it via email, etc.
                //For example:
                //System.IO.File.WriteAllBytes(@"C:\output.pdf", pdfData);

                //Get its size
                int pdfSize = pdfData.Length;

                //Create our message
                string message = string.Format("A PDF file of {0} bytes was just saved or downloaded! (callback)", pdfSize);

                //Add event to the Windows Application Log
                EventLog.WriteEntry("RAD PDF", message, EventLogEntryType.Information);
                break;

            default:

                //Ignore all other save types (Print, etc)
                break;
        }
    }
}

web.config


<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="RadPdfConnectionString" value="Server=.\SQLExpress;Database=RadPdf;Trusted_Connection=Yes;"/>
    <add key="RadPdfIntegrationProvider" value="CustomPdfIntegrationProvider,App_Code"/>
    <add key="RadPdfLicenseKey" value="DEMO"/>
  </appSettings>
  <system.web>
    <httpHandlers>
      <add path="RadPdf.axd" verb="GET,POST" type="RadPdf.Web.HttpHandler.PdfHttpHandler"/>
    </httpHandlers>
  </system.web>
  <!--
    The system.webServer element is for use with IIS 7 (and later) when Managed Pipeline Mode is set to "Integrated".
    It will be ignored in other versions of IIS.
    -->
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <handlers>
      <add path="RadPdf.axd" verb="GET,POST" name="PdfHttpHandler" preCondition="integratedMode" type="RadPdf.Web.HttpHandler.PdfHttpHandler"/>
    </handlers>
  </system.webServer>
</configuration>

App_Code\CustomPdfIntegrationProvider.cs


using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Web;

using RadPdf.Data.Document.Objects.Shapes;
using RadPdf.Integration;

public class CustomPdfIntegrationProvider : PdfIntegrationProvider
{
    public override void OnDocumentSaved(DocumentSavedEventArgs e)
    {
        base.OnDocumentSaved(e);

        //Check what raised the Saved event
        switch(e.SaveType)
        {
            //When we are saving or downloading
            case DocumentSaveType.Save:
            case DocumentSaveType.Download:

                //Get saved PDF
                byte[] pdfData = e.DocumentData;

                //If desired, we could save the modified PDF to a file, database, send it via email, etc.
                //For example:
                //System.IO.File.WriteAllBytes(@"C:\output.pdf", pdfData);

                //Get its size
                int pdfSize = pdfData.Length;

                //Create our message
                string message = string.Format("A PDF file of {0} bytes was just saved or downloaded! (provider)", pdfSize);

                //Add event to the Windows Application Log
                EventLog.WriteEntry("RAD PDF", message, EventLogEntryType.Information);
                break;

            default:

                //Ignore all other save types (Print, etc)
                break;
        }
    }
}

Default.aspx

<%@ Page Language="VB" CodeFile="Default.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>RAD PDF Sample</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <radPdf:PdfWebControl id="PdfWebControl1" runat="server" height="600px" width="100%"
             OnSaved="Saved" />
    </div>
    </form>
</body>
</html>

Default.aspx.vb

Option Explicit On
Option Strict On

Imports System.Diagnostics
Imports RadPdf.Integration

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

            'Get PDF as byte array from file (or database, browser upload, remote storage, etc)
            Dim pdfData As Byte() = System.IO.File.ReadAllBytes("C:\demo.pdf")

            'Load PDF byte array into RAD PDF
            Me.PdfWebControl1.CreateDocument("Document Name", pdfData)

        End If
    End Sub

    Protected Sub Saved(ByVal sender As Object, ByVal e As DocumentSavedEventArgs)

        'Check what raised the Saved event
        Select Case e.SaveType

            'When we are saving or downloading
            Case DocumentSaveType.Save, DocumentSaveType.Download
                
                'Get saved PDF
                Dim pdfData As Byte() = e.DocumentData
                
                'If desired, we could save the modified PDF to a file, database, send it via email, etc.
                'For example:
                'System.IO.File.WriteAllBytes("C:\output.pdf", pdfData)
                
                'Get its size
                Dim pdfSize As Integer = pdfData.Length
                
                'Create our message
                Dim message As String = String.Format("A PDF file of {0} bytes was just saved or downloaded! (callback)", pdfSize)
                
                'Add event to the Windows Application Log
                EventLog.WriteEntry("RAD PDF", message, EventLogEntryType.Information)

            Case Else
                
                'Ignore all other save types (Print, etc)

        End Select
    End Sub
End Class

web.config


<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="RadPdfConnectionString" value="Server=.\SQLExpress;Database=RadPdf;Trusted_Connection=Yes;"/>
    <add key="RadPdfIntegrationProvider" value="CustomPdfIntegrationProvider,App_Code"/>
    <add key="RadPdfLicenseKey" value="DEMO"/>
  </appSettings>
  <system.web>
    <httpHandlers>
      <add path="RadPdf.axd" verb="GET,POST" type="RadPdf.Web.HttpHandler.PdfHttpHandler"/>
    </httpHandlers>
  </system.web>
  <!--
    The system.webServer element is for use with IIS 7 (and later) when Managed Pipeline Mode is set to "Integrated".
    It will be ignored in other versions of IIS.
    -->
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <handlers>
      <add path="RadPdf.axd" verb="GET,POST" name="PdfHttpHandler" preCondition="integratedMode" type="RadPdf.Web.HttpHandler.PdfHttpHandler"/>
    </handlers>
  </system.webServer>
</configuration>

App_Code\CustomPdfIntegrationProvider.vb

Option Explicit On
Option Strict On

Imports System
Imports System.Diagnostics
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Imaging
Imports System.IO
Imports System.Web

Imports RadPdf.Data.Document.Objects.Shapes
Imports RadPdf.Integration

Public Class CustomPdfIntegrationProvider
    Inherits PdfIntegrationProvider

    Public Overrides Sub OnDocumentSaved(ByVal e As DocumentSavedEventArgs)
        MyBase.OnDocumentSaved(e)

        'Check what raised the Saved event
        Select Case e.SaveType

            'When we are saving or downloading
            Case DocumentSaveType.Save, DocumentSaveType.Download
                
                'Get saved PDF
                Dim pdfData As Byte() = e.DocumentData
                
                'If desired, we could save the modified PDF to a file, database, send it via email, etc.
                'For example:
                'System.IO.File.WriteAllBytes("C:\output.pdf", pdfData)
                
                'Get its size
                Dim pdfSize As Integer = pdfData.Length
                
                'Create our message
                Dim message As String = String.Format("A PDF file of {0} bytes was just saved or downloaded! (provider)", pdfSize)
                
                'Add event to the Windows Application Log
                EventLog.WriteEntry("RAD PDF", message, EventLogEntryType.Information)

            Case Else
                
                'Ignore all other save types (Print, etc)

        End Select
    End Sub
End Class
RAD PDF is a Red Software product - ©2007-2024 Red Software