Products Technologies Demo Docs Blog Support Company

Angular: Loading Documents from an ASP.NET Core Backend

This sample shows how to load documents into the Angular document editor from an ASP.NET Core backend application using Web API Http requests.

Angular: Loading Documents from an ASP.NET Core Backend

Using the Angular version of the TX Text Control DocumentEditor, documents can be loaded using the JavaScript API by passing the document as a base64 encoded string. In real-world applications, you might want to load documents from a server-side Web API endpoint.

This sample application uses an ASP.NET Core backend project to provide the WebSocketHandler that also exposes a Web API to load documents from a server.

Web API Controller

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

[ApiController]
[Route("api/[controller]")]
public class TextControlController : ControllerBase
{
  [HttpGet]
  [Route("LoadDocument")]
  public TextControlDocument LoadDocument(string DocumentName) {
     using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl()) {
        tx.Create();
        tx.Load("App_Data/" + DocumentName, TXTextControl.StreamType.WordprocessingML);

        byte[] data;

        tx.Save(out data, TXTextControl.BinaryStreamType.InternalUnicodeFormat);

        return new TextControlDocument() { Document = Convert.ToBase64String(data) };
     }
  }

}

The method simply loads a document given by it's name into a new instance of the ServerTextControl in order to return the document in the internal Text Control format. Technically, the document editor is able to load all supported document types, but this way, we don't have to check the used StreamType and potentially required document checks can be performed completed server-side.

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

public class TextControlDocument {
  public string Document { get; set; }
}

Angular TypeScript

The view consists of a Text Control editor and a button to load the document:

<tx-document-editor width="1000px"
                    height="500px"
                    webSocketURL="ws://localhost:32760/">
</tx-document-editor>

<input (click)="onClickLoadDocument('demo.docx')" type="button" value="Load Document" />

The following code shows the app.component.ts implementation including the onClickLoadDocument function that calls the loadDocument JavaScript function directly. The function onClickLoadDocument executes an Http GET method on our Web API controller method LoadDocument to retrieve the document:

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

declare const loadDocument: any;

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

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

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

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

The JavaScript function loadDocument simply loads the given document:

function loadDocument(document) {
  TXTextControl.loadDocument(TXTextControl.streamType.InternalUnicodeFormat, document.document);
}

Loading documents into TX Text Control

You can test this on your own by cloning or downloading the 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

  • TX Text Control .NET Server
  • Visual Studio 2019

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.NETDocumentEditor

Announcing New Signature Features: Initials and SVG

We are working on new DocumentViewer features for electronic signatures. The next version supports initials and SVG signatures for better export quality.


AngularASP.NETDocumentEditor

Document Editor: Implementing a Contextual Toolbar

Contextual toolbars can be used to provide users quick access to the most common functions and settings. This article shows how to implement a contextual toolbar to manipulate form fields in a…


AngularASP.NETBlazor

Building an ASP.NET Core Backend (Linux and Windows) for the Document Editor…

This article shows how to create a backend for the Document Editor and Viewer using ASP.NET Core. The backend can be hosted on Windows and Linux and can be used in Blazor, Angular, JavaScript, and…


AngularASP.NETASP.NET Core

Impressions from .NET Developer Conference DDC 2024

This week we sponsored the .NET Developer Conference DDC 2024 in Cologne, Germany. It was a great event with many interesting talks and workshops. Here are some impressions of the conference.


AngularASP.NETASP.NET Core

Back from Florida: Impressions from VSLive! Orlando 2024

We had an incredible time showcasing our digital document processing SDKs at VSLive! Orlando 2024 as a silver sponsor! Throughout the event, we interacted with hundreds of developers, shared…