Products Technologies Demo Docs Blog Support Company

Getting Started: Loading and Saving Documents using Angular

This tutorial shows how to load and save documents using the JavaScript API in Angular applications.

Getting Started: Loading and Saving Documents using Angular

In a previous tutorial, you learned how to create your first Angular application using the TX Text Control Document Editor.

This tutorial shows how to use the JavaScript API to load and save documents within an Angular application.

Creating the Application

  1. Create a basic Angular application as described in this tutorial:
    Getting Started: Document Editor with Angular CLI

  2. Create a folder named js in the folder src/assets/ and create a new JavaScript file named custom.js.

    function saveDocument() {
        // save document
        TXTextControl.saveDocument(TXTextControl.StreamType.AdobePDF, function (e) {
            // create temporary link element
            var element = document.createElement('a');
            element.setAttribute('href', 'data:application/octet-stream;base64,' + e.data);
            element.setAttribute('download', "results.pdf");
    
            element.style.display = 'none';
            document.body.appendChild(element);
    
            // simulate click
            element.click();
    
            // remove the link
            document.body.removeChild(element);
        });
    }
    
    function loadDocument() {
    
        const htmlDocument = "<p>Hello <strong>World!</strong></p>";
    
        TXTextControl.loadDocument(TXTextControl.StreamType.HTMLFormat,
            window.btoa(htmlDocument));
    }
  3. Add this JavaScript file to the scripts array of the projects architect/build/options element in the angular.json file:

    "scripts": [
      "src/assets/js/custom.js"
    ]
  4. Add the declaration and the methods onClickSave and onClickLoad to the app.component.ts TypeScript file, so that the complete file looks like this:

    import { Component } from '@angular/core';
    declare const saveDocument: any;
    declare const loadDocument: any;
    
    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    export class AppComponent {
      title = 'my-editor-app';
    
      onClickSave() {
        saveDocument();
      }
    
      onClickLoad() {
        loadDocument();
      }
    
    }
  5. Finally, add two buttons to the app.component.html with the Angular click handlers:

    <tx-document-editor
      width="1000px"
      height="500px"
      webSocketURL="wss://backend.textcontrol.com/TXWebSocket?access-token=yourtoken">
    </tx-document-editor>
    
    <div>
      <button (click)="onClickLoad()" class="btn btn-primary">Load Document</button>
      <button (click)="onClickSave()" class="btn btn-primary">Save Document</button>
    </div>

When executing the application, an HTML document is loaded into the editor when clicking the button Load Document and the button Save Document saves the content as an Adobe PDF document that is downloaded to the browser.

Loading and saving documents in Angular

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

AngularJavaScriptReporting

Getting Started: Angular Document Editor Attributes Explained

This article explains the possible attributes that can be used when initializing the TX Text Control Document Editor for Angular.


AngularJavaScriptReporting

Getting Started: Programming the Angular Document Editor using JavaScript

This tutorial shows how to use the JavaScript API to program the TX Text Control Angular Document Editor.


AngularJavaScriptReporting

Impressions from Web Developer Conference (WDC) in Hamburg

This week, we exhibited at Web Developer Conference (WDC) in Hamburg and presented the latest technology preview of our Angular and Node.js versions.


AngularJavaScriptReporting

Loading and Saving Documents using Angular

This tutorial shows how to load and save documents using the JavaScript API in Angular applications.


AngularJavaScriptReact

Technology Preview: Embeddable HTML Widget to integrate Document Editing to…

This technology preview shows an early version of an HTML widget that can be embedded into any HTML page.