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.
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.
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
Can you please share the code that you’re running so we could better understand what you’re trying to achieve?
Yes, multipage TIF files are supported. Please check our demo at View documents online | Free GroupDocs Apps - 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)
- 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 aViewer
type constructor parameternew Viewer("sample.tiff")
. - 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
- The
ReleaseFileStream
is called after the file is rendered, so it is the expected behavior - 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.
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.