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>
<radPdf:PdfWebControl id="PdfWebControl1" runat="server" height="600px" width="100%"
OnSaved="Saved" />
</div>
<div>
<a href="#" onclick="SetCustomImageTool('signature'); return false;">Add Signature Image From Server</a>
</div>
<div>
<a href="#" onclick="SetCustomImageTool('dynamic'); return false;">Add Dynamic Date Image From Server</a>
</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!", 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" type="RadPdf.Web.HttpHandler.PdfHttpHandler"/>
</handlers>
</system.webServer>
</configuration>
App_Code\CustomIntegrationProvider.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(PdfObjectDataContext context)
{
switch(context.Request.ObjectDataKey)
{
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;
}
}
public override void OnObjectDataAdding(ObjectDataAddingEventArgs e)
{
base.OnObjectDataAdding(e);
//If data is added to an image
if (e.PdfObjectType == typeof(PdfImageShape))
{
//Check image size (if larger than 1 MB)
if (e.Data.Length > 0x100000)
{
//Cancel object data adding and display a message
e.Cancel = true;
e.CancelMessage = "Maximum image size is 1 MB.";
}
}
else
{
throw new ArgumentException("PdfObjectType unsupported.");
}
}
}
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>
<radPdf:PdfWebControl id="PdfWebControl1" runat="server" height="600px" width="100%"
OnSaved="Saved" />
</div>
<div>
<a href="#" onclick="SetCustomImageTool('signature'); return false;">Add Signature Image From Server</a>
</div>
<div>
<a href="#" onclick="SetCustomImageTool('dynamic'); return false;">Add Dynamic Date Image From Server</a>
</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!", 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" type="RadPdf.Web.HttpHandler.PdfHttpHandler"/>
</handlers>
</system.webServer>
</configuration>
App_Code\CustomIntegrationProvider.vb
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 PdfObjectDataContext)
Select Case context.Request.ObjectDataKey
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
Public Overrides Sub OnObjectDataAdding(ByVal e As ObjectDataAddingEventArgs)
MyBase.OnObjectDataAdding(e)
'If data is added to an image
If e.PdfObjectType Is GetType(PdfImageShape) Then
'Check image size (if larger than 1 MB)
If e.Data.Length > &H100000 Then
'Cancel object data adding and display a message
e.Cancel = True
e.CancelMessage = "Maximum image size is 1 MB."
End If
Else
Throw New ArgumentException("PdfObjectType unsupported.")
End If
End Sub
End Class