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.
- ng new Timepicker
Open this project in Visual Studio Code and install Bootstrap by using the following command.
- npm install bootstrap –save
Now open styles.css file and add Bootstrap file reference. To add reference in styles.css file add this line.
- @import ‘~bootstrap/dist/css/bootstrap.min.css’;
Now install ngx-doc-viewer by using the following command.
- npm install ngx-doc-viewer –save
Now create a new component by using the following command.
- ng g c Docviewer
Now open docviewer.component.html file and add the following code,
- <div class=“row”>
- <div class=“col-sm-12 btn btn-primary”>
- How to Add a document Viewer in Angular 10
- </div>
- </div>
- <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,
- import { Component, OnInit } from ‘@angular/core’;
- @Component({
- selector: ‘app-docviewer’,
- templateUrl: ‘./docviewer.component.html’,
- styleUrls: [‘./docviewer.component.css’]
- })
- export class DocviewerComponent implements OnInit {
- viewer = ‘google’;
- selectedType = ‘docx’;
- DemoDoc=“http://www.africau.edu/images/default/sample.pdf”
- constructor() { }
- ngOnInit(): void {
- }
- }
Now open docviewer.component.css file and add the following code,
- :host {
- display: block;
- }
- p {
- font-family: Lato;
- }
Open app.moudle.ts file and add the following code:
- import { BrowserModule } from ‘@angular/platform-browser’;
- import { NgModule } from ‘@angular/core’;
- import { FormsModule } from ‘@angular/forms’;
- import { AppRoutingModule } from ‘./app-routing.module’;
- import { AppComponent } from ‘./app.component’;
- import { BsDatepickerModule } from ‘ngx-bootstrap/datepicker’;
- import { BrowserAnimationsModule } from ‘@angular/platform-browser/animations’;
- import { NgxDocViewerModule } from ‘ngx-doc-viewer’;
- import { DocviewerComponent } from ‘./docviewer/docviewer.component’;
- @NgModule({
- declarations: [
- AppComponent,
- DocviewerComponent
- ],
- imports: [
- BrowserModule,BrowserAnimationsModule,FormsModule,NgxDocViewerModule,
- AppRoutingModule
- ],
- providers: [DatePipe],
- bootstrap: [AppComponent]
- })
- export class AppModule { }
Now open app.component.html file and add the following code,
- <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.
- ng g c Docviewerdemo
Now open docviewerdemo.component.html file and add the following code,
- <div class=“row”>
- <div class=“col-sm-12 btn btn-primary”>
- How to Add a document Viewer in Angular 10
- </div>
- </div>
- <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,
- import { Component, OnInit } from ‘@angular/core’;
- @Component({
- selector: ‘app-docviewerdemo’,
- templateUrl: ‘./docviewerdemo.component.html’,
- styleUrls: [‘./docviewerdemo.component.css’]
- })
- export class DocviewerComponent implements OnInit {
- viewer = ‘google’;
- selectedType = ‘txt’;
- DemoDoc=“https://www.le.ac.uk/oerresources/bdra/html/resources/example.txt”
- constructor() { }
- ngOnInit(): void {
- }
- }
Now open app.component.html file and add the following code,
- <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.