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:
C# Code:
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
WPF Code:
<
Window
x:Class
=
"TelerikPDFViewerTest.MainWindow"
xmlns:telerik
=
"http://schemas.telerik.com/2008/xaml/presentation"
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