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.

using System.IO;
using System.Collections.Generic;
using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
using GroupDocs.Viewer.Interfaces;
// ...

// 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);
    viewer.View(viewOptions);
}

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();
        _pages.Add(pageStream);
        return pageStream;
    }

    public void ReleasePageStream(int pageNumber, Stream pageStream)
    {
        //Do not release page stream as we'll need to keep the stream open
    }
}
Imports System.IO
Imports GroupDocs.Viewer
Imports GroupDocs.Viewer.Options
Imports GroupDocs.Viewer.Interfaces
' ...

Module Program
    Sub Main(args As String())
        ' Create the list to store output pages
        Dim pages As New List(Of MemoryStream)()

        Using viewer As New Viewer("sample.docx")
            Dim pageStreamFactory As New MemoryPageStreamFactory(pages)
            Dim viewOptions As ViewOptions = HtmlViewOptions.ForEmbeddedResources(pageStreamFactory)
            viewer.View(viewOptions)
        End Using
    End Sub

    Class MemoryPageStreamFactory
        Implements IPageStreamFactory

        Private ReadOnly _pages As List(Of MemoryStream)

        Public Sub New(pages As List(Of MemoryStream))
            _pages = pages
        End Sub

        Public Function CreatePageStream(pageNumber As Integer) As Stream Implements IPageStreamFactory.CreatePageStream
            Dim pageStream As New MemoryStream()
            _pages.Add(pageStream)
            Return pageStream
        End Function

        Public Sub ReleasePageStream(pageNumber As Integer, pageStream As Stream) _
            Implements IPageStreamFactory.ReleasePageStream
            'Do not release page stream as we'll need to keep the stream open
        End Sub
    End Class
End Module