docViewer ExportToPdf Cannot access a closed Stream

I’m getting this error “Cannot access a closed Stream” while trying to export pdf

I want the api to return the document, i doen’t want to use document viewer ui

string token = docViewer.OpenDocument(attachment.Data.FileData, fileExtension, config, documentOptions);

byte[] docData = docViewer.ExportToPdf();

return new FileContentResult(docData, attachment.Data.FileType);

solved, by this way I was able to return watermark document from the API

         byte[] file = attachment.Data.FileData;
                Stream stream = new MemoryStream(file);
                FileInfo fi = new FileInfo(attachment.Data.FileName);
                var viewToken = docViewer.OpenDocument(stream, fi, config, documentOptions);

                byte[] docData = docViewer.ExportToPdfWatermark("watermark text", 24, System.Drawing.Color.Red, 45, 50, false);

                docViewer.CloseDocument();

                return new FileContentResult(docData, "application/pdf");

@meldeeb,

I am glad the issue was solved. I will copy your code snippet into the other post for future reference, especially for other users who may face the same problem.

1 Like