RAD PDF - PDF Viewer & Editor for ASP.NET
Feature Overview Live Demo Download Support Pricing & Purchasing Contact Us

RAD PDF - Interactive Demonstrations

In this example, we use the Client API to add a custom "stamp" tool which automatically sets the image used to a signature file selected server-side. Showing of the versatility of the PdfIntegrationProvider, we also have setup an image tool which allows the placement of a dynamically generated image.
Custom Stamps:

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>
    <script type="text/javascript">
    function SetCustomImageTool(key)
    {
        //Get ID
        var id = "<%= PdfWebControl1.ClientID%>";
        
        //Get api instance
        var api = new PdfWebControlApi(id);
        
        //Set image mode
        api.setMode(api.Mode.InsertImageShape, key);
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div style="font-size:1.2em; padding:10px;">
        Custom Stamps:
        <button onclick="SetCustomImageTool('signature'); return false;">Add Signature Image From Server</button>
        <button onclick="SetCustomImageTool('dynamic'); return false;">Add Dynamic Date Image From Server</button>
    </div>
    <div>
        <radPdf:PdfWebControl id="PdfWebControl1" runat="server" height="600px" width="100%" />
    </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);
        }
    }
}

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.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 ProcessObjectDataRequest(PdfDataContext context)
    {
        switch(context.Request.DataKey)
        {
            case "dynamic":
                //Create a dynamic image showing the date (200px by 50px)
                using (Bitmap bmp = new Bitmap(200, 50, PixelFormat.Format32bppArgb))
                {
                    //Create graphics object
                    using (Graphics gr = Graphics.FromImage(bmp))
                    {
                        //Set smoothing mode
                        gr.SmoothingMode = SmoothingMode.AntiAlias;

                        //Get the rect for the bitmap
                        RectangleF rect = gr.VisibleClipBounds;

                        //Create a new brush to draw background with
                        using (Brush br = new SolidBrush(Color.Yellow))
                        {
                            //Draw background
                            gr.FillRectangle(br, rect);
                        }

                        //Create a new brush to draw text with
                        using (Brush br = new SolidBrush(Color.Black))
                        {
                            //Create a new font to draw text with
                            using (Font ft = new Font("Arial", 20.0f, FontStyle.Regular, GraphicsUnit.Pixel))
                            {
                                //Create string format to draw text with
                                using (StringFormat sf = new StringFormat())
                                {
                                    //Set format properties
                                    sf.Alignment = StringAlignment.Center;
                                    sf.LineAlignment = StringAlignment.Center;

                                    //Draw current date to bitmap
                                    gr.DrawString(DateTime.Now.ToString("yyyy-MM-dd\nhh:mm tt"), ft, br, rect, sf);
                                }
                            }
                        }
                    }

                    //Create output strea
                    using (MemoryStream ms = new MemoryStream())
                    {
                        //Save image to stream
                        bmp.Save(ms, ImageFormat.Gif);

                        //Write bytes to the response
                        context.Response.Write(ms.ToArray());
                    }
                }
                break;
        
            case "signature":
                //Write a file to the response
                //Alternatively, we could also use the .Write method to write data from almost any source (e.g. database, memory, generated on the file)
                context.Response.WriteFile(HttpContext.Current.Server.MapPath("images/signature.gif"));
                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>
    <script type="text/javascript">
    function SetCustomImageTool(key)
    {
        //Get ID
        var id = "<%= PdfWebControl1.ClientID%>";
        
        //Get api instance
        var api = new PdfWebControlApi(id);
        
        //Set image mode
        api.setMode(api.Mode.InsertImageShape, key);
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div style="font-size:1.2em; padding:10px;">
        Custom Stamps:
        <button onclick="SetCustomImageTool('signature'); return false;">Add Signature Image From Server</button>
        <button onclick="SetCustomImageTool('dynamic'); return false;">Add Dynamic Date Image From Server</button>
    </div>
    <div>
        <radPdf:PdfWebControl id="PdfWebControl1" runat="server" height="600px" width="100%" />
    </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
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.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 ProcessObjectDataRequest(ByVal context As PdfDataContext)

        Select Case context.Request.DataKey
            Case "dynamic"
                'Create a dynamic image showing the date (200px by 50px)
                Using bmp As New Bitmap(200, 50, PixelFormat.Format32bppArgb)

                    'Create graphics object
                    Using gr As Graphics = Graphics.FromImage(bmp)

                        'Set smoothing mode
                        gr.SmoothingMode = SmoothingMode.AntiAlias

                        'Get the rect for the bitmap
                        Dim rect As RectangleF = gr.VisibleClipBounds

                        'Create a new brush to draw background with
                        Using br As Brush = New SolidBrush(Color.Yellow)

                            'Draw background
                            gr.FillRectangle(br, rect)

                        End Using

                        'Create a new brush to draw text with
                        Using br As Brush = New SolidBrush(Color.Black)

                            'Create a new font to draw text with
                            Using ft As New Font("Arial", 20.0F, FontStyle.Regular, GraphicsUnit.Pixel)

                                'Create string format to draw text with
                                Using sf As New StringFormat()

                                    'Set format properties
                                    sf.Alignment = StringAlignment.Center
                                    sf.LineAlignment = StringAlignment.Center

                                    'Draw current date to bitmap
                                    gr.DrawString(DateTime.Now.ToString("yyyy-MM-dd" + vbLf + "hh:mm tt"), ft, br, rect, sf)

                                End Using
                            End Using
                        End Using
                    End Using

                    'Create output strea
                    Using ms As New MemoryStream()

                        'Save image to stream
                        bmp.Save(ms, ImageFormat.Gif)

                        'Write bytes to the response
                        context.Response.Write(ms.ToArray())
                    End Using
                End Using

            Case "signature"
                'Write a file to the response
                'Alternatively, we could also use the .Write method to write data from almost any source (e.g. database, memory, generated on the file)
                context.Response.WriteFile(HttpContext.Current.Server.MapPath("images/signature.gif"))

        End Select

    End Sub

End Class
Terms of Use | Privacy
RAD PDF & PDFescape are Red Software products - ©2007-2018 Red Software