In this article, we will learn how to create a document viewer in an Angular 10 application. We can show many different document formats in Angular applications.

Create an Angular project by using the following command.
  1. ng new Timepicker

Open this project in Visual Studio Code and install Bootstrap by using the following command.

  1. npm install bootstrap –save

Now open styles.css file and add Bootstrap file reference. To add reference in styles.css file add this line.

  1. @import ‘~bootstrap/dist/css/bootstrap.min.css’;

Now install ngx-doc-viewer by using the following command.

  1. npm install ngx-doc-viewer –save
 Now create a new component by using the following command.
  1. ng g c Docviewer
 Now open docviewer.component.html file and add the following code,
  1. <div class=“row”>
  2.     <div class=“col-sm-12 btn btn-primary”>
  3.         How to Add a document Viewer in Angular 10
  4.     </div>
  5.   </div>
  6. <ngx-doc-viewer [url]=“DemoDoc” [viewer]=“viewer” style=“width:100%;height:100vh;”></ngx-doc-viewer>

Now open docviewer.component.ts file and add the following code,

  1. import { Component, OnInit } from ‘@angular/core’;
  2. @Component({
  3.   selector: ‘app-docviewer’,
  4.   templateUrl: ‘./docviewer.component.html’,
  5.   styleUrls: [‘./docviewer.component.css’]
  6. })
  7. export class DocviewerComponent implements OnInit {
  8.   viewer = ‘google’;
  9.   selectedType = ‘docx’;
  10.   DemoDoc=“http://www.africau.edu/images/default/sample.pdf”
  11.   constructor() { }
  12.   ngOnInit(): void {
  13.   }
  14. }
Now open docviewer.component.css file and add the following code,
  1. :host {
  2.     display: block;
  3.   }
  4.   p {
  5.     font-family: Lato;
  6.   }
Open app.moudle.ts file and add the following code:
  1. import { BrowserModule } from ‘@angular/platform-browser’;
  2. import { NgModule } from ‘@angular/core’;
  3. import { FormsModule } from ‘@angular/forms’;
  4. import { AppRoutingModule } from ‘./app-routing.module’;
  5. import { AppComponent } from ‘./app.component’;
  6. import { BsDatepickerModule } from ‘ngx-bootstrap/datepicker’;
  7. import { BrowserAnimationsModule } from ‘@angular/platform-browser/animations’;
  8. import { NgxDocViewerModule } from ‘ngx-doc-viewer’;
  9. import { DocviewerComponent } from ‘./docviewer/docviewer.component’;
  10. @NgModule({
  11.   declarations: [
  12.     AppComponent,
  13.     DocviewerComponent
  14.   ],
  15.   imports: [
  16.     BrowserModule,BrowserAnimationsModule,FormsModule,NgxDocViewerModule,
  17.     AppRoutingModule
  18.   ],
  19.   providers: [DatePipe],
  20.   bootstrap: [AppComponent]
  21. })
  22. export class AppModule { }
 Now open app.component.html file and add the following code,
  1. <app-docviewer></app-docviewer>
 Now run the project by using the following command: ‘npm start’
We can view many different document formats using this library. Create  another component and check another file format. Create a new component using the following command.
  1. ng g c Docviewerdemo

Now open docviewerdemo.component.html file and add the following code,

  1. <div class=“row”>
  2.     <div class=“col-sm-12 btn btn-primary”>
  3.         How to Add a document Viewer in Angular 10
  4.     </div>
  5.   </div>
  6. <ngx-doc-viewer [url]=“DemoDoc” [viewer]=“viewer” style=“width:100%;height:90vh;”></ngx-doc-viewer>
  Now open docviewerdemo.component.ts file and add the following code,
  1. import { Component, OnInit } from ‘@angular/core’;
  2. @Component({
  3.   selector: ‘app-docviewerdemo’,
  4.   templateUrl: ‘./docviewerdemo.component.html’,
  5.   styleUrls: [‘./docviewerdemo.component.css’]
  6. })
  7. export class DocviewerComponent implements OnInit {
  8.   viewer = ‘google’;
  9.   selectedType = ‘txt’;
  10.   DemoDoc=“https://www.le.ac.uk/oerresources/bdra/html/resources/example.txt”
  11.   constructor() { }
  12.   ngOnInit(): void {
  13.   }
  14. }
Now open app.component.html file and add the following code,
  1. <app-docviewerdemo></app-docviewerdemo>
Now run the project by using the following command: ‘npm start’
 

Summary

In this article, we learned how to add document viewer in Angular 10 applications.
error: Content is protected !!