MVVM: load pdf by stream or uri

3 posts, 0 answers
  1. cielo valdoz
    cielo valdoz avatar
    33 posts
    Member since:
    Dec 2009

    Posted 28 Mar 2012 Link to this post

    Hi,

    I want to load pdf files dynamically to the pdf viewer but the file is not displaying.

    <

     

     

    telerik:RadPdfViewer Grid.Row="1" HorizontalAlignment="Stretch" Margin="0" DocumentSource="{Binding DocumentStream, Converter={StaticResource DocumentConverter}, ConverterParameter=1}" VerticalAlignment="Stretch" />

     


    DocumentStream is binded on the DocumentSource and assigned by this code:
    #Region "Property"
      
            Private m_DocumentStream As Stream
            Public Property DocumentStream As Stream
                Get
                    Return m_DocumentStream
                End Get
                Set(value As Stream)
                    m_DocumentStream = value
                    Me.RaisePropertyChanged("DocumentStream")
                End Set
            End Property
    #End Region
      
    #Region "Sub"
            Private Sub ReadStream(absolutePath As String)
                Dim wc = New WebClient()
                AddHandler wc.OpenReadCompleted, AddressOf OnWebReadCompleted
                wc.OpenReadAsync(New Uri(absolutePath, UriKind.Absolute))
            End Sub
      
            Private Sub OnWebReadCompleted(sender As Object, e As OpenReadCompletedEventArgs)
                ' Read your stream from e.Result
      
                DocumentStream = e.Result
      
            End Sub
    #End Region
      
    #Region "Navigation"
      
            Public Sub ConfirmNavigationRequest(navigationContext As Microsoft.Practices.Prism.Regions.NavigationContext, continuationCallback As System.Action(Of Boolean)) Implements Microsoft.Practices.Prism.Regions.IConfirmNavigationRequest.ConfirmNavigationRequest
                continuationCallback(True)
            End Sub
      
            Public Function IsNavigationTarget(navigationContext As Microsoft.Practices.Prism.Regions.NavigationContext) As Boolean Implements Microsoft.Practices.Prism.Regions.INavigationAware.IsNavigationTarget
      
            End Function
      
            Public Sub OnNavigatedFrom(navigationContext As Microsoft.Practices.Prism.Regions.NavigationContext) Implements Microsoft.Practices.Prism.Regions.INavigationAware.OnNavigatedFrom
      
            End Sub
      
            Public Sub OnNavigatedTo(navigationContext As Microsoft.Practices.Prism.Regions.NavigationContext) Implements Microsoft.Practices.Prism.Regions.INavigationAware.OnNavigatedTo
                Dim strDocument As String = navigationContext.Parameters("DocumentPath")
      
                ReadStream(strDocument)
            End Sub
      
      
            Public ReadOnly Property KeepAlive As Boolean Implements Microsoft.Practices.Prism.Regions.IRegionMemberLifetime.KeepAlive
                Get
                    Return True
                End Get
            End Property
    #End Region

    Converter for the DocumentStream
    Namespace Converter
        Public Class DocumentConverter
            Implements IValueConverter
      
            Public Function Convert(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
                Dim file As PdfDocumentSource
                Dim intParam As Integer = CInt(parameter)
                If Not value Is Nothing Then
      
                    Select Case intParam
      
                        Case 1 'pdf
                            Using stream As Stream = Application.GetResourceStream(GetResourceUri("documents/test.pdf")).Stream
                                Dim x As Uri
                                x = New Uri("http://localhost:50609/documents/test.pdf")
      
                                file = New PdfDocumentSource(stream)
                                Return file
                            End Using
      
                        Case 2 'word
      
                        Case 3 'excel
      
                    End Select
                Else
                    Return Nothing
                End If
      
            End Function
      
            Public Function ConvertBack(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
      
            End Function
      
            Public Shared Function GetResourceUri(resource As String) As Uri
                Dim assemblyName As New AssemblyName(GetType(DocumentConverter).Assembly.FullName)
                Dim resourcePath As String = "/" + assemblyName.Name + ";component/" + resource
                Dim resourceUri As New Uri(resourcePath, UriKind.Relative)
      
                Return resourceUri
            End Function
        End Class



    It's not displaying even if i tried to load from absolute/relative uri
    Please help.

    Thanks,
    Cielo
  2. Kammen
    Admin
    Kammen avatar
    312 posts

    Posted 28 Mar 2012 Link to this post

    Hello Cielo,

    The problem in your code is that you close your stream before the document is loaded (this is done because of the Using statement). You should note that the DocumentSource loads the document asynchronously. So you can close the stream only when the document is loaded (in the handler of the DocumentSource.Loaded event for example). 

    Kind regards,

    Kammen
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  3. DevCraft banner
  4. cielo valdoz
    cielo valdoz avatar
    33 posts
    Member since:
    Dec 2009

    Posted 28 Mar 2012 Link to this post

    Hi,

    I added this code and solved my problem. Thanks a lot :)
    AddHandler file.Loaded, AddressOf DocEvent_Loaded

     

     

     

    Private Sub DocEvent_Loaded(sender As Object, e As System.EventArgs) Handles DocEvent.Loaded

     

     

     


    End
    Sub

     



Back to Top