How to get an output stream when viewing a document
By default GroupDocs.Viewer saves output results to the local disk but we also provide a way to save output results into a stream.
There are three interfaces that we can utilize:
- IFileStreamFactory - defines the methods that are required for instantiating and releasing output file stream.
- IPageStreamFactory - defines the methods that are required for instantiating and releasing output page stream.
- IResourceStreamFactory - defines the methods that are required for creating resource URL, instantiating and releasing output HTML resource stream.
Let’s say that instead of saving rendering results to the local disk we want to have all the output file or output files in form of stream or list of streams.
What we need to do is to implement one or two of the interfaces listed above.
- When rendering into PDF we have to implement IFileStreamFactory interface and pass implementation into PdfViewOptions constructor
- When rendering into JPG/PNG or HTML with embedded resources we have to implement IPageStreamFactory interface and pass implementation into JpgViewOptions/PngViewOptions constructor or HtmlViewOptions ForEmbeddedResources factory method
- When rendering into HTML with external resources we have to implement IPageStreamFactory and IResourceStreamFactory interfaces and pass implementation into JpgViewOptions/PngViewOptions constructor or HtmlViewOptions ForExternalResources factory method
In this example, we’ll render into HTML with embedded resources so we need to implement only IPageStreamFactory interface.
// 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
}
}
More resources
GitHub Examples
You may easily run the code above and see the feature in action in our GitHub examples:
- GroupDocs.Viewer for .NET examples, plugins, and showcase
- GroupDocs.Viewer for Java examples, plugins, and showcase
- Document Viewer for .NET MVC UI Example
- Document Viewer for .NET App WebForms UI Modern Example
- Document Viewer for Java App Dropwizard UI Modern Example
- Document Viewer for Java Spring UI Example
Free Online App
Along with full-featured .NET library we provide simple but powerful free Apps. You are welcome to view Word, PDF, Excel, PowerPoint documents with free to use online GroupDocs Viewer App.