Home : HTML5 Web Viewing Controls : Implementing a Custom Web Document Viewer Handler
Q10347 - HOWTO: Implementing a Custom Web Document Viewer Handler

This article goes over how to create a custom Web Document Viewer handler. This allows you to provide custom image sources to the Web Document Viewer, whether it is a document loaded from a database, a procedurally generated image or simply a stream that doesn't map directly to a file in the relative web path.

The first step is to make sure you have the relevant references in your project. Make sure you have Atalasoft.dotImage.dll, Atalasoft.dotImage.Lib.dll, Atalasoft.dotImage.WebControls.dll, Atalasoft.dotImage.Lib.dll and Atalasoft.Shared.dll added to your project. You will also need any additional libraries you may be using.

The next step is to either create or include a "Generic Handler" (an .ashx file.) It can be either C# or VB.Net. I've attached pre-made handlers for both C# and VB.Net to this KB, so you can skip most of this and jump straight to grabbing those .ashx files.

Once you have that added to your project, open it up and update the definition of the class. Instead of Implementing the IHttpHandler interface we're going to inherit from Atalasoft.Imaging.WebControls.WebDocumentRequestHandler

(Please note that from here on out, you will need to replace our default "Handler" with whatever class name you have chosen.  Also, I'm not going to assume you've added the relevant using or Imports statements to your handler and will include the full class name of all the classes we're using. Feel free to add them and save yourself some space.)

C#
public class Handler : Atalasoft.Imaging.WebControls.WebDocumentRequestHandler

VB.Net
Public Class CustomWebDocumentViewer_VB
     Inherits Atalasoft.Imaging.WebControls.WebDocumentRequestHandler

Then in your class constructor, add the handlers for the two events we're going to be working with, DocumentInfoRequested and ImageRequested.

C#
   public CustomWebDocumentViewer()
    {
        this.DocumentInfoRequested += new Atalasoft.Imaging.WebControls.DocumentInfoRequestedEventHandler(CustomWebDocumentViewer_DocumentInfoRequested);
        this.ImageRequested += new Atalasoft.Imaging.WebControls.ImageRequestedEventHandler(CustomWebDocumentViewer_ImageRequested);
    }

VB.Net
   Public Sub CustomWebDocumentViewer_VB()
     
AddHandler Me.DocumentInfoRequested, AddressOf Me.CustomWebDocumentViewer_DocumentInfoRequested
      AddHandler Me.ImageRequested, AddressOf Me.CustomWebDocumentViewer_ImageRequested
   End Sub

Okay, so all we have left to do is to implement those two events. Before we jump in to that, let me take a moment to explain what each event does. The DocumentInfoRequested event fires only once when .openUrl is called on the viewer or thumbnailer to build a basic document outline for the viewer. It gets the page count and page size before the viewer starts requesting pages. The ImageRequested event fires once per document page, and handles serving the page to the user.

C#
void CustomWebDocumentViewer_DocumentInfoRequested(object sender, Atalasoft.Imaging.WebControls.DocumentInfoRequestedEventArgs e)
{
   //e.FilePath //Requested image
   //e.PageCount //We need to return how many pages to load
   //e.PageSize //We need to return default page size.

   e.PageCount = 1;
   e.PageSize =
new System.Drawing.Size(800, 600);
}

void CustomWebDocumentViewer_ImageRequested(object sender, Atalasoft.Imaging.WebControls.ImageRequestedEventArgs e)
{
   //e.FilePath //Tells you what image is requested
   //e.FrameIndex //What page is requested(0-indexed)
   //e.Image //Return the image to display to client here.

   e.Image =
new Atalasoft.Imaging.AtalaImage(800,600, Atalasoft.Imaging.PixelFormat.Pixel24bppBgr, System.Drawing.Color.Red);
}

VB.Net
Public Sub CustomWebDocumentViewer_DocumentInfoRequested(ByVal sender As Object, ByVal e As Atalasoft.Imaging.WebControls.DocumentInfoRequestedEventArgs)
'e.FilePath 'Requested image
'e.PageCount 'We need to return how many pages to load
'e.PageSize 'We need to return default page size.

e.PageCount = 1
e.PageSize =
New System.Drawing.Size(800, 600)
End Sub

Public Sub CustomWebDocumentViewer_ImageRequested(ByVal sender As Object, ByVal e As Atalasoft.Imaging.WebControls.ImageRequestedEventArgs)
'e.FilePath 'Tells you what image is requested
'e.FrameIndex 'What page is requested(0-indexed)
'e.Image 'Return the image to display to client here.

e.Image =
New Atalasoft.Imaging.AtalaImage(800, 600, Atalasoft.Imaging.PixelFormat.Pixel24bppBgr, System.Drawing.Color.Red)
End Sub

In the DocumentInfoRequested we're telling the viewer to expect a single page at 800, 600 and in the ImageRequested simply serving a new 800, 600 red AtalaImage. You will need to pass in the required information to either get or make your image(s) in the FilePath field. It should be fairly easy with this to use your own image repository to serve to the Web Document Viewer.

The last step is to go in to your .aspx file where you have the WebDocumentViewer and change the _serverUrl to point to your new custom handler.

<script type="text/javascript" language="javascript">
var _docUrl = 'document12345';
var _serverUrl = 'CustomWebDocumentViewer.ashx';
//You get the idea.

That _docUrl will be passed to your handler in the e.FilePath to both the DocumentInfoRequested and ImageRequested events. In your client-side javascript, you can change the document using _viewer.OpenUrl("newDocument") to change the currently loaded document, again whatever you pass in that Open will get passed directly as the e.FilePath

A note about .NET Core support

With the release of DotImage 11.0, we have added support for .NET Core web applications under .NET Framework.

.NET Core does not use generic handlers... however, you can still get at the WDV middleware to handle the same types of events.

Please see the following article for more:
Q10443 - INFO: Changes Introduced in DotImage 11.0

 

Related Articles
No Related Articles Available.

Article Attachments
CustomWebDocumentViewer.ashx.txt
CustomWebDocumentViewer-VB.ashx.txt

Related External Links
No Related Links Available.
Help us improve this article...
What did you think of this article?

poor 
1
2
3
4
5
6
7
8
9
10

 excellent
Tell us why you rated the content this way. (optional)
 
Approved Comments...
No user comments available for this article.

Powered By InstantKB.NET v1.3
Copyright © 2002, 2017. InstantASP Ltd. All Rights Reserved