Render Documents in Web Page using ASP.NET MVC

Hello,

I am trying to integrate the GroupDocs Viewer into my .NET MVC application. Within the controller it appears to be functioning correctly and converting documents into Viewer objects. Where I am struggling is displaying the Viewer object in a View/HTML page. I’ve looked at the MVC demo for the document viewer and when I tried to run it in IIS it crashed.

Is there another resource or example that explains how to display the converted document on a webpage?

I’m using this snippet to create the viewer:

    using (Viewer viewer = new Viewer("aDocument.docx"))
    {
        HtmlViewOptions options = HtmlViewOptions.ForEmbeddedResources();
        viewer.View(options);
    }

I’m not sure how to utilise this object to display in a view.

@LBuchan1

Welcome to GroupDocs Forum!

The Viewer itself does not have any UI that can be used in a view. It’s a converter that can load a file and create pages in HTML/PNG/JPEG formats. The code snippet posted above will open a document and create HTML files on a disk where each HTML file represents a page in a document.

To render a document page on a webpage you can return that page contents as shown in the code snippet (I’m running .NET Core App but the idea would be the same for ASP.NET MVC)

using System.IO;
using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;

namespace RenderPage
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapGet("/", async context =>
                {
                    var pageStream = GetPageStream("sample.docx", 1);

                    context.Response.ContentType = "text/html";
                    await pageStream.CopyToAsync(context.Response.Body);
                });
            });
        }

        private static MemoryStream GetPageStream(string filePath, int pageNumber)
        {
            var pageStream = new MemoryStream();

            using (Viewer viewer = new Viewer(filePath))
            {
                HtmlViewOptions viewOptions = HtmlViewOptions.ForEmbeddedResources(
                    num => pageStream,
                    (num, stream) => { });

                viewer.View(viewOptions, pageNumber);
            }

            pageStream.Position = 0;
            return pageStream;
        }
    }
}

Your application is targeting .NET Framework or .NET / .NET Core?

In case it’s .NET / .NET Core application please check this demo app that handles rendering and includes UI out of the box.

Hi again,

I’ve been using the solution suggested above and putting the MemoryStream object into the ViewBag of my view. This has worked so far with most file types but seems to have issues with any pdf files. It seems to be trying to process the pdf but will time out before getting anywhere. Here’s the code I’m using:

The blobStream variable is reading a PDF from the Azure Blob Storage.

var viewStream = new MemoryStream();
using (Viewer viewer = new Viewer(blobStream))
            {
                HtmlViewOptions options = HtmlViewOptions.ForEmbeddedResources(
                    num => viewStream, (num, stream) => { });

                viewer.View(options);
            }

            ViewBag.viewer = viewStream;

@LBuchan1

Please follow Timeout when rendering PDF files from Azure Blob Storage topic.