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

RAD PDF - Interactive Demonstrations

Basic Demo | Customized PDF Viewer | Integration | PDF Editor | PDF Form Filler | MVC | Signatures | Upload
View Source
RAD PDF supports signatures in a variety of ways:
  1. Typed Signature With Cursive Font
    You can use a custom signature font and a simple text form field (or text shape) to give your user a convenient place to type his or her signature. The user simply needs to click the object and type! This sample shows how to setup RAD PDF to use the open source font Herr Von Muellerhoff both as a web font and PDF file processing to facilitate typed signatures.
  2. Image Based Signature
    You can use an image to represent a signature. The user can upload images via the GUI or you can use a more customized integration like this one which loads a specific signature file from the server based on the key passed to our Client API. This sample shows how to use the PdfImageShape to place signature images at the click of a button.
  3. Mouse / Touch Signatures
    RAD PDF also offers a signature shape object which allows the user to draw a signature in a specific region of the page. The user can use a mouse or touch device to sign. This signature is captured as vector and flattened directly into the PDF on save. This example shows a basic implementation with a border.
When saving or printing, the PdfIntegrationProvider implementation adds a timestamp to each page.

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">
    <script type="text/javascript">
        function initRadPdf()
        {
            // Get id
            var id = "<%= PdfWebControl1.ClientID%>";

            // Get api instance
            var api = new PdfWebControlApi(id);

            // Attach listeners
            api.addEventListener(
                "objectClicked", 
                function(evt) {
                    // If this is our special signature object
                    if( "signature" == evt.obj.getProperties()["customData"] )
                    {
                        // Set the image via key
                        evt.obj.setImage("signature");
                    }
                }
                );
        }
    </script>

    <title>RAD PDF Sample</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <radPdf:PdfWebControl id="PdfWebControl1" runat="server" height="600px" width="100%" OnClientLoad="initRadPdf();" />
    </div>
    </form>
</body>
</html>

Default.aspx.cs

using System;
using System.Drawing;
using System.IO;
using System.Web.UI.HtmlControls;

using RadPdf.Data.Document;
using RadPdf.Data.Document.Common;
using RadPdf.Data.Document.Objects;
using RadPdf.Data.Document.Objects.FormFields;
using RadPdf.Data.Document.Objects.Shapes;
using RadPdf.Data.Document.Pages;

partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // Create new document for DEMO
            this.PdfWebControl1.CreateDocument("Document_Name", 1, PdfPageSize.Letter, PdfDocumentSettings.FlattenForm);

            // Setup document editor
            PdfDocumentEditor editor = this.PdfWebControl1.EditDocument();

            int dpi = Convert.ToInt32(editor.Document.Dpi);

            // Create custom font form field example
            PdfTextField field = editor.Pages[0].CreateObject(PdfObjectCreatable.FormFieldText) as PdfTextField;
            field.Top = dpi;
            field.Left = 3 * dpi / 4;
            field.Width = dpi * 2;
            field.Height = dpi / 2;
            field.Font.Name = "Signature";
            field.Font.Size = dpi / 4;
            field.Deletable = false;
            field.Duplicatable = false;
            field.Moveable = false;
            field.Resizable = false;
            field.Stylable = false;

            // Create image example
            PdfImageShape image = editor.Pages[0].CreateObject(PdfObjectCreatable.ShapeImage) as PdfImageShape;
            image.Top = dpi;
            image.Left = 13 * dpi / 4;
            image.Width = dpi * 2;
            image.Height = dpi / 2;
            image.CustomData = "signature";
            image.Deletable = false;
            image.Duplicatable = false;
            image.Moveable = false;
            image.Resizable = false;
            image.Stylable = false;
            image.ImageData = File.ReadAllBytes(Server.MapPath(@"/demo/images/empty.gif"));

            // Create signature shape example
            PdfSignatureShape signature = editor.Pages[0].CreateObject(PdfObjectCreatable.ShapeSignature) as PdfSignatureShape;
            signature.Top = dpi;
            signature.Left = 23 * dpi / 4;
            signature.Width = dpi * 2;
            signature.Height = dpi / 2;
            signature.Border = new PdfBorder(2, PdfBorderStyle.Solid, new PdfColor(Color.Black));
            signature.PenColor = new PdfColor(Color.Blue);
            signature.PenWidth = 2;

            // Create hints
            PdfTextShape fHint = editor.Pages[0].CreateObject(PdfObjectCreatable.ShapeText) as PdfTextShape;
            fHint.Top = 3 * dpi / 2;
            fHint.Left = 3 * dpi / 4;
            fHint.Width = dpi * 2;
            fHint.Height = dpi / 4;
            fHint.Font.Name = "Arial";
            fHint.Font.Size = dpi / 8;
            fHint.Changeable = false;
            fHint.Deletable = false;
            fHint.Duplicatable = false;
            fHint.HideFocusOutline = true;
            fHint.Moveable = false;
            fHint.Resizable = false;
            fHint.Stylable = false;
            fHint.Text = "type a signature";

            PdfTextShape iHint = editor.Pages[0].CreateObject(PdfObjectCreatable.ShapeText) as PdfTextShape;
            iHint.Top = 3 * dpi / 2;
            iHint.Left = 13 * dpi / 4;
            iHint.Width = dpi * 2;
            iHint.Height = dpi / 4;
            iHint.Font.Name = "Arial";
            iHint.Font.Size = dpi / 8;
            iHint.Changeable = false;
            iHint.Deletable = false;
            iHint.Duplicatable = false;
            iHint.HideFocusOutline = true;
            iHint.Moveable = false;
            iHint.Resizable = false;
            iHint.Stylable = false;
            iHint.Text = "click above to sign via image";

            PdfTextShape sHint = editor.Pages[0].CreateObject(PdfObjectCreatable.ShapeText) as PdfTextShape;
            sHint.Top = 3 * dpi / 2;
            sHint.Left = 23 * dpi / 4;
            sHint.Width = dpi * 2;
            sHint.Height = dpi / 4;
            sHint.Font.Name = "Arial";
            sHint.Font.Size = dpi / 8;
            sHint.Changeable = false;
            sHint.Deletable = false;
            sHint.Duplicatable = false;
            sHint.HideFocusOutline = true;
            sHint.Moveable = false;
            sHint.Resizable = false;
            sHint.Stylable = false;
            sHint.Text = "draw a signature with your mouse";

            // Save changes
            editor.Save();
        }
    }
}

web.config

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="RadPdfConnectionString" value="Server=.\SQLExpress;Database=RadPdf;Trusted_Connection=Yes;"/>
    <add key="RadPdfIntegrationProvider" value="SignaturesPdfIntegrationProvider,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\SignaturesPdfIntegrationProvider.cs

using System;
using System.Drawing;
using System.Web;

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

public class SignaturesPdfIntegrationProvider : CustomPdfIntegrationProvider
{
    public SignaturesPdfIntegrationProvider()
        : base()
    {
        // Add the signature font of our choice.
        // This example uses Herr Von Muellerhoff which is an open source font (SIL Open Font License, Version 1.1).
        // Herr Von Muellerhoff must be installed on your server. The TrueType file is provided by Google here:
        // https://github.com/google/fonts/tree/master/ofl/herrvonmuellerhoff
        this.FontResources.Add(new PdfFontResource("Signature", "Herr Von Muellerhoff, cursive, serif", "Herr Von Muellerhoff"));
    }

    private static void ApplyTimestamp(PdfDocument document, string timestampText)
    {
        const string TimestampCustomData = "timestamp";

        int dpi = Convert.ToInt32(document.Dpi);

        // Add our watermark on each page
        foreach (PdfPage p in document.Pages)
        {
            PdfTextShape timestamp = FindOnPage(p, TimestampCustomData) as PdfTextShape;

            // If not found
            if (null == timestamp)
            {
                timestamp = p.CreateObject(PdfObjectCreatable.ShapeText) as PdfTextShape;

                // Set custom data so we can find it next save and re-use it
                timestamp.CustomData = TimestampCustomData;
            }

            // Set position
            timestamp.Left = 0;
            timestamp.Top = dpi * 2;
            timestamp.Height = dpi / 2;
            timestamp.Width = p.Width;

            // Set properties
            timestamp.Changeable = false;
            timestamp.Deletable = false;
            timestamp.Duplicatable = false;
            timestamp.HideFocusOutline = true;
            timestamp.Moveable = false;
            timestamp.Resizable = false;
            timestamp.Stylable = false;
            timestamp.Wrappable = true;

            // Set font
            timestamp.Font.Alignment = PdfHorizontalAlignment.AlignCenter;
            timestamp.Font.Color = new PdfColor(Color.Red);
            timestamp.Font.Size = dpi / 3;

            // Set text
            timestamp.Text = timestampText;
        }
    }

    private static PdfObject FindOnPage(PdfPage page, string searchForCustomData)
    {
        foreach (PdfObject o in page.Objects)
        {
            if (o.CustomData == searchForCustomData)
            {
                return o;
            }
        }

        return null;
    }

    public override void OnDocumentInit(DocumentInitEventArgs e)
    {
        base.OnDocumentInit(e);

        //Add web font for client side fall back if Herr Von Muellerhoff isn't installed on clients.
        e.ExternalStyle = "https://fonts.googleapis.com/css?family=Herr+Von+Muellerhoff";
    }

    public override void OnDocumentPrinting(DocumentPrintingEventArgs e)
    {
        base.OnDocumentPrinting(e);

        string timestampText = "Last Printed " + DateTime.UtcNow.ToString();

        ApplyTimestamp(e.Document, timestampText);
    }

    public override void OnDocumentSaving(DocumentSavingEventArgs e)
    {
        base.OnDocumentSaving(e);

        string timestampText = "Last Saved " + DateTime.UtcNow.ToString();

        ApplyTimestamp(e.Document, timestampText);
    }

    public override void OnDocumentSaved(DocumentSavedEventArgs e)
    {
        base.OnDocumentSaved(e);

        // Reload the document client side after saving
        e.ThenReloadDocument = true;
    }

    public override void ProcessObjectDataRequest(PdfDataContext context)
    {
        switch (context.Request.DataKey)
        {
            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, etc.)
                context.Response.WriteFile(HttpContext.Current.Server.MapPath("/demo/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">
    <script type="text/javascript">
        function initRadPdf()
        {
            // Get id
            var id = "<%= PdfWebControl1.ClientID%>";

            // Get api instance
            var api = new PdfWebControlApi(id);

            // Attach listeners
            api.addEventListener(
                "objectClicked", 
                function(evt) {
                    // If this is our special signature object
                    if( "signature" == evt.obj.getProperties()["customData"] )
                    {
                        // Set the image via key
                        evt.obj.setImage("signature");
                    }
                }
                );
        }
    </script>

    <title>RAD PDF Sample</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <radPdf:PdfWebControl id="PdfWebControl1" runat="server" height="600px" width="100%" OnClientLoad="initRadPdf();" />
    </div>
    </form>
</body>
</html>

Default.aspx.vb

Option Explicit On
Option Strict On

Imports System
Imports System.Drawing
Imports System.IO
Imports System.Web.UI.HtmlControls

Imports RadPdf.Data.Document
Imports RadPdf.Data.Document.Common
Imports RadPdf.Data.Document.Objects
Imports RadPdf.Data.Document.Objects.FormFields
Imports RadPdf.Data.Document.Objects.Shapes
Imports RadPdf.Data.Document.Pages

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

            ' Create new document for DEMO
            Me.PdfWebControl1.CreateDocument("Document_Name", 1, PdfPageSize.Letter, PdfDocumentSettings.FlattenForm)

            ' Setup document editor
            Dim editor As PdfDocumentEditor = Me.PdfWebControl1.EditDocument()

            Dim dpi as Integer = Convert.ToInt32(editor.Document.Dpi)

            ' Create custom font form field example
            Dim field As PdfTextField = DirectCast(editor.Pages(0).CreateObject(PdfObjectCreatable.FormFieldText), PdfTextField)
            field.Top = dpi
            field.Left = 3 * dpi \ 4
            field.Width = dpi * 2
            field.Height = dpi \ 2
            field.Font.Name = "Signature"
            field.Font.Size = dpi \ 4
            field.Deletable = False
            field.Duplicatable = False
            field.Moveable = False
            field.Resizable = False
            field.Stylable = False

            ' Create image example
            Dim image As PdfImageShape = DirectCast(editor.Pages(0).CreateObject(PdfObjectCreatable.ShapeImage), PdfImageShape)
            image.Top = dpi
            image.Left = 13 * dpi \ 4
            image.Width = dpi * 2
            image.Height = dpi \ 2
            image.CustomData = "signature"
            image.Deletable = False
            image.Duplicatable = False
            image.Moveable = False
            image.Resizable = False
            image.Stylable = False
            image.ImageData = File.ReadAllBytes(Server.MapPath("/demo/images/empty.gif"))

            ' Create signature shape example
            Dim signature As PdfSignatureShape = DirectCast(editor.Pages(0).CreateObject(PdfObjectCreatable.ShapeSignature), PdfSignatureShape)
            signature.Top = dpi
            signature.Left = 23 * dpi \ 4
            signature.Width = dpi * 2
            signature.Height = dpi \ 2
            signature.Border = new PdfBorder(2, PdfBorderStyle.Solid, new PdfColor(Color.Black))
            signature.PenColor = new PdfColor(Color.Blue)
            signature.PenWidth = 2

            ' Create hints
            Dim fHint As PdfTextShape = DirectCast(editor.Pages(0).CreateObject(PdfObjectCreatable.ShapeText), PdfTextShape)
            fHint.Top = 3 * dpi \ 2
            fHint.Left = 3 * dpi \ 4
            fHint.Width = dpi * 2
            fHint.Height = dpi \ 4
            fHint.Font.Name = "Arial"
            fHint.Font.Size = dpi \ 8
            fHint.Changeable = False
            fHint.Deletable = False
            fHint.Duplicatable = False
            fHint.HideFocusOutline = True
            fHint.Moveable = False
            fHint.Resizable = False
            fHint.Stylable = False
            fHint.Text = "type a signature"

            Dim iHint As PdfTextShape = DirectCast(editor.Pages(0).CreateObject(PdfObjectCreatable.ShapeText), PdfTextShape)
            iHint.Top = 3 * dpi \ 2
            iHint.Left = 13 * dpi \ 4
            iHint.Width = dpi * 2
            iHint.Height = dpi \ 4
            iHint.Font.Name = "Arial"
            iHint.Font.Size = dpi \ 8
            iHint.Changeable = False
            iHint.Deletable = False
            iHint.Duplicatable = False
            iHint.HideFocusOutline = True
            iHint.Moveable = False
            iHint.Resizable = False
            iHint.Stylable = False
            iHint.Text = "click above to sign via image"

            Dim sHint As PdfTextShape = DirectCast(editor.Pages(0).CreateObject(PdfObjectCreatable.ShapeText), PdfTextShape)
            sHint.Top = 3 * dpi \ 2
            sHint.Left = 23 * dpi \ 4
            sHint.Width = dpi * 2
            sHint.Height = dpi \ 4
            sHint.Font.Name = "Arial"
            sHint.Font.Size = dpi \ 8
            sHint.Changeable = False
            sHint.Deletable = False
            sHint.Duplicatable = False
            sHint.HideFocusOutline = True
            sHint.Moveable = False
            sHint.Resizable = False
            sHint.Stylable = False
            sHint.Text = "draw a signature with your mouse"

            ' Save changes
            editor.Save()

        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="SignaturesPdfIntegrationProvider,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\SignaturesPdfIntegrationProvider.vb

Option Explicit On
Option Strict On

Imports System
Imports System.Drawing
Imports System.Web

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

Public Class SignaturesPdfIntegrationProvider
    Inherits PdfIntegrationProvider

    Public Sub New()
        MyBase.New()

        ' Add the signature font of our choice.
        ' This example uses Herr Von Muellerhoff which is an open source font (SIL Open Font License, Version 1.1).
        ' Herr Von Muellerhoff must be installed on your server. The TrueType file is provided by Google here:
        ' https://github.com/google/fonts/tree/master/ofl/herrvonmuellerhoff
        Me.FontResources.Add(new PdfFontResource("Signature", "Herr Von Muellerhoff, cursive, serif", "Herr Von Muellerhoff"))

    End Sub

    Private Shared Sub ApplyTimestamp(ByVal document As PdfDocument, ByVal timestampText As String)

        Const TimestampCustomData As String = "timestamp"

        Dim dpi As Integer = Convert.ToInt32(document.Dpi)

        ' Add our watermark on each page
        For Each p As PdfPage In document.Pages

            Dim timestamp As PdfTextShape = DirectCast(FindOnPage(p, TimestampCustomData), PdfTextShape)

            ' If not found
            If (timestamp Is Nothing) Then
                timestamp = DirectCast(p.CreateObject(PdfObjectCreatable.ShapeText), PdfTextShape)

                ' Set custom data so we can find it next save and re-use it
                timestamp.CustomData = TimestampCustomData
            End If

            ' Set position
            timestamp.Left = 0
            timestamp.Top = dpi * 2
            timestamp.Height = dpi \ 2
            timestamp.Width = p.Width

            ' Set properties
            timestamp.Changeable = False
            timestamp.Deletable = False
            timestamp.Duplicatable = False
            timestamp.HideFocusOutline = True
            timestamp.Moveable = False
            timestamp.Resizable = False
            timestamp.Stylable = False
            timestamp.Wrappable = True

            ' Set font
            timestamp.Font.Alignment = PdfHorizontalAlignment.AlignCenter
            timestamp.Font.Color = new PdfColor(Color.Red)
            timestamp.Font.Size = dpi \ 3

            ' Set text
            timestamp.Text = timestampText
        Next

    End Sub

    Private Shared Function FindOnPage(ByVal page As PdfPage, ByVal searchForCustomData As String) As PdfObject

        For Each o As PdfObject In page.Objects

            If (o.CustomData = searchForCustomData) Then

                Return o

            End If
        Next

        Return Nothing

    End Function

    Public Overrides Sub OnDocumentInit(ByVal e As DocumentInitEventArgs)
        MyBase.OnDocumentInit(e)

        'Add Web font for client side
        e.ExternalStyle = "https://fonts.googleapis.com/css?family=Herr+Von+Muellerhoff"
    End Sub

    Public Overrides Sub OnDocumentPrinting(ByVal e As DocumentPrintingEventArgs)
        MyBase.OnDocumentPrinting(e)

        Dim timestampText As String = "Last Printed " & DateTime.UtcNow.ToString()

        ApplyTimestamp(e.Document, timestampText)
    End Sub

    Public Overrides Sub OnDocumentSaving(ByVal e As DocumentSavingEventArgs)
        MyBase.OnDocumentSaving(e)

        Dim timestampText As String = "Last Saved " & DateTime.UtcNow.ToString()

        ApplyTimestamp(e.Document, timestampText)
    End Sub

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

        ' Reload the document client side after saving
        e.ThenReloadDocument = true
    End Sub

    Public Overrides Sub ProcessObjectDataRequest(ByVal context As PdfDataContext)

        Select context.Request.DataKey

            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, etc.)
                context.Response.WriteFile(HttpContext.Current.Server.MapPath("/demo/images/signature.gif"))

        End Select
    End Sub

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