ASP.NET Core Document Viewer

The ASP.NET MVC TXTextControl.Web.MVC.DocumentViewer.DocumentViewer Class for .NET Core is typically used to display documents and resulting reports in ASP.NET Core web application. Documents can be loaded from files resides physically server-side, from data or from JavaScript.

The ASP.NET MVC DocumentViewer is a lightweight, cross-browser compatible, mobile-friendly viewer for ASP.NET Core. In comparison, our HTML5-based document editor is used to create and modify documents using an MS Word compatible interface and user experience. For pure viewing tasks, the editor is probably too powerful and the requirements for a viewer interface is totally different.

This tutorial shows how to integrate Web.DocumentViewer into an ASP.NET Core web application.

Create Your Application

  1. In Visual Studio 2019 (> 16.5.4), create a new project and select ASP.NET Core Web Application as the project template.

    image

  2. Select a project name (myDotnetCoreApp in this sample), location and solution name in the next dialog and confirm with Create.

    image

  3. In the last dialog, select .NET Core and ASP.NET Core 3.1 as the project target, select Web Application (Model-View-Controller) as the template and confirm with Create.

    image

  4. Open the Package Manager Console by choosing Package Manager Console from the Tools -> NuGet Package Manager main menu and type in the following command to install the TXTextControl.Web.DocumentViewer package:

    PM> Install-Package TXTextControl.Web.DocumentViewer

  5. While the project is selected in the Solution Explorer, choose Project -> Add Reference... to open the Reference Manager. In the opened dialog, select Browse... to select the required TX Text Control assemblies. Navigate to the installation folder of TX Text Control and select the following assemblies from the Assembly folder:

    • TXDocumentServer.dll
    • TXTextControl.dll
    • TXTextControl.Server.dll
    • TXDrawing.dll

    image

    Repeat this step with the following assemblies from the Assembly/bin64 folder:

    • txic.dll
    • txkernel.dll
    • txtools.dll
    • txpdf.dll

    After selecting these assemblies, close the Reference Manager by confirming with OK.

  6. While the project is selected in the Solution Explorer, choose Project -> Add Existing Item.... Browse to the TX Text Control installation folder and select the following files from the Assembly/bin64:

    • tx28_xml.dll
    • tx28_css.dll
    • tx28_doc.dll
    • tx28_dox.dll
    • tx28_htm.dll
    • tx28_pdf.dll
    • tx28_rtf.dll
    • tx28_xlx.dll
  7. Select the files from step 6 in the Solution Explorer and set the Copy to Output Directory to Copy always.

  8. While the project is selected in the Solution Explorer, choose Project -> Add New Item.... Select Text File, name the file licenses.licx and close the dialog by clicking Add.

    image

    Open the newly added file and add the following content:

    TXTextControl.ServerTextControl, TXTextControl.Server, Culture=neutral, PublicKeyToken=6b83fe9a75cfb638

    Set the Build Action property to Embedded Resource.

    In case, you are getting compile errors (MSB6003 The specified task executable "lc.exe" could not be run.), please refer to this article:

    LC Task in .NET Core Projects

  9. While the project is selected in the Solution Explorer, create a new folder and name it App_Data by choosing New Folder from the Project main menu.

    Select the App_Data folder and click Add Existing Item... from the Project main menu.

    Browse to the following TX Text Control installation folder:

    C:\Program Files\Text Control GmbH\TX Text Control 28.0.NET Server for ASP.NET\Samples\Demo\

    Select the file demo.rtf and confirm with Add.

  10. Open the file Startup.cs and insert the following code anywhere to the Configure method to enable the App_Data folder:

    string baseDir = env.ContentRootPath;
    AppDomain.CurrentDomain.SetData("DataDirectory", System.IO.Path.Combine(baseDir, "App_Data"));
  11. Open the Index.cshtml view from the Views -> Home folder and delete the pre-defined code and replace it with the following:

    @using TXTextControl.Web.MVC.DocumentViewer
    @using System.IO;
    
    @Html.TXTextControl().DocumentViewer(settings =>
    {
        settings.DocumentPath =
            AppDomain.CurrentDomain.GetData("DataDirectory").ToString() + "\demo.rtf ";
        settings.Dock = DocumentViewerSettings.DockStyle.Window;
    }).Render()

Compile and start your application by pressing F5.