ASP.NET Word Viewer Control
How to view Word document in browser without Office, interop in ASP.NET Core MVC


How to read, view, edit Microsoft Office Word in web browser using C# ASP.NET MVC PDF Viewer control.





EdgePDF is an ASP.NET PDF viewer and editor web control. You can also view Microsoft Office Word file in web browser with EdgePDF using C# ASP.NET

  • Open, read a Word document from server network file system
  • EdgePDF will automatically convert loaded Word to PDF document in ASP.NET Core or ASP.NET web app
  • Converted PDF with bookmark generated automatically. Or customize the PDF bookmark by Word outline levels
  • View, comment Word file online in web browser

How to view a Word file programmatically in client browser in asp.net using C#

  1. Download EdgePDF asp.net PDF viewer web control
  2. Install EdgePDF demo project in IIS
  3. Follow step by step tutorial






Preparation



To run the following tutorial successfully, we need the following setup ready.



  1. For ASP.NET Core web app: Setup EdgePDF
  2. For ASP.NET Core MVC web app: Setup EdgePDF
  3. For ASP.NET (.net framework): Setup EdgePDF on IIS
  4. A demo Office Word document in folder C:\temp\word-1.docx




How to open, view Word document in ASP.NET Core or ASP.NET MVC?



The following steps and C# demo source code will help to setup a demo ASP.NET Core or ASP.NET web app, which allows you to read, view a Microsoft Word document in web browser using ASP.NET C# code.

After you have completed the following guide, you can open, view a Office Word file online in web browser through url (a sample url http://localhost:56643/?yourtarget=word-1.docx)



  • For ASP.NET Core web app, open file UserCommandProcessMiddleware.cs from /DemoProjects/EdgePDF for ASP.NET Core/

  • For ASP.NET (.net framework) project, open file UserCommandProcessHandler.ashx from {EdgePDF demo project}/RasterEdge_Resource_Files/

  • Go to method FileProcess()
  • Create a new byte[] object with viewed Word file in byte array
  • Call method REProcessControl.PageLoadFile() to load, view Word file in EdgePDF. The Word file (word-1.docx) has been converted into PDF file with bookmarks and rendered in EdgePDF online in web browser


    public override PDFWebDocument FileProcess()
    {
        HttpRequest request = this.Context.Request;

        if (!String.IsNullOrEmpty(request.QueryString["yourtarget"]))
        {
            String docid = request.QueryString["yourtarget"];

            byte[] dataBytes = System.IO.File.ReadAllBytes(@"C:\\temp\" + docid);
                
            return REProcessControl.PageLoadFile(request, this.Manager, dataBytes, docid);
        }
        else
        {
            Logger.Log(">>> Unknown load file mode. Load default file defined in Web.config.", LogType.DEBUG);

            // load default file. defined in Web.config
            return REProcessControl.PageLoadFile(request, this.Manager, "", LoadType.Server);
        }
    }







How to view Word with customized bookmark online in web browser in ASP.NET C#?



The following steps and C# demo source code will show how to generate customized bookmark for Word document web browser using ASP.NET C# code.

The C# code below will change the Word document outline level to 1, which means only H1 contents will be generated in the PDF bookmark. Go to page How to convert Word to PDF with bookmark using C# for more information about outline settings.



  • For ASP.NET Core web app, open file UserCommandProcessMiddleware.cs from /DemoProjects/EdgePDF for ASP.NET Core/

  • For ASP.NET (.net framework) web application, open file UserCommandProcessHandler.ashx from {EdgePDF demo project}/RasterEdge_Resource_Files/

  • Go to method FileProcess()
  • Create a new DOCXDocument object with viewed Word document loaded
  • Set Word document object outline level to 1. Only H1 text content will be generated in the bookmark.
  • Call method DOCXDocument.ConvertToDocument() to convert docx file to PDF document with bookmark generated
  • Call method REProcessControl.PageLoadFile() to load, view Word file in EdgePDF.


    public override PDFWebDocument FileProcess()
    {
        HttpRequest request = this.Context.Request;

        if (!String.IsNullOrEmpty(request.QueryString["yourtarget"]))
        {
            String docid = request.QueryString["yourtarget"];

            DOCXDocument doc = new DOCXDocument(@"C:\\temp\" + docid);
            //  Set strategy used to create outline entries for the document.
            //  Default: OutlineMode.ByOutlineLevel
            doc.OutlineSetting.Mode = OutlineMode.ByHeading;
            //  Set the maximum level of entires in the outline.
            //  Valid range: 1 ~ 9; default value: 3
            doc.OutlineSetting.MaxLevel = 1;

            MemoryStream pdfDoc = new MemoryStream();
            //  Convert DOCX document to PDF file.
            doc.ConvertToDocument(DocumentType.PDF, pdfDoc);

            return REProcessControl.PageLoadFile(request, this.Manager, pdfDoc, docid);
        }
        else
        {
            Logger.Log(">>> Unknown load file mode. Load default file defined in Web.config.", LogType.DEBUG);

            // load default file. defined in Web.config
            return REProcessControl.PageLoadFile(request, this.Manager, "", LoadType.Server);
        }
    }







How to deploy ASP.NET Word viewer web application to Azure web service?



After complete the ASP.NET Word viewer web application, you could easily publish the ASP.NET MVC web app to Azure cloud service. View details at How to deploy ASP.NET Core Word viewer web application to Azure?