Created
December 22, 2010 00:16
-
-
Save staxmanade/750864 to your computer and use it in GitHub Desktop.
RadDocumentViewerViewModel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Public Class RadDocumentViewerViewModel | |
Private pdfWebControl As PdfWebControl | |
Property ControlID As String = Guid.NewGuid.ToString().Replace("-", "") | |
Property DocumentKey As String | |
Sub New(ByVal specialDocumentKey As String, ByVal documentName As Func(Of String), ByVal fileContents As Func(Of Byte())) | |
pdfWebControl = New PdfWebControl() | |
Dim radPdfDocumentKey = TryGetCachedDocumentKey(specialDocumentKey) | |
' TODO: How to to test when we set the radPdfDocumentKey to the control it | |
' won't throw an a "Document key has expired or is invalid." exception | |
' Tell the control to load up the document with the following key | |
pdfWebControl.DocumentKey = radPdfDocumentKey | |
' If it's not loaded - create it | |
If (Not pdfWebControl.DocumentID > 0) Then | |
pdfWebControl.CreateDocument(documentName(), fileContents(), PdfDocumentSettings.IsReadOnly) | |
GlobalDocumentKeyCache.Add(specialDocumentKey, pdfWebControl.DocumentKey) | |
End If | |
pdfWebControl.ID = ControlID | |
pdfWebControl.ClientIDMode = ClientIDMode.Static | |
'TOOD: what to do??? Can't seem to set the CssClass on the iframe (see string manipulation in the render control function) | |
'pdfWebControl.CssClass = "rad_pdf_viewer_iframe" | |
Me.DocumentKey = pdfWebControl.DocumentKey | |
End Sub | |
Private ReadOnly Property GlobalDocumentKeyCache As Dictionary(Of String, String) | |
Get | |
Dim documentKeyCache = TryCast(HttpContext.Current.Application("documentKeyCache"), Dictionary(Of String, String)) | |
If (documentKeyCache Is Nothing) Then | |
documentKeyCache = New Dictionary(Of String, String) | |
HttpContext.Current.Application("documentKeyCache") = documentKeyCache | |
End If | |
Return documentKeyCache | |
End Get | |
End Property | |
Private Function TryGetCachedDocumentKey(ByVal specialDocumentKey As String) As String | |
Dim cache = GlobalDocumentKeyCache | |
Dim resultKey As String = Nothing | |
If (cache.TryGetValue(specialDocumentKey, resultKey)) Then | |
Return resultKey | |
End If | |
Return Nothing | |
End Function | |
Public Function RenderPdfControl() As String | |
Dim sb = New StringBuilder() | |
Dim tw = New StringWriter(sb) | |
Dim hw = New HtmlTextWriter(tw) | |
pdfWebControl.RenderControl(hw) | |
Dim html As String = sb.ToString() | |
' The rendered html does not put the RadPdf handler src url at the root of the domain. | |
' In my case it rendered src="/Controller/Action/RadPdf.axd..." | |
' I wanted it to render src="/RadPdf.axd..." | |
html = html.Replace("src=""R", "src=""/R") | |
' The rendered html does not seem to support the webformControl.CssClass property - so hack in my own CSS | |
html = html.Replace("src=", "class='rad_pdf_viewer_iframe' src=") | |
Return html | |
End Function | |
End Class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ Control Language="VB" Inherits="System.Web.Mvc.ViewUserControl(Of RadDocumentViewerViewModel)" %> | |
<div id="rad_document_viewer_container" style="height:100%;"> | |
<%= Model.RenderPdfControl %> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment