Skip to main content
Kofax

Implement a Custom Image Handler for WebDocumentViewer

3018570

Question / Problem: 

How do I implement a custom image handler for the WebDocumentViewer?

Answer / Solution: 

In a Kofax Web Capture application, it is common to implement a customized Web Capture Handler to implement custom logic when working with images within the document logic you are working with.

After creating the Kofax Web Capture ASP.NET solution, you can create new "Generic Handler" .ashx handler in either C# or VB.NET, alter the handler to inherit from Atalasoft.Imaging.WebControls.WebDocumentRequestHandler instead of IHttpHandler.

C#:

public class CustomWebDocumentViewer_CS : WebDocumentRequestHandler

VB.NET:

Public Class CustomWebDocumentViewer_VB
     Inherits WebDocumentRequestHandler

Next, we need to wire up the events to be handled.

C#

   public CustomWebDocumentViewer()
    {
        this.DocumentInfoRequested += new  DocumentInfoRequestedEventHandler(CustomWebDocumentViewer_DocumentInfoRequested);
        this.ImageRequested += new  ImageRequestedEventHandler(CustomWebDocumentViewer_ImageRequested);
    }

VB.NET

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

All we have left to do is to implement those two events.

The DocumentInfoRequested event fires only once when .openUrl is called on the viewer or thumbnailer to build a basic document outline for the viewer, returning the page count and page size before the viewer starts requesting pages.

C#
void CustomWebDocumentViewer_DocumentInfoRequested( object sender, 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 = CodeHereToGetActualNumberOfPages();
   e.PageSize = CodeHereToGetSizeOfFirstPage();
}

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 = CodeHereToGetActualNumberOfPages()
    e.PageSize = CodeHereToGetSizeOfFirstPage()
End Sub

The ImageRequested event fires once per document page, and handles serving the page to the user.

C#
void CustomWebDocumentViewer_ImageRequested( object sender, 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 = CodeHereToGetAtalaImageOfSpecificFrameDesired());
}

VB.NET:
Public Sub CustomWebDocumentViewer_ImageRequested( ByVal sender As Object , ByVal e As 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 = CodeHereToGetAtalaImageOfSpecificFrameDesired()
End Sub

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.

 

Applies to:  

Product Version
KWC 11.0
KWC 11.1

 

 

  • Was this article helpful?