Products Technologies Demo Docs Blog Support Company

Angular DocumentViewer: Loading Documents from an ASP.NET Core Backend Web API

TX Text Control DocumentViewer for Angular can be used to display documents in Angular applications. This article shows how to load documents from an ASP.NET Core API Controller.

Angular DocumentViewer: Loading Documents from an ASP.NET Core Backend Web API

Using the Angular version of the TX Text Control DocumentViewer, you can load documents by passing the document as a base64 encoded string to the documentData parameter. In real-world applications, you might want to load documents from a server-side endpoint.

This article explains how to load a document from a Web API running in an ASP.NET Core Web Application.

The API Controller

Consider the following simple controller implementation with the LoadDocument Http Get method:

[Route("api/[controller]")]
[ApiController]
public class ViewerController : ControllerBase
{
  [HttpGet]
  [Route("LoadDocument")]
  public LoadedDocument LoadDocument()
  {
    string documentName = "invoice.docx";

    LoadedDocument document = new LoadedDocument()
    {
      DocumentData = Convert.ToBase64String(
        System.IO.File.ReadAllBytes("App_Data/" + documentName)),
      DocumentName = documentName
    };

    return document;
  }
}

It simply returns an object of type LoadedDocument that contains the document as a base64 encoded string and the document name. The model object is defined as follows:

public class LoadedDocument
{
    public string DocumentData { get; set; }
    public string DocumentName { get; set; }
}

Angular TypeScript

In the Angular HomeComponent TypeScript, the endpoint is called in the constructor and the results are passed to the JavaScript function loadInitialDocument:

import { Component, Inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';

declare const loadDocument: any;
declare const loadInitialDocument: any;

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
})

export class HomeComponent {
  public _http: HttpClient;
  public _baseUrl: string;

  constructor(http: HttpClient, @Inject('BASE_URL') baseUrl: string) {
    this._http = http;
    this._baseUrl = baseUrl;

    // get a document from the Web API endpoint 'LoadDocument'
    this._http.get<any>(this._baseUrl + 'api/viewer/loaddocument').subscribe(result => {
      loadInitialDocument(result);
    }, error => console.error(error));
  }
}

In the JavaScript code, the event documentViewerLoaded is added which is fired when the DocumentViewer has been initialized completely. In the event callback, the loadDocument function is called that actually loads the document into the DocumentViewer:

function loadDocument(Document) {
  TXDocumentViewer.loadDocument(Document.documentData, Document.documentName);
}

var documentLoading;

function loadInitialDocument(Document) {

  documentLoading = true;

  window.addEventListener("documentViewerLoaded", function () {
    
    if (documentLoading === true)
      loadDocument(Document);

    documentLoading = false;

  });
}

Loading a Document on a Button Click

The above method shows how to initialize the DocumentViewer with a document. When a document should be loaded on another action, you can simply call the loadDocument method. The following code shows the home.component.html:

<tx-document-viewer width="800px"
                    height="800px"
                    basePath="https://localhost:44354"
                    dock="Fill"
                    toolbarDocked=true
                    isSelectionActivated=true
                    showThumbnailPane=true>
</tx-document-viewer>

<input (click)="onClickLoadDocument()" type="button" value="Load Document" />

The following code shows the home.component.ts implementation including the onClickLoadDocument function that calls the loadDocument JavaScript function directly:

import { Component, Inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';

declare const loadDocument: any;

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
})

export class HomeComponent {
  public _http: HttpClient;
  public _baseUrl: string;

  async onClickLoadDocument() {
    // get a document from the Web API endpoint 'LoadDocument'
    this._http.get<any>(this._baseUrl + 'api/viewer/loaddocument').subscribe(result => {
      loadDocument(result);
    }, error => console.error(error));
  }
}

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Angular

Integrate document processing, editing, sharing, collaboration, creation, electronic signatures, and PDF generation into your Angular Web applications.

Learn more about Angular

Related Posts

AngularASP.NETDocumentViewer

Preview: TX Text Control DocumentViewer Becomes a Web Component

The next version of the TX Text Control DocumentViewer for ASP.NET (Core) and Angular is becoming a Web Component that can be easily used directly in JavaScript. This article gives an overview of…


AngularASP.NETDocumentViewer

Electronic Signatures: Document Audit Trails

In order to implement a legally binding document signing process, a complete document audit trail must be recorded to verify every action taken on a document. This article shows how the eSign demo…


AngularASP.NETDocumentViewer

Combine Form Fields, Merge Fields and Signature Boxes to Request Signatures

The eSign Demo shows how to use TX Text Control to implement electronic signatures into your applications. This article shows how to combine form fields, merge fields and signature boxes to…


AngularASP.NETDocumentViewer

eSign Demo: Requesting Signatures from Multiple Signers

We just published an enhanced version of our electronic signature demo that supports the request of signatures from multiple signers. This article gives an overview of the new features.


AngularASP.NETCollaboration

DocumentViewer 29.2 (29.0.302.500) Final Released

We are happy to announce the final release of version 29.2 of the DocumentViewer for ASP.NET and ASP.NET Core. It comes with many new annotation and form field features to deploy documents for…