Cannot convert from "System.IO.Stream" in "GroupDocs.Viewer.Common.Func<System.IO.Stream>"

Hello,

when i try to put a stream in the viewer i get following failure:

Cannot convert from “System.IO.Stream” in
“GroupDocs.Viewer.Common.Func<System.IO.Stream>”

I use following code in a .net framework WinForms Project:

            Stream stream = GetDocument(dokumentZuAktenzeichen.DokumentId);
            using (Viewer viewer = new Viewer(stream))
            {
                HtmlViewOptions viewOptions = HtmlViewOptions.ForExternalResources();
                viewer.View(viewOptions);
            }
1 Like

@ramomJr

Please have a look at this sample code. This is how you load a document from stream.

This means that i have to store the stream in a file and then read it from there?

@ramomJr

Yes. Have a look at this documentation article.

Okay, that works. Are there any plans that it could work with a stream directly?

1 Like

@ramomJr

We’ll investigate this. Your investigation ticket ID is VIEWERNET-2593.

@ramomJr

The example here shows that API accepts a delegate - the reference to the method that returns a stream. It doesn’t matter where the stream comes from. Check the following example where we’re creating a stream and passing it to the Viewer.

// we're using GroupDocs.Viewer.Common.Func here as there are no Func delegates in .NET Framework 2.0
GroupDocs.Viewer.Common.Func<Stream> streamFactory = () => {

    // this code will be called when you call viewer.View()
    byte[] bytes = Encoding.UTF8.GetBytes("Hello world!");

    // the stream will be disposed by the Viewer when Dispose() will be called on Viewer instance
    return new MemoryStream(bytes);
};

GroupDocs.Viewer.Common.Func<LoadOptions> loadOptionsFactory = () => {
    LoadOptions loadOptions = new LoadOptions(FileType.TXT);
    return loadOptions;
}; 

using (Viewer viewer = new Viewer(streamFactory, loadOptionsFactory))
{
    viewer.View(HtmlViewOptions.ForEmbeddedResources("created_file.html"));
}

Or where we’re downloading the file from the internet:

GroupDocs.Viewer.Common.Func<Stream> streamFactory = () =>
{
    using (WebClient webClient = new WebClient())
    {
        byte[] data = webClient.DownloadData("https://cms.admin.containerize.com/templates/groupdocs/img/bg/header2.jpg");
        MemoryStream content = new MemoryStream(data);

        return content;
    }
};

using (Viewer viewer = new Viewer(streamFactory))
{
    viewer.View(HtmlViewOptions.ForEmbeddedResources("from_internet.html"));
}

The Viewer accepts Func instead of Stream because we want to have full control over the stream. Please explore this sample_app.zip (1.6 KB) and let us know if you have any questions.