Products Technologies Demo Docs Blog Support Company

DS Server: Using DocumentViewer with pure HTML and JavaScript

We provide Angular and .NET Core client-side libraries to use the DocumentViewer with DS Server, but it can be used with pure HTML and JavaScript as well. This article shows how to create a simple HTML based application.

DS Server: Using DocumentViewer with pure HTML and JavaScript

In order to use the DS Server DocumentViewer with pure HTML and JavaScript, an additional step is required to retrieve the OAuth access token from DS Server. This sample shows how to retrieve the token and how to initialize the viewer on a simple HTML page.

In order to run this application, a DS Server trial token is required. If you don't have a token yet, you can get your token here for free:

Trial Token

This sample implements a DocumentViewer JavaScript class that requests the access token in order to initialize the viewer:

var DocumentServices = (function (tx) {
        
        var m_clientID,
                m_clientSecret,
                m_serviceURL,
                m_containerID,
                m_accessToken,
                m_userNames = [ "unknown user" ];
                m_isSelectionActivated = true;
                m_showThumbnailPane = true;

        tx.DocumentViewer = {

                setUserNames: function (userNames) {
                        m_userNames = userNames;
                },

                setIsSelectionActivated: function (activated) {
                        m_isSelectionActivated = activated;
                },

                setShowThumbnailPane: function (show) {
                        m_showThumbnailPane = show;
                },

                init: function (clientID, clientSecret, serviceURL, containerID) { 
                        m_clientID = clientID;
                        m_clientSecret = clientSecret;
                        m_serviceURL = serviceURL;
                        m_containerID = containerID;

                        var script = document.createElement('script');

                        script.onload = function () {
                                        
                                TXDocumentViewer.init( {
                                        containerID: m_containerID,
                                        viewerSettings: {
                                                toolbarDocked: true,
                                                dock: "Fill",
                                                userNames: m_userNames,
                                                isSelectionActivated: m_isSelectionActivated,
                                                showThumbnailPane: m_showThumbnailPane,
                                                basePath: m_serviceURL + "/documentviewer",
                                                oauthSettings: {
                                                        accessToken: m_accessToken
                                                }
                                        }
                                });
                                
                        };

                        getAccessToken(function(token) {
                                m_accessToken = token;
                                script.src = m_serviceURL + 
                                        "/documentviewer/textcontrol/GetResource?Resource=minimized.tx-viewer.min.js&access_token=" +
                                        m_accessToken;
                                document.head.appendChild(script);
                        });
                },

        }

        function getAccessToken(callback) {

                $.ajax({
                        type: "POST",
                        headers: {
                                "Authorization": "Basic " + btoa(m_clientID + ":" + m_clientSecret)
                        },
                        url: m_serviceURL + "/OAuth/Token",
                        data: {
                                "grant_type": "client_credentials"
                        },
                        success: successFunc
                });

                function successFunc(data, status) {
                        if (typeof callback == "function") {          
                                callback(data.access_token);
                        }
                }
        }

        return tx;

} ( DocumentServices || {} ));

This JavaScript file is then referenced in the sample HTML. Additionally, a host element (txViewer) is created and the init() method is called with your trial token credentials:

<html>
    <head>
        <!-- jQuery CDN -->
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" 
                integrity="sha512-bLT0Qm9VnAYZDflyKcBaQ2gg0hSYNQrJ8RilYldYQ1FxQYoCLtUjuuRuZo+fjqhx/qtq/1itJ0C2ejDxltZVFg=="
                crossorigin="anonymous">
        </script>

        <!-- DocumentServices JavaScript -->
        <script src="DocumentServices.DocumentViewer.js"></script>
    </head>
    <body>

        <!-- Host container for the document viewer -->
        <div id="txViewer" style="width: 800px; height: 500px;"></div>

        <script>
            var clientID = "";
            var clientSecret = "";
            var serviceURL = "https://trial.dsserver.io";

            // initialize DocumentViewer
            DocumentServices.DocumentViewer.setUserNames(["user1"]);
            DocumentServices.DocumentViewer.setShowThumbnailPane(true);
            DocumentServices.DocumentViewer.init(clientID, clientSecret, serviceURL, "txViewer");
        </script>
            

    </body>
</html>

The function getAccessToken is requesting an access token from the OAuth endpoints of DS Server by exchanging the ClientId and ClientSecret for a valid OAuth access token:

function getAccessToken(callback) {
        
  $.ajax({
    type: "POST",
    headers: {
      "Authorization": "Basic " + btoa(m_clientID + ":" + m_clientSecret)
    },
    url: m_serviceURL + "/OAuth/Token",
    data: {
      "grant_type": "client_credentials"
    },
    success: successFunc
  });

  function successFunc(data, status) {
    if (typeof callback == "function") {              
      callback(data.access_token);
    }
  }
}

The returned access token is then used to inject the required DocumentViewer JavaScript in order to call the init() method of DocumentViewer:

var script = document.createElement('script');
        
script.onload = function () {

  TXDocumentViewer.init( {
    containerID: m_containerID,
    viewerSettings: {
      toolbarDocked: true,
      dock: "Fill",
      userNames: m_userNames,
      isSelectionActivated: m_isSelectionActivated,
      showThumbnailPane: m_showThumbnailPane,
      basePath: m_serviceURL + "/documentviewer",
      oauthSettings: {
        accessToken: m_accessToken
      }
    }
  });

};

getAccessToken(function(token) {
  m_accessToken = token;
  script.src = m_serviceURL + 
    "/documentviewer/textcontrol/GetResource?Resource=minimized.tx-viewer.min.js&access_token=" +
    m_accessToken;
  document.head.appendChild(script);
});

This initializes the DocumentViewer on the HTML page. The loadDocument method of the JavaScript API can be used to load a document into the created DocumentViewer.

You can test this on your own by downloading the full sample from our GitHub repository.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

GitHub

Download and Fork This Sample on GitHub

We proudly host our sample code on github.com/TextControl.

Please fork and contribute.

Download ZIP

Open on GitHub

Open in Visual Studio

Requirements for this sample

  • DS Server Trial Token or Trial Version

Related Posts

AngularASP.NETJavaScript

JavaScript: Avoid Flickering and Visual Updates by Grouping Undo Steps

The JavaScript API can be used to group several steps into one undo action that can be undone with a single undo call. Additionally, those groups are visually updated at once to avoid screen…


ASP.NETJavaScriptDeployment

DS Server or TX Text Control? Different Deployment Scenarios

The Text Control document processing technology can be deployed in various ways using different products for different applications. This overview explains the differences and deployment strategies.


ASP.NETJavaScriptDS Server

Document Editor: JavaScript Object Availability and Order of Events

The online document editor, available for MVC and Angular, provides a JavaScript API to manipulate the document and the style and behavior of the editor. This article explains the order of events…


ASP.NETJavaScriptAnnotations

Deploying Documents with Annotations

The DocumentViewer allows to add and share annotations from different users to any type of document supported by TX Text Control including PDF, DOC, DOCX, RTF and the internal TX Text Control…


ASP.NETBlazorJavaScript

Using TX Text Control .NET Server in Blazor Server Apps

The TX Text Control document editor and the server-side non-UI component ServerTextControl can be used in Blazor Server Apps. This article shows how to save documents to the server and how to…