RAD PDF - Interactive Demonstrations

This basic implementation demonstrates how to use RAD PDF without a database. Instead, RAD PDF uses Lite Documents and a PdfIntegrationProvider with a custom PdfLiteSessionProvider and PdfLiteStorageProvider are implemented here. This example attempts to demonstrate how easily another storage system can be used instead of SQL Server. While this code uses the filesystem another NoSQL (e.g. Couchbase, DocumentDB, MongoDB, etc) or SQL (e.g. MySQL, PostgreSQL, etc.) based storage could easily be substituted.

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:PdfWebControlLite id="PdfWebControlLite1" runat="server" height="600px" width="100%" />
    </div>
    </form>
</body>
</html>

Default.aspx.cs

using System;

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.PdfWebControlLite1.CreateDocument("Document Name", pdfData);
        }
    }
}

web.config


<?xml version="1.0"?>
<configuration>
  <appSettings>
    <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 RadPdf.Integration;

public class CustomPdfIntegrationProvider : PdfIntegrationProvider
{
    public CustomPdfIntegrationProvider() : base()
    {
        // Use a custom PdfLiteSessionProvider
        this.LiteSessionProvider = new CustomPdfLiteSessionProvider();

        // Use a custom PdfLiteStorageProvider
        // The directory used must allow the web application user to read and write
        this.LiteStorageProvider = new CustomPdfLiteStorageProvider(@"C:\RadPdfLiteStorageProvider");
    }
}

App_Code\CustomPdfLiteSessionProvider.cs


using System;
#if NET40
using System.Collections.Concurrent;
#else
using System.Collections.Generic;
#endif
using System.Web;

using RadPdf.Lite;

public class CustomPdfLiteSessionProvider : PdfLiteSessionProvider
{
    // This example uses an in memory dictionary, which won't have  
    // persistent storage, but a database or other key /value store 
    // can easily be substituted. 
#if NET40
    private readonly ConcurrentDictionary<string, byte[]> _dict;
#else
    private readonly Dictionary<string, byte[]> _dict;
#endif

    public CustomPdfLiteSessionProvider()
        : base()
    {
#if NET40
        _dict = new ConcurrentDictionary<string, byte[]>();
#else
        _dict = new Dictionary<string, byte[]>();
#endif
    }

    public override string AddSession(PdfLiteSession session)
    {
        string key = GenerateKey();

#if !NET40
        lock (_dict)
        {
#endif
            _dict[key] = session.Serialize();
#if !NET40
        }
#endif
        return key;
    }

    public override PdfLiteSession GetSession(string key)
    {
        byte[] data;

#if !NET40
        lock (_dict)
        {
#endif
            data = _dict[key];
#if !NET40
        }
#endif

        if (null == data)
        {
            return null;
        }

        return PdfLiteSession.Deserialize(data);
    }
}

App_Code\CustomPdfLiteStorageProvider.cs


using System;
using System.IO;
using System.Web;

using RadPdf.Lite;

public class CustomPdfLiteStorageProvider : PdfLiteStorageProvider
{
    private readonly DirectoryInfo _dir;

    public CustomPdfLiteStorageProvider(string path)
        : base()
    {
        _dir = new DirectoryInfo(path);

        if (!_dir.Exists)
        {
            _dir.Create();
        }
    }

    private string GetPath(PdfLiteSession session, int subtype)
    {
        return Path.Combine(_dir.FullName, session.ID.ToString("N") + "-" + subtype.ToString() + ".dat");
    }

    public override void DeleteData(PdfLiteSession session)
    {
        FileInfo[] files = _dir.GetFiles(session.ID.ToString("N") + "*");

        foreach (FileInfo file in files)
        {
            lock (string.Intern(file.FullName))
            {
                file.Delete();
            }
        }
    }

    public override byte[] GetData(PdfLiteSession session, int subtype)
    {
        string path = GetPath(session, subtype);

        lock (string.Intern(path))
        {
            if (!File.Exists(path))
            {
                return null;
            }

            return File.ReadAllBytes(path);
        }
    }

    public override void SetData(PdfLiteSession session, int subtype, byte[] value)
    {
        string path = GetPath(session, subtype);

        lock (string.Intern(path))
        {
            File.WriteAllBytes(path, value);
        }
    }
}

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:PdfWebControlLite id="PdfWebControlLite1" runat="server" height="600px" width="100%" />
    </div>
    </form>
</body>
</html>

Default.aspx.vb

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

            '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.PdfWebControlLite1.CreateDocument("Document Name", pdfData)

        End If
    End Sub
End Class

web.config


<?xml version="1.0"?>
<configuration>
  <appSettings>
    <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 RadPdf.Integration

Public Class CustomPdfIntegrationProvider
    Inherits PdfIntegrationProvider

    Public Sub New()
        MyBase.New()

        ' Use a custom PdfLiteSessionProvider
        Me.LiteSessionProvider = New CustomPdfLiteSessionProvider()

        ' Use a custom PdfLiteStorageProvider
        ' The directory used must allow the web application user to read and write
        Me.LiteStorageProvider = New CustomPdfLiteStorageProvider("C:\RadPdfLiteStorageProvider")

    End Sub

End Class

App_Code\CustomPdfLiteSessionProvider.vb

Option Explicit On
Option Strict On

Imports System
#If NET40 Then
Imports System.Collections.Concurrent
#End If
Imports System.Collections.Generic
Imports System.Web

Imports RadPdf.Lite

Public Class CustomPdfLiteSessionProvider
    Inherits PdfLiteSessionProvider

    ' This example uses an in memory dictionary, which won't have  
    ' persistent storage, but a database or other key /value store 
    ' can easily be substituted. 
#If NET40 Then
    Private ReadOnly _dict As ConcurrentDictionary(Of String, Byte())
#Else
    Private ReadOnly _dict As Dictionary(Of String, Byte())
#End If

    Public Sub New()
#If NET40 Then
        _dict = New ConcurrentDictionary(Of String, Byte())()
#Else
        _dict = New Dictionary(Of String, Byte())()
#End If
    End Sub

    Public Overrides Function AddSession(ByVal session As PdfLiteSession) As String

        Dim key As String = GenerateKey()

#If Not NET40 Then
        SyncLock _dict
#End If
            _dict(key) = session.Serialize()
#If Not NET40 Then
        End SyncLock
#End If

        Return key

    End Function

    Public Overrides Function GetSession(ByVal key As String) As PdfLiteSession

        Dim data As Byte()

#If Not NET40 Then
        SyncLock _dict
#End If
            data = _dict(key)
#If Not NET40 Then
        End SyncLock
#End If

        If data Is Nothing Then
            Return Nothing
        End If

        Return PdfLiteSession.Deserialize(data)

    End Function

End Class

App_Code\CustomPdfLiteStorageProvider.vb

Option Explicit On
Option Strict On

Imports System
Imports System.IO
Imports System.Web

Imports RadPdf.Lite

Public Class CustomPdfLiteStorageProvider
    Inherits PdfLiteStorageProvider

    Private ReadOnly _dir As DirectoryInfo

    Public Sub New(ByVal path As String)

        _dir = New DirectoryInfo(path)

        If Not _dir.Exists Then

            _dir.Create()

        End If

    End Sub

    Private Function GetPath(ByVal session As PdfLiteSession, ByVal subtype As Integer) As String

        Return Path.Combine(_dir.FullName, session.ID.ToString("N") & "-" + subtype.ToString() & ".dat")

    End Function

    Public Overrides Sub DeleteData(ByVal session As PdfLiteSession)

        Dim files As FileInfo() = _dir.GetFiles(session.ID.ToString("N") & "*")

        For Each file As FileInfo In files

            SyncLock String.Intern(file.FullName)

                file.Delete()

            End SyncLock

        Next

    End Sub

    Public Overrides Function GetData(ByVal session As PdfLiteSession, ByVal subtype As Integer) As Byte()

        Dim path As String = GetPath(session, subtype)

        SyncLock String.Intern(path)

            If Not File.Exists(path) Then

                Return Nothing

            End If

            Return File.ReadAllBytes(path)

        End SyncLock

    End Function

    Public Overrides Sub SetData(ByVal session As PdfLiteSession, ByVal subtype As Integer, ByVal value As Byte())

        Dim path As String = GetPath(session, subtype)

        SyncLock String.Intern(path)

            File.WriteAllBytes(path, value)

        End SyncLock

    End Sub

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