简体   繁体   中英

How to close FileStream without causing an error in "Syncfusion.PdfViewer"?

I'm using Syncfusion.PdfViewer in my project. When I click an item in a list, the related pdf file is loaded and shown in the PdfViewer :

private void PdfReport(string address)
{
    //Load the stream from the local system.
    FileStream fs = new FileStream(address, FileMode.Open);
    PdfSource = fs;
}

The problem is that each time I load a pdf file, a new instance of FileStream is created and the memory usage increases. When I try to close FileStream like the following code, the pdf is not shown in the viewer:

private void PdfReport(string address)
{
    //Load the stream from the local system.
    FileStream fs = new FileStream(address, FileMode.Open);
    PdfSource = fs;
    fs.Dispose();
}

How can I solve this problem?

You should check if a PdfSource exists and if so close/ dispose that before creating the new filestream, so just

if (PdfSource is not null)
    PdfSource.Dispose();

PdfSource = new FileStream(address, FileMode.Open);

In Syncfusion PDFViewer, while loading the PDF document as stream/file, it unloads the existing loaded document internally. While unloading the document, it will dispose the file stream. Hence, we don't need to dispose the file stream on the sample side.

Regards, Salman

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM