MVVM: DocumentSource binding

11 posts, 1 answers
  1. Deepak Shakya
    Deepak Shakya avatar
    118 posts
    Member since:
    Aug 2007

    Posted 28 Nov 2011 Link to this post

    Why can't I use binding to the DocumentSource property of the PDFViewer? I am using MVVM pattern and would like to bind it to the Uri property in my viewmodel.

    I have PDFs in the server (ClientBin/PDFs/). How do I display these in my silverlight app?

    EDIT: I saw in the webinar last night that I can point to document in the ClientBin using "../Documents/myDoc.pdf".

    Also, I tried to set the documentsource from the codebehind with a string as in the online example but it says it cannot convert string to FixedDocumentSource (?). How do I set the filelocation in codebehind?
  2. Deepak Shakya
    Deepak Shakya avatar
    118 posts
    Member since:
    Aug 2007

    Posted 29 Nov 2011 Link to this post

    Another issue is that: 

    If I start a new project with a user control and and click event handler to show the childwindow that has PDF viewer, it works fine. The viewer loads and I can even open PDF files using open button.

    I do the same thing in my existing silverlight project that uses MVVM. I simply place a button and then add a click handler to open up a childwindow with the PDFviewer. The child window opens but when I try to open a PDF file using the Open button, nothing appears. The canvas is just blank. I can't even see the "Loading...". I have not done anything drastic. The silverlight project is a working project. It is fairly big project and hence cannot even create a sample to represent the project.

    Any suggestions?
  3. DevCraft banner
  4. Deepak Shakya
    Deepak Shakya avatar
    118 posts
    Member since:
    Aug 2007

    Posted 29 Nov 2011 Link to this post

    Thanks for the another thread regarding "Stream loading" which I failed to look before posting this answered how to set the PdfDocumentSource. I now know how to set the Document source.

    But it still isn't displaying the PDF document.

    • I am using SimpleMVVMToolkit and DelegateCommand (ICommand) to fire up the event from Button click and passing a Uri as CommandParameter.
    • The document is located in ClientBin\Documents\TestDocument.pdf
    • e.Data in the source below has a value: "../Documents/TestDocument.pdf"
    •    private OverviewViewModel vm;
       
             public OverviewView()
      {
          InitializeComponent();
          vm = (OverviewViewModel)DataContext;
          vm.OpenPDFNotice += vm_OpenPDFNotice;
      }
       
      void vm_OpenPDFNotice(object sender, SimpleMvvmToolkit.NotificationEventArgs<string> e)
      {
          //var myViewer = new PDFViewer();
          //myViewer.Show();
          if (e.Data != null)
          {
              var myViewer = new PDFViewer
                                 {
                                     Viewer =
                                         {
                                             DocumentSource = new PdfDocumentSource(new Uri(e.Data,UriKind.Relative))
                                         }
                                 };
              myViewer.Show();
          }
      }
    • The childwindow pops up with the PDFViewer but the document is not loaded. 
    • I try to load the document through the open button but it still doesn't work.

    Any suggestion is highly appreciated.

    Regards.
  5. Kammen
    Admin
    Kammen avatar
    312 posts

    Posted 30 Nov 2011 Link to this post

    Hi Deepak Shakya,

    We created a demo project and tried to load file from ClientBin/Documents folder. The code we have used to do this is the following:

    this.viewer.DocumentSource = new PdfDocumentSource(new Uri("/ClientBin/Documents/Sample.pdf", UriKind.Relative));

    It works fine. Please, note that you should make sure your web project has access to the files in the ClientBin folder.

    All the best,
    Kammen
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  6. Deepak Shakya
    Deepak Shakya avatar
    118 posts
    Member since:
    Aug 2007

    Posted 30 Nov 2011 Link to this post

    Hi Kammen,

    Thanks for your reply. When I create a clean solution, I can access the PDF in the ClientBin and open it. No problem with that, whether it is in the main window or through ChildWindow. Its just that when I implement this in my existing project, the document doesn't seem to load. I know I have access to the files in the ClientBin folder as I can read XML files stored in the directory using Webclient.

    Is there anything that would stop documents from loading? When I click on the open file button, I see the gray gradient screen and black background immediately. Sometimes, the "Loading..." comes up but it seems to do nothing. I have left it for a long time and nothign seems to load. Not sure if something in my existing solution is preventing it from loading. Would you know of anything that would result in document not being loaded? Anything in the viewmodel?

    I will keep trying as this is one of the most required feature in the application. Will let you know if I get through.

    Cheers!
  7. Answer
    Kammen
    Admin
    Kammen avatar
    312 posts

    Posted 05 Dec 2011 Link to this post

    Hello Deepak Shakya,
    Maybe the problem is in the Uri from e.Data - the document is located in "ClientBin\Documents\TestDocument.pdf", but e.Data in the code below has a value "../Documents/TestDocument.pdf". You could try to load the document explicitly from "/ClientBin/Documents/TestDocument.pdf".
    If this doesn't help, you should try to isolate the problem in a sample project, so we can investigate it further.

    Best wishes,
    Kammen
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  8. Deepak Shakya
    Deepak Shakya avatar
    118 posts
    Member since:
    Aug 2007

    Posted 20 Dec 2011 Link to this post

    Hi Kammen,

    Thanks for your reply. I have got to the bottom of the problem and is now solved. 

    The problem was that I had upgraded to Q3 release.

    When I added the PDFViewer, out of four DLLs required by the PDFViewer, one of them was still Q2 Release in my reference. It was Telerik.Windows.Zip.DLL

    Once I had all four DLLs with the correct version (Q3), I can now display PDF files in my application.

    Many thanks!
  9. Pierre
    Pierre avatar
    10 posts
    Member since:
    Jul 2011

    Posted 22 Dec 2011 Link to this post

    Hy,
     I use the
     PDFViewer, I can not run the PDFViewer. Let me explain, I put a pdfdocument in my file "pdf" and I use your code. But nothing to do, the document does not open.
    <telerik:RadPdfViewer x:Name="pdfViewer" DocumentSource="/MyProject;component/pdf/Test.pdf"/>

    I then
     tried your second method with no success.
    private void LoadFromUri(object sender, System.Windows.RoutedEventArgs e)
    {
        this.pdfViewer.DocumentSource = new PdfDocumentSource(new System.Uri("MyProject;component/pdf/Test.pdf", System.UriKind.Relative));
    }

     
    I have this error : 
    SCRIPT5022: Unhandled Error in Silverlight Application
    Code: 4004   
    Category: ManagedRuntimeError      
    Message: System.Collections.Generic.KeyNotFoundException: La clé donnée était absente du dictionnaire.
       à System.ThrowHelper.ThrowKeyNotFoundException()
       à System.Collections.Generic.Dictionary`2.get_Item(TKey key)
       à System.Windows.ResourceManagerWrapper.GetResourceForUri(Uri xamlUri, Type componentType)

    Thanks
  10. Kammen
    Admin
    Kammen avatar
    312 posts

    Posted 23 Dec 2011 Link to this post

    Hello Pierre,

    Can you verify that the PDF file's Build Action is set to Resource. You can select the file, then Right Click / Properties/ Build Action. If that does not help, please send us your demo to further investigate the problem. 


    Greetings,
    Kammen
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  11. Nancy
    Nancy avatar
    4 posts
    Member since:
    Aug 2012

    Posted 16 Aug 2012 Link to this post

    Hi Kammen ,

    I need to  get the data from database but it is from entity framework . How could i make that as stream  from entity to  Radpdfviewer
  12. Kammen
    Admin
    Kammen avatar
    312 posts

    Posted 20 Aug 2012 Link to this post

    Hello Nancy,

    In this article you can find help how to load a PDF file using either a Uri or a Stream. In your case, you can read the data from the database as a byte array and then create a new MemoryStream with the data. Afterwards, you can use this stream as described in the article.

    Kind regards,
    Kammen
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top
DevCraft banner