This is a migrated thread and some comments may be shown as answers.

MVVM: DocumentSource binding

10 Answers 381 Views
PDFViewer
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Deepak Shakya
Top achievements
Rank 1
Deepak Shakya asked on 28 Nov 2011, 08:06 AM
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?

10 Answers, 1 is accepted

Sort by
0
Deepak Shakya
Top achievements
Rank 1
answered on 29 Nov 2011, 07:04 AM
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?
0
Deepak Shakya
Top achievements
Rank 1
answered on 30 Nov 2011, 02:36 AM
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.
0
Kammen
Telerik team
answered on 30 Nov 2011, 06:07 PM
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 >>

0
Deepak Shakya
Top achievements
Rank 1
answered on 01 Dec 2011, 01:22 AM
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!
0
Accepted
Kammen
Telerik team
answered on 05 Dec 2011, 09:44 AM
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 >>

0
Deepak Shakya
Top achievements
Rank 1
answered on 20 Dec 2011, 07:29 AM
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!
0
Pierre
Top achievements
Rank 1
answered on 22 Dec 2011, 04:16 PM
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
0
Kammen
Telerik team
answered on 23 Dec 2011, 12:54 PM
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 >>

0
Nancy
Top achievements
Rank 1
answered on 17 Aug 2012, 03:23 AM
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
0
Kammen
Telerik team
answered on 20 Aug 2012, 08:41 AM
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.

Tags
PDFViewer
Asked by
Deepak Shakya
Top achievements
Rank 1
Answers by
Deepak Shakya
Top achievements
Rank 1
Kammen
Telerik team
Pierre
Top achievements
Rank 1
Nancy
Top achievements
Rank 1
Share this question
or