C# PDFViewerContoller caching Issues

Hello All, 

I have a problem when opening the pdf viewer in multiple tabs showing different PDF, the 2nd instance keeps showing the first loaded PDF(cached).

This is MY load function
        [HttpPost("Load")]
        [Microsoft.AspNetCore.Cors.EnableCors("MyPolicy")]
        [Route("[controller]/Load")]
        //Post action for Loading the PDF documents   
        public IActionResult Load([FromBody] Dictionary<string, string> jsonObject)
        {
            SettingsController settings = new SettingsController(_context);
            Settings setting = settings.GetSettingsByName("TEMPLATE_DIR_PATH");
            PdfRenderer pdfviewer = new PdfRenderer(_cache);
            MemoryStream stream = new MemoryStream();
            object jsonResult = new object();
            Templates template = new TemplatesController(_context).GetTemplates(Int32.Parse(jsonObject["document"]));

            string documentName = template.FilePath;
            if (jsonObject != null && jsonObject.ContainsKey("document"))
            {
                if (bool.Parse(jsonObject["isFileName"]))
                {

                    string documentPath = GetDocumentPath(setting.SettingValue + documentName);
                    if (!string.IsNullOrEmpty(documentPath))
                    {
                        byte[] bytes = System.IO.File.ReadAllBytes(documentPath);

                        PdfLoadedDocument loadedDocument = new PdfLoadedDocument(bytes);
                        Syncfusion.Drawing.SizeF pageSize = loadedDocument.Pages[0].Size;
                        PdfMargins margins = new PdfMargins();
                        margins.All = 0;

                        //Insert a new page in the beginning of the document
                        if (template.HasCoverPage.Value)
                        {
                            loadedDocument.Pages.Insert(0, pageSize , margins);
                        }
                        

                        if (template.HasTrailingPage.Value)
                        {
                            loadedDocument.Pages.Add(pageSize , margins);
                        }
                        loadedDocument.Save(stream);
                    }
                    else
                    {
                        //return this.Content(jsonObject["document"] + " is not found");

                        string fileName = jsonObject["document"].Split("://")[0];
                        if (fileName == "http" || fileName == "https")
                        {
                            var WebClient = new WebClient();
                            byte[] pdfDoc = WebClient.DownloadData(documentName);
                            stream = new MemoryStream(pdfDoc);
                        }
                        else
                        {
                            return this.Content(documentName + " is not found");
                        }


                    }
                }
                else
                {
                    byte[] bytes = Convert.FromBase64String(jsonObject["document"]);
                    stream = new MemoryStream(bytes);
                }
            }
            jsonResult = pdfviewer.Load(stream, jsonObject);
            return Content(JsonConvert.SerializeObject(jsonResult));
        }

Thanks,
Mohamed

1 Reply 1 reply marked as answer

AA Akshaya Arivoli Syncfusion Team June 29, 2020 12:02 PM UTC

Hi Mohamed, 

Thank you for contacting Syncfusion support. 

We can reproduce the reported issue with the provided code snippet. We have analyzed and found that the previous stream is not disposed properly so the reported issue occurs. We can resolve this by setting the stream position to 0. Please refer to the below modified code.  

   [HttpPost("Load")] 
        [Microsoft.AspNetCore.Cors.EnableCors("MyPolicy")] 
        [Route("[controller]/Load")] 
        //Post action for Loading the PDF documents    
        public IActionResult Load([FromBody] Dictionary<string, string> jsonObject) 
        { 
            SettingsController settings = new SettingsController(_context); 
            Settings setting = settings.GetSettingsByName("TEMPLATE_DIR_PATH"); 
            PdfRenderer pdfviewer = new PdfRenderer(_cache); 
            MemoryStream stream = new MemoryStream(); 
            object jsonResult = new object(); 
            Templates template = new TemplatesController(_context).GetTemplates(Int32.Parse(jsonObject["document"])); 

            string documentName = template.FilePath; 
            if (jsonObject != null && jsonObject.ContainsKey("document")) 
            { 
                if (bool.Parse(jsonObject["isFileName"])) 
                { 

                    string documentPath = GetDocumentPath(setting.SettingValue + documentName); 
                    if (!string.IsNullOrEmpty(documentPath)) 
                    { 
                        byte[] bytes = System.IO.File.ReadAllBytes(documentPath); 

                        PdfLoadedDocument loadedDocument = new PdfLoadedDocument(bytes); 
                        Syncfusion.Drawing.SizeF pageSize = loadedDocument.Pages[0].Size; 
                        PdfMargins margins = new PdfMargins(); 
                        margins.All = 0; 

                        //Insert a new page in the beginning of the document 
                        if (template.HasCoverPage.Value) 
                        { 
                            loadedDocument.Pages.Insert(0, pageSize , margins); 
                        } 
                         

                        if (template.HasTrailingPage.Value) 
                        { 
                            loadedDocument.Pages.Add(pageSize , margins); 
                       
                        loadedDocument.Save(stream); 
                        stream.Position = 0; 
                    } 
                    else 
                    { 
                        //return this.Content(jsonObject["document"] + " is not found"); 

                        string fileName = jsonObject["document"].Split("://")[0]; 
                        if (fileName == "http" || fileName == "https") 
                        { 
                            var WebClient = new WebClient(); 
                            byte[] pdfDoc = WebClient.DownloadData(documentName); 
                            stream = new MemoryStream(pdfDoc); 
                        } 
                        else 
                        { 
                            return this.Content(documentName + " is not found"); 
                        } 


                    } 
                } 
                else 
                { 
                    byte[] bytes = Convert.FromBase64String(jsonObject["document"]); 
                    stream = new MemoryStream(bytes); 
                } 
            } 
            jsonResult = pdfviewer.Load(stream, jsonObject); 
            return Content(JsonConvert.SerializeObject(jsonResult)); 
        } 



Please revert us with more details about your issue, simple sample if you need any further assistance on this. 

Regards, 
Akshaya 


Marked as answer
Loader.
Up arrow icon