username avatar
Kirti Kulkarni

Jun 28, 2022Beginner-12 min

5
12

Work with PDF's in ABP Commercial Project using PDFTron

In this short article we will integrate PDFTron in an ABP commercial Angular application. PDFTron enables embedding of PDFs and document viewing experience in web applications and also mobile, and desktop applications.

It supports viewing PDF, MS Office, video, images, HTML and several other file formats. The document viewer can be customized based on your branding requirements and users can stay in your application itself to view, search, and reference documents in PDF.

Let’s get started with creating an Angular ABP application first.

Setting up the PDFTron Integration

Create an ABP Commercial Angular application using the ABP CLI and ABP Suite. Once the application is created, follow standard steps mentioned here to run the application locally. https://docs.abp.io/en/commercial/latest/getting-started?UI=NG&DB=EF&Tiered=No

1. Open the PdfTron website

2. Click on Developers menu at the top and then click Download center - SDK downloads and download sdk zip for WebView.

PDF TronPdf Tron Download Center

3. Create a new folder wv-resources inside the angular folder of your ABP application app/src.

Create File

4. Create another new folder webviewer inside the folder path /src/app.

folderWebviewer

5. Extract downloaded zip into wv-resources

6. Delete all the extracted files except lib folder

7. Open the angular.json file in the application and under assets add src/wv-resources/lib and under scripts add src/wv-resources/lib/webviewer.min.js.

8. Add webviewer.component.html , component.css, webviewer.component.ts files in the webviewer folder

9. Copy the following code into the component.html file


    <!-- webviewer.component.html -->
    <!-- Simple DOM element for WebViewer to populate -->
    <div className="page">
    <div className="header">WebViewer</div>
    <div #viewer className="viewer"></div>
    </div> 

10. Now copy the following code in webviewer.component.css file


    /* webviewer.component.css */
    /* Change this to suit your viewing needs*/
.page {
    display: flex;
    flex-direction: column;
    width: 100%;
    height: 100%;
}
.header {
    height: 60px;
    padding: 8px 8px 8px 16px;
    background: #00a5e4;
    box-sizing: border-box;
    font-size: 1.2em;
    line-height: 44px;
    color: white;
}
app-webviewer {
    flex: 1;
    margin: 8px;
    -webkit-box-shadow: 1px 1px 10px #999;
            box-shadow: 1px 1px 10px #999;
}
.viewer { width: 100%; height: 600px; }

11. Now copy following code in the webviewer.component.ts file


import { Component, ViewChild, OnInit, ElementRef, AfterViewInit } from '@angular/core';

declare const WebViewer: any;

@Component({
  selector: 'web-viewer',
  templateUrl: './webviewer.component.html',
  styleUrls: ['./webviewer.component.css']
})
export class WebViewerComponent implements OnInit, AfterViewInit {
  // Syntax if using Angular 8+
  // true or false depending on code
  @ViewChild('viewer', {static: true / false}) viewer: ElementRef;

  // Syntax if using Angular 7 and below
  @ViewChild('viewer') viewer: ElementRef;

  wvInstance: any;

  ngOnInit() {
    this.wvDocumentLoadedHandler = this.wvDocumentLoadedHandler.bind(this);
  }

  wvDocumentLoadedHandler(): void {
    // you can access docViewer object for low-level APIs
    const { documentViewer, annotationManager, Annotations } = this.wvInstance.Core;
    // and access classes defined in the WebViewer iframe
    const rectangle = new Annotations.RectangleAnnotation();
    rectangle.PageNumber = 1;
    rectangle.X = 100;
    rectangle.Y = 100;
    rectangle.Width = 250;
    rectangle.Height = 250;
    rectangle.StrokeThickness = 5;
    rectangle.Author = annotationManager.getCurrentUser();
    annotationManager.addAnnotation(rectangle);
    annotationManager.drawAnnotations(rectangle.PageNumber);
    // see https://www.pdftron.com/api/web/WebViewerInstance.html for the full list of low-level APIs
  }

  ngAfterViewInit(): void {
    // The following code initiates a new instance of WebViewer.

    WebViewer({
      path: '../../wv-resources/lib',
      initialDoc: 'https://pdftron.s3.amazonaws.com/downloads/pl/webviewer-demo.pdf'
    }, this.viewer.nativeElement).then(instance => {
      this.wvInstance = instance;

      // now you can access APIs through this.webviewer.getInstance()
      instance.UI.openElement('notesPanel');
      // see https://www.pdftron.com/documentation/web/guides/ui/apis
      // for the full list of APIs

      // or listen to events from the viewer element
      this.viewer.nativeElement.addEventListener('pageChanged', (e) => {
        const [ pageNumber ] = e.detail;
        console.log(Current page is {pageNumber});
      });

      // or from the docViewer instance
      instance.Core.documentViewer.addEventListener('annotationsLoaded', () => {
        console.log('annotations loaded');
      });

      instance.Core.documentViewer.addEventListener('documentLoaded', this.wvDocumentLoadedHandler)
    })
  }
}      

12. Now Import the module under component and register under declaration

13. Insert selector in html component

14. If your Angular application is already running, you should see the theme change right away.

Otherwise execute npm start again, then navigate to http://localhost:4200 and you should see that the theme has changed.

Welcome To My App

15. For file picker inside WebViewerComponent add enableFilePicker: true,

Featured Comments
username avatar
Joe ThomsonToday at 5:42PM

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor

username avatar
Joe ThomsonToday at 5:42PM

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor

username avatar
Joe ThomsonToday at 5:42PM

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor

username avatar
Kirti Kulkarni

ABOUT THE AUTHOR

With over 20 years of experience in software development, Kirti heads Product R&D and Competency Management at WAi Technologies, leading the training and skills upgradation program at WAi. Kirti introduced the 'Women Back To Work' Initiative that encourages women to return back to mainstream software development after a career break or sabbatical.