Products Technologies Demo Docs Blog Support Company

MVC DocumentViewer: Loading Documents

The TX Text Control MVC DocumentViewer supports three document loading methods: from server-side files via the DocumentPath property, from Base64-encoded data using the DocumentData property with ServerTextControl format conversion, or dynamically from JavaScript through Ajax.

MVC DocumentViewer: Loading Documents

The MVC DocumentViewer is typically used to display documents and resulting reports in web applications. Documents can be loaded from files resides physically server-side, from data or from JavaScript.

This article describes these three different ways to load documents into the DocumentViewer.

  1. Loading documents from files

    Documents that resides physically on the server can be loaded directly from the HTML Helper view code. In order to load a document from a physical file, the DocumentPath property must point to a supported file type on the server.

    @Html.TXTextControl().DocumentViewer(settings =>
    {
       settings.DocumentPath = Server.MapPath("~/App_Data/Documents/car_insurance.tx");
       settings.Dock = DocumentViewerSettings.DockStyle.Fill;
       settings.IsSelectionActivated = true;
       settings.ShowThumbnailPane = true;
    }).Render()

    See this live

  2. Loading documents from data

    In order to load a document from data, the DocumentData property must retrieve a Base64 encoded string in the internal TX Text Control format. If you want to load a document from another format such as DOCX, the document must be converted first using the ServerTextControl. Typically, the document is loaded in the Controller and passed to the View as a Model.

    If a document is loaded from data, the DocumentPath property can contain a filename which is displayed in the status bar.

    @Html.TXTextControl().DocumentViewer(settings =>
    {
        settings.DocumentData = Convert.ToBase64String(
          File.ReadAllBytes(Server.MapPath("~/App_Data/Documents/car_insurance.tx")));
        settings.DocumentPath = "MyDocument.tx";
        settings.Dock = DocumentViewerSettings.DockStyle.Fill;
        settings.IsSelectionActivated = true;
        settings.ShowThumbnailPane = true;
    }).Render()

    See this live

  3. Loading documents from JavaScript

    Documents can be loaded from client-side JavaScript as well. This is typically used when documents are asynchronously created server-side using MailMerge and then loaded into the DocumentViewer.

    This code shows how to load a document using Ajax from an HttpGet method of the Controller in order to load the results into the viewer.

    View (HTML Helper)

    @Html.TXTextControl().DocumentViewer(settings =>
    {
        settings.Dock = DocumentViewerSettings.DockStyle.Fill;
        settings.IsSelectionActivated = true;
        settings.ShowThumbnailPane = true;
    }).Render()
    
    <button onclick="LoadDocument('car_insurance.tx')" 
        class="btn mt-3 btn-success">Load Document</button>

    Controller

    [HttpGet]
    public string LoadDocument(string filename)
    {
        string sDocument = Convert.ToBase64String(
            System.IO.File.ReadAllBytes(
                Server.MapPath("~/App_Data/Documents/" + filename)));           
    
        return sDocument;
    }

    View (JavaScript)

    function LoadDocument(filename) {
         $('.alert').show();
    
        var bDocument;
        var serviceURL = "@Url.Action("LoadDocument", "TX")";
    
        // send document to controller
        $.ajax({
            type: "GET",
            url: serviceURL,
            data: {
                filename: filename
            },
            success: successFunc,
            error: errorFunc
        });
    }
    
    function successFunc(data, status) {
        TXDocumentViewer.loadDocument(data, "loaded.tx");
        TXDocumentViewer.toggleSidebar();
    
        $('.alert').hide();
    }
    
    function errorFunc() {
        alert("Error");
    }

    See this live

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

ASP.NET

Integrate document processing into your applications to create documents such as PDFs and MS Word documents, including client-side document editing, viewing, and electronic signatures.

ASP.NET Core
Angular
Blazor
JavaScript
React
  • Angular
  • Blazor
  • React
  • JavaScript
  • ASP.NET MVC, ASP.NET Core, and WebForms

Learn more Trial token Download trial

Related Posts

ASP.NETDocument ViewerMVC

Configuring ASP.NET and IIS for Larger Requests

The session-less MVC DocumentViewer sends documents via POST, requiring three web.config changes for large files: IIS maxAllowedContentLength for byte limits, httpRuntime maxRequestLength for…


ASP.NETDocument ViewerMVC

Updated MVC DocumentViewer: Session-less and Improved Image Quality

The ASP.NET MVC DocumentViewer 27.0.8.500 removes the InProc session state dependency, supporting session-less and SQL session state deployments. The update also improves page rendering quality to…


ASP.NETDocument ViewerElectronic Signature

Using Multiple Electronic Signatures on a Document

The MVC DocumentViewer supports multi-signer electronic signature workflows where signers sequentially apply signatures to designated fields. A JSON-based workflow defines signer roles and field…


ASP.NETReportingDocument Viewer

MVC DocumentViewer Update: Printing, Resources and Mobile-Friendly Document…

The ASP.NET MVC DocumentViewer 27.0.5.500 resolves the Chrome 77 print dialog issue, adds ResourceManager support for localization, and enables mobile-friendly signature capture. The update…


ASP.NETJavaScriptDocument Viewer

Using the ASP.NET MVC DocumentViewer JavaScript API

The TX Text Control MVC DocumentViewer exposes a client-side JavaScript API through the TXDocumentViewer object. Methods include toggleToolbar, toggleSidebar, toggleSelection, find, scrollToPage,…

Share on this blog post on: