Setting PdfDocumentSource in code

5 posts, 0 answers
  1. Tim
    Tim avatar
    18 posts
    Member since:
    Jul 2013

    Posted 29 Jan 2014 Link to this post

    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);
  2. Kammen
    Admin
    Kammen avatar
    312 posts

    Posted 31 Jan 2014 Link to this post

    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 >>
  3. UI for WPF is Visual Studio 2017 Ready
  4. Tim
    Tim avatar
    18 posts
    Member since:
    Jul 2013

    Posted 15 Apr 2014 in reply to Kammen Link to this post

    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();
            }
        }
  5. Petya
    Admin
    Petya avatar
    975 posts

    Posted 16 Apr 2014 Link to this post

    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.

     
  6. Tim
    Tim avatar
    18 posts
    Member since:
    Jul 2013

    Posted 22 Apr 2014 in reply to Petya Link to this post

    Okay, thank you
Back to Top
UI for WPF is Visual Studio 2017 Ready