We're sorry GroupDocs doesn't work properply without JavaScript enabled.

Free Support Forum - groupdocs.com

How to view file directly without storing the converted output file in local?

Hi,
I have used Groupdocs.Viewer nudget package for the web application to view some unsupported files in browser.So when I use it,the file from database is successfully converted to pdf using PdfViewOptions .But the supported output file is saved in my local ,it may affect performance
I need to know in detail is there any way to store the output of viewer in stream.When I click the file it should be viewed in different or same tab.Please let me is there any better options for storing the output.

@Santhosh_JR

Sure, you can save the result into memory using MemoryStream as it shown in the following example:

using System;
using System.IO;
using GroupDocs.Viewer;
using GroupDocs.Viewer.Interfaces;
using GroupDocs.Viewer.Options;

namespace ViewerSampleApp
{
    static class Program
    {
        static void Main()
        {
            MemoryStream stream = new MemoryStream();

            using (Viewer viewer = new Viewer("sample.docx"))
            {
                MemoryFileStreamFactory streamFactory = 
                    new MemoryFileStreamFactory(stream);

                PdfViewOptions viewOptions = 
                    new PdfViewOptions(streamFactory);

                viewer.View(viewOptions);
            }

            Console.WriteLine(stream.Length);
        }
    }

    public class MemoryFileStreamFactory : IFileStreamFactory
    {
        private readonly MemoryStream _stream;

        public MemoryFileStreamFactory(MemoryStream stream)
        {
            _stream = stream;
        }

        public Stream CreateFileStream()
        {
            return _stream;
        }


        public void ReleaseFileStream(Stream fileStream)
        {
            _stream.Position = 0;
            //Do not release stream here we'll need to keep the stream open
        }
    }
}

Please let us know if it works for you.

Hi @vladimir.litvinchik

Thanks for the response.But I need to know can we convert the data in stream again to byte(file) so that when we click the file.It returns the file in converted form .I’m not able to convert streamFactory to byte array.I tried copyto(not working).I also tried ReleaseFileStream still it doesn’t work for me.So I need the stream to be converted as a byte and stored in the file

@Santhosh_JR

Can you please share the code that you’re running so we could better understand what you’re trying to achieve?

Hi @vladimir.litvinchik
Shall we use Groupdocs Viewer to view Multipage TIF .Is that supported?

@Santhosh_JR

Yes, multipage TIF files are supported. Please check our demo at https://products.groupdocs.app/viewer/total - in case you upload multipage TIFF each frame will be rendered as a separate page.

Hi @vladimir.litvinchik.
Thanks for immediate responses.

1.Can we render the tiff as PDF using PdfViewOptions?
2.When we checked ,we find more memory consumption for the view load options it seems,How can we reduce memory consumption or clear memory once it gets loaded
image.png (36.4 KB)

@Santhosh_JR

  1. Yes, you can render TIFF as PDF with PdfViewOptions using the same code as I posted above, except you have to pass the file path as a Viewer type constructor parameter new Viewer("sample.tiff").
  2. The memory consumption depends on a number of parameters including source file size, source file type, rendering options. Since you want to store the result in memory you have to expect that the memory is going to be consumed for some time till GC remove MemoryStream object.

Hi @vladimir.litvinchik
1.The memory consumption is increasing when it hits the interface ,ReleaseFileStream function.Is there any possibility to clear the memory or reduce it.

2.Can you please share any code snippet if possibe,to store converted file as in-memorycache

@Santhosh_JR

  1. The ReleaseFileStream is called after the file is rendered, so it is the expected behavior
  2. The following code snippet shows how to store output PDF file using in-memory cache
using System;
using System.IO;
using GroupDocs.Viewer;
using GroupDocs.Viewer.Interfaces;
using GroupDocs.Viewer.Options;
using Microsoft.Extensions.Caching.Memory;

namespace ViewerSampleApp
{
    static class Program
    {
        static void Main()
        {
            string fileName = "sample.docx";
            IMemoryCache memoryCache =
                new MemoryCache(new MemoryCacheOptions());

            DocxToPdf(fileName, memoryCache);

            if (memoryCache.TryGetValue<byte[]>(fileName, out byte[] bytes))
            {
                Console.WriteLine("Output file size: " + bytes.Length);
            }
        }

        private static void DocxToPdf(string fileName, IMemoryCache memoryCache)
        {

            MemoryFileStreamFactory streamFactory =
                new MemoryFileStreamFactory(fileName, memoryCache);

            using (Viewer viewer = new Viewer(fileName))
            {
                PdfViewOptions viewOptions =
                    new PdfViewOptions(streamFactory);
                viewer.View(viewOptions);
            }
        }
    }

    public class MemoryFileStreamFactory : IFileStreamFactory
    {
        private readonly string _filePath;
        private readonly IMemoryCache _cache;

        public MemoryFileStreamFactory(string filePath, IMemoryCache cache)
        {
            _filePath = filePath;
            _cache = cache;
        }

        public Stream CreateFileStream()
        {
            return new MemoryStream();
        }

        public void ReleaseFileStream(Stream stream)
        {
            var bytes = ((MemoryStream)stream).ToArray();

            _cache.Set(_filePath, bytes);

            stream.Dispose();
        }
    }
}

Please also find the attached sample-app.zip (6.3 KB).

Hi @vladimir.litvinchik
How can we release memory in the viewer object(Viewer viewer) .Dispose method doesn’t make any difference in memory release.

@Santhosh_JR

That’s right, Dispose won’t release memory used by an object since it is designed to release resources that should be manually released, like database connections, file handles, etc. GC is responsible for releasing memory, you can try force garbage collection using GC.Collect(); method.