Clearing document from viewer causes NullReferenceException

7 posts, 0 answers
  1. Lee
    Lee avatar
    10 posts
    Member since:
    Jan 2013

    Posted 06 Sep 2013 Link to this post

    I was working with the previous version of this control but for some reason it would not render some of our pdf files.  I upgraded to the latest version but now I am running into a new issue.  I can't seem to clear the document from the viewer.  I have attached some sample code that will reproduce the NullReferenceException errors I am getting.

    WPF Code:
    <Window x:Class="TelerikPDFViewerTest.MainWindow"
            Title="MainWindow" Height="550" Width="525">
        <Window.CommandBindings>
            <CommandBinding Command="Open" CanExecute="Open_CanExecute" Executed="Open_Executed" />
            <CommandBinding Command="Close" CanExecute="Close_CanExecute" Executed="Close_Executed" />
        </Window.CommandBindings>
        <Grid>
     
            <telerik:RadPdfViewer Name="pdfViewer" Margin="3,30,3,3" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" />
            <telerik:RadButton Name="btnOpen" Content="Open" Margin="3,3,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="72" Command="Open" />
            <telerik:RadButton Name="btnClose" Content="Close" Margin="0,3,0,3" VerticalAlignment="Top" HorizontalAlignment="Right" Width="72" Command="Close" />
        </Grid>
    </Window>

    C# Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
     
    namespace TelerikPDFViewerTest
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            private System.IO.FileStream fs = null;
            public MainWindow()
            {
                InitializeComponent();
            }
     
            private void Open_CanExecute(object sender, CanExecuteRoutedEventArgs e)
            {
                e.CanExecute =(System.IO.File.Exists("benchbook.pdf"));
                e.Handled = true;
            }
     
            private void Open_Executed(object sender, ExecutedRoutedEventArgs e)
            {
                fs = new System.IO.FileStream("benchbook.pdf", System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Delete);
                pdfViewer.DocumentSource = new Telerik.Windows.Documents.Fixed.PdfDocumentSource(fs);
            }
     
            private void Close_CanExecute(object sender, CanExecuteRoutedEventArgs e)
            {
                e.CanExecute = (this.pdfViewer.Document != null);
                e.Handled = true;
            }
     
            private void Close_Executed(object sender, ExecutedRoutedEventArgs e)
            {
                this.fs.Close();
                //Get NullReferenceException in both cases
                //First try to set document source = null
                this.pdfViewer.DocumentSource = null;
                 
                //Or try just releasing the document
                //this.pdfViewer.Document = null;
            }
        }
    }

    I could not attach the pdf I was using but any pdf will reproduce the problem.  Please let me know if there is some workaround for this.

    Ideally it would be great if the PdfViewer had a Clear() function that would release all resources referenced and displayed.

    Thanks,
    Lee Keel
  2. Petya
    Admin
    Petya avatar
    975 posts

    Posted 10 Sep 2013 Link to this post

    Hi Lee,

    Thank you for contacting us!

    The issue is actually a known one and it has already been resolved in the LIB. The fix will also be included in the next official release Q3 2013.

    As for your suggestion, at this point we do not consider implementing a Clear() method of RadPdfViewer or something similar. The recommended approach for the time being is to set the Document property of RadPdfViewer to null.

    Let us know if you have further comments or questions.

    Regards,
    Petya
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
  3. UI for WPF is Visual Studio 2017 Ready
  4. Stephen
    Stephen avatar
    17 posts
    Member since:
    Aug 2013

    Posted 13 Nov 2013 Link to this post

    I have tried a myriad of things and still cannot clear the document, including,
    rPdfV.Document=null;
    which returns a null exception...

    For now I just manipulate the rPdfV.Visibility property

    (rPdfV is an instance of RadPdfViewer.)
  5. Petya
    Admin
    Petya avatar
    975 posts

    Posted 18 Nov 2013 Link to this post

    Hello,

    Are you experiencing the issue in a project targeting the Q3 2013 version of RadControls for WPF? As previously said, we are aware of a similar problem in the Q2 SP1 2013 version of the controls, but it has been resolved in the most recent official release. 

    I am looking forward to your reply.

    Regards,
    Petya
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
  6. Stephen
    Stephen avatar
    17 posts
    Member since:
    Aug 2013

    Posted 18 Nov 2013 Link to this post

    Yikes, now it is really bad!
    I went ahead and downloaded Q3, removed the old reference in all the projects in my solution, and then added the ref. back in to Q3. The solution compiles fine, but when I try to run it encounters an exception at "xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"" in the XAML.
  7. Petya
    Admin
    Petya avatar
    975 posts

    Posted 20 Nov 2013 Link to this post

    Hello,

    We are not aware of a similar issue in the Q3 2013 version of the controls. Could you please verify all required assembly references are added in your project and all of them are the same version? I'd also suggest trying to clean and rebuild the solution, or even better deleting the bin and obj folders altogether.

    In case this does not help please open a support ticket and attach your project, so that we can troubleshoot it.

    Let me know how it goes.

    Regards,
    Petya
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
  8. Stephen
    Stephen avatar
    17 posts
    Member since:
    Aug 2013

    Posted 21 Nov 2013 Link to this post

    Looks good now.
    I think what works best is removing all the Q2 Telerik references, and then adding all the Q3 back in.
    In any event my runtime errors are cleared up and, more importantly,
    rPdfV.Document=null;
    works...great!
    Thanks!!
Back to Top
UI for WPF is Visual Studio 2017 Ready