How to output a stream instead of creating html documents using C#

Hi,

    I'm evaluating GroupDocs.Viewer 19.9 
    The samples in the developer guide are based on a console application which takes a document as input (it could be .docx, .xlsx, .txt whatever), creates its equivalent html/png/jpg file in the output folder.

    I would like to know if it's possible not to create html/png/jpg file in the output folder but just output as a stream like in your previous sample based on version 19.3 + angular js 
   https://github.com/groupdocs-viewer/GroupDocs.Viewer-for-.NET-MVC-App

   My goal is to do a demo for an **MVC application**, and I would like to have a sample like the one above with the version 19.9 instead of 19.3 ... There are breaking changes since 19.7 so I can't use the same code as in 19.3 for the version 19.9 ... All I have is console application samples with the usual code like this 

using (var viewer = new Viewer(inputFilePath))
{
HtmlViewOptions options = HtmlViewOptions.ForEmbeddedResources(pageFilePathFormat);
viewer.View(options);
}

Any sample code for MVC is appreciated,
Anthony

@tony47,

We have logged it in our Issue Tracking System as VIEWERNET-2179 to further investigate this scenario. We shall get back to you as soon as we have any updates.

We do have it in our plans to upgrade the API version in the demo MVC application. The process will be initiated in the coming week, however, we can not promise any ETA at the moment.

@tony47,

We have got the updates for you on how to get and process the output as streams. In general, you have two options to do that:

  1. Using delegates
  2. Using interfaces

Instead of passing page/resource file path format into ViewOptions constructors or factory methods, you have to initialize and release the stream. The workflow of the process is the following:

  1. You initialize the stream.
  2. Viewer writes the data into it.
  3. You post-process and release the stream.

You can download this sample application that demonstrates both methods. I hope it helps.

Hi,

I examined the sample application. It creates HTML files physically on the disk using Streams. Is it possible - for an asp.net MVC application - to output a System.Web.Mvc.FileContentResult to a view Instead of this code which is in the sample?

            using (Viewer viewer = new Viewer(FileName))
            {
                ViewOptions viewOptions =
                    HtmlViewOptions.ForEmbeddedResources(
                        (int pageNumber) =>
                        { 
                            string pageFilePath = string.Format(pageFileFormat, pageNumber);
                            Stream pageStream = System.IO.File.OpenWrite(pageFilePath);
                            return pageStream;
                        },
                        (int pageNumber, Stream pageStream) => pageStream.Dispose()
                    );

                viewer.View(viewOptions);
            }

How can we transform this code to return a System.Web.Mvc.FileContentResult ?

Thanks for your help,
Anthony

@tony47,

One possible way to return rendered HTML pages as System.Web.Mvc.FileContentResult is mentioned below. You can modify this code further to meet your needs.

public ActionResult Index()
{
	string outputPath = Server.MapPath("~/output");
	string outputDir = Path.Combine(outputPath, "html_embedded/");
	string pageFileFormat = Path.Combine(outputDir, "p_{0}.html");

	if (!Directory.Exists(outputDir))
		Directory.CreateDirectory(outputDir);

	using (Viewer viewer = new Viewer("D:\\storage\\candy.pdf"))
	{
		ViewOptions viewOptions =
			HtmlViewOptions.ForEmbeddedResources(pageFileFormat);

		viewer.View(viewOptions);
	}

	byte[] contents = System.IO.File.ReadAllBytes(Path.Combine(outputDir, string.Format("p_{0}.html", 1)));
	return File(contents, "text/html", "sample.html");  
}

@usman.aziz

     The code you gave me downloads *only* the 1st page of a word document as "sample.html".

     Is it possible to view a file as a stream instead ? I mean I don't want to create a physical html file on disk (in ~/output/html_embedded/ ..)  and then view it. I just want to output a stream. 

thanks in advance,
Tony47

@tony47,

That code is only for the demonstration of how to return page as FileContentResult and you can modify it as per your scenario.

For getting stream of each HTML page, you can use the sample application shared in the previous reply. However, please note that in both cases, the physical HTML files will be generated on the disk.