Save output to a stream

By default, GroupDocs.Viewer saves output results to the local disk. Also, it can provide results as a stream.

To provide output results as a stream, use one of the following interfaces:

  • IFileStreamFactory defines methods to instantiate and release the output file streams.
  • IPageStreamFactory defines methods to instantiate and release the output page streams.
  • IResourceStreamFactory defines the methods to create resource URLs, instantiate, and release output HTML resource streams.

Use these interface as follows:

The following code snippet shows how to render to HTML with embedded resources and to provide the output result as a stream. To do this, it implements the IPageStreamFactory interface only.

// Create the list to store output pages
List<MemoryStream> pages = new List<MemoryStream>();

using (Viewer viewer = new Viewer("sample.docx"))
    MemoryPageStreamFactory pageStreamFactory = new MemoryPageStreamFactory(pages);
    ViewOptions viewOptions = HtmlViewOptions.ForEmbeddedResources(pageStreamFactory);
internal class MemoryPageStreamFactory : IPageStreamFactory
    private readonly List<MemoryStream> _pages;

    public MemoryPageStreamFactory(List<MemoryStream> pages)
        _pages = pages;

    public Stream CreatePageStream(int pageNumber)
        MemoryStream pageStream = new MemoryStream();
        return pageStream;

    public void ReleasePageStream(int pageNumber, Stream pageStream)
        //Do not release page stream as we'll need to keep the stream open