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

Changing FixedDocumentPresenter(nullException)

1 Answer 110 Views
PDFViewer
This is a migrated thread and some comments may be shown as answers.
Eric
Top achievements
Rank 1
Eric asked on 10 Feb 2018, 05:12 PM

I am getting a null reference exception whenever assigning to the RadPdfViewer FixedDocumentPresenter property. The last line of code below is where an exception is thrown. I have also tried to set the FixedDocumentPresenter property by using pdfViewer.GetRegisteredPresenter(), but I get the same exception. I need the presenter to be set so I can get coordinates of the PDF but the value is null.

            pdfViewer = new RadPdfViewer();
            string pdfFilePath = "C:\\temp\\GeneratedDocument.pdf";
            MemoryStream stream = new MemoryStream();
            using (Stream input = File.OpenRead(pdfFilePath))
            {
                input.CopyTo(stream);
            }
            FormatProviderSettings settings = new FormatProviderSettings(ReadingMode.OnDemand);
            PdfFormatProvider provider = new PdfFormatProvider(stream, settings);
            RadFixedDocument doc = provider.Import();
            pdfViewer.Document = doc;
            pdfViewer.MouseDoubleClick += PdfViewer_MouseDoubleClick;

            var myPresenter = new FixedDocumentSinglePageViewPresenter();
            pdfViewer.FixedDocumentPresenter = myPresenter;

 

EXCEPTION:Error Message: Object reference not set to an instance of an object.

   at Telerik.Windows.Controls.FixedDocumentViewerBase.InitializeFixedDocumentPresenter(IFixedDocumentPresenter releasedPresenter, IFixedDocumentPresenter presenter)
   at Telerik.Windows.Controls.FixedDocumentViewerBase.set_FixedDocumentPresenter(IFixedDocumentPresenter value)

 

1 Answer, 1 is accepted

Sort by
0
Polya
Telerik team
answered on 14 Feb 2018, 03:29 PM
Hello Eric,

The reason for this exception is that the RadPdfViewer is not added in the UI tree, which is required in order for the control to initialize its context, which then initializes its FixedDocumentPresenter.
That said, you need to include the pdfViewer in the UI tree (for example add it in the parent Grid) and call ApplyTemplate() so that everything is initialized before the FixedDocumentPresenter is changed: 
pdfViewer = new RadPdfViewer();
string pdfFilePath = "C:\\temp\\GeneratedDocument.pdf";
MemoryStream stream = new MemoryStream();
using (Stream input = File.OpenRead(pdfFilePath))
{
    input.CopyTo(stream);
}
FormatProviderSettings settings = new FormatProviderSettings(ReadingMode.OnDemand);
PdfFormatProvider provider = new PdfFormatProvider(stream, settings);
RadFixedDocument doc = provider.Import();
pdfViewer.Document = doc;
pdfViewer.MouseDoubleClick += PdfViewer_MouseDoubleClick;
 
this.grid.Children.Add(pdfViewer);
pdfViewer.ApplyTemplate();
 
var myPresenter = new FixedDocumentSinglePageViewPresenter();
pdfViewer.FixedDocumentPresenter = myPresenter;
If you do not wish to display the pdfViewer you could set its Visibility to Collapsed.

In order for us to get a better understanding of what you wish to achieve, I would like to ask you to elaborate more on your scenario: why are you using a new instance of RadPdfViewer, which is not added to the UI of your application; what do you wish to change using a new FixedDocumentPresenter? 
Looking forward to your reply.

Regards,
Polya
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
Tags
PDFViewer
Asked by
Eric
Top achievements
Rank 1
Answers by
Polya
Telerik team
Share this question
or