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

Setting PdfDocumentSource in code

4 Answers 390 Views
PDFViewer
This is a migrated thread and some comments may be shown as answers.
Tim
Top achievements
Rank 1
Tim asked on 29 Jan 2014, 02:44 PM
Hello,
Could you please explain why I can't use the 'using' statement? How am I supposed to dispose the stream and clean up resources?
Thank you
byte[] readAllBytes = File.ReadAllBytes(@"C:\Users\username\Desktop\How_to_dispose_streams_the_right_way.pdf");
//Not working:
using (var memoryStream = new MemoryStream(readAllBytes))
{
this.PdfViewer.DocumentSource = new PdfDocumentSource(memoryStream);
}

//Working:
var memoryStream = new MemoryStream(readAllBytes);
this.PdfViewer.DocumentSource = new PdfDocumentSource(memoryStream);

4 Answers, 1 is accepted

Sort by
0
Kammen
Telerik team
answered on 31 Jan 2014, 04:21 PM
Hello Kai,

RadPdfViewer parses PDF files asynchronously on demand. That is why you cannot dispose the stream after initializing the PdfDocumentSource. The right time to dispose the stream is when the stream is not used anymore. Often this happens when the document is loaded.

PdfDocumentSource has Loaded event where you can close the stream.

Hope this answers your question.

Regards,
Kammen
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 >>
0
Tim
Top achievements
Rank 1
answered on 15 Apr 2014, 09:21 AM
Hello,

Would you mind having a look at this sample? I'm still not sure how to close the stream in a proper manner. Question as source code comment below.

public partial class ControlUserManual : UserControl
    {
        #region properties        public const string PdfViewerPropertyName = "PdfViewer";
        private RadPdfViewer pdfViewer = new RadPdfViewer();
        public RadPdfViewer PdfViewer
        {
            get
            {
                return this.pdfViewer;
            }            set
            {
                this.pdfViewer = value;
                OnPropertyChanged(PdfViewerPropertyName);
            }
        }        #endregion        private readonly MemoryStream documentMemoryStream;        public ControlUserManual(byte[] document)
        {
            InitializeComponent();            this.documentMemoryStream = new MemoryStream(document);   //Stream is never closed
            //this.PdfViewer.DocumentSource = new PdfDocumentSource(new MemoryStream(document));   //
            this.PdfViewer.DocumentSource = new PdfDocumentSource(documentMemoryStream);   
   //Running condition, Stream is not dispose if the document is loaded before hitting the next line.
            this.PdfViewer.DocumentSource.Loaded +=DocumentSource_Loaded;
        }        private void DocumentSource_Loaded(object sender, EventArgs e)
        {
            documentMemoryStream.Dispose();
        }
    }
0
Petya
Telerik team
answered on 16 Apr 2014, 01:16 PM
Hello,

I am not sure I properly understand you comment.

The snippet you pasted seems okay to me. Please note that with this setup the Loaded event will be raised and the stream would be disposed if you open a new document, for example. Like Kammen said, documents are loaded asynchronously in RadPdfViewer so the fact that part of the document is shown does not mean it has been loaded. In fact, the Loaded event is only raised when the stream used by DocumentSource is no longer needed.

I hope this makes things clear.

Regards,
Petya
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Tim
Top achievements
Rank 1
answered on 22 Apr 2014, 07:42 AM
Okay, thank you
Tags
PDFViewer
Asked by
Tim
Top achievements
Rank 1
Answers by
Kammen
Telerik team
Tim
Top achievements
Rank 1
Petya
Telerik team
Share this question
or