Learn MVC : Using Angular PDF File Viewer

Introduction

This article demonstrates how to use Angular PDF File Viewer in Visual Studio 2017.

Angular PDF File Viewer

Angular Portable Document Format (PDF) File Viewer that is built with HTML 5 and supported by Mozilla Labs. It is a web standard based platform for parsing and rendering PDFs.

Features

The delegate service injects this Controller, so you can fetch an instance using its delegate handle and call methods.

The following methods are available to the delegate.

  • Prev
  • Next
  • Zoom In
  • Zoom Out
  • Zoom To
  • Rotate
  • Page Count
  • Goto Page
  • Load

Following the below steps you can use Angular File Upload in Angular JS in MVC.

  • Create MVC Project.
  • Configure Angular PDF File Viewer.
  • Work in Angular PDF File Viewer.

Create MVC Project

Open Visual Studio 2017.

MVC

Go to New >> New project. Now it will open "New Project" window.

MVC

You can select ASP.NET Web Application on Framework 4.5. Enter the name of the project in “Solution name” textbox and click OK button.

MVC

One more window should be appearing. Select MVC Template in this popup and click OK button. Now, start cropping the image.

Configure Angular PDF File Viewer

You can download the plug-in from.

Open the _Layout.cshtml and must refer the .js file from downloaded folder to this view page.

  1. <link href="~/Content/Site.css" rel="stylesheet" type="text/css" />  
  2.  <link href="~/PlugIn/AngularPdf/basscss.min.css" rel="stylesheet" />  
  3.  <link href="~/Content/bootstrap.min.css" rel="stylesheet" type="text/css" />  
  4.  <script src="~/PlugIn/AngularPdf/pdf.combined.js"></script>  
  5.  <script src="~/Scripts/jquery-1.10.2.min.js"></script>  
  6.  <script src="~/Scripts/bootstrap.min.js"></script>  
  7.  <script src="~/PlugIn/angular/angular.min.js"></script>  
  8.  <script src="~/PlugIn/AngularPdf/angular-pdf-viewer.min.js"></script>  
  9.  <script src="~/PlugIn/angular-ui-router/release/angular-ui-router.js"></script>  

Link your Angular configurable file.

  1. <script src="~/App/App.module.js"></script>  
  2. <script src="~/App/App.config.js"></script>  
  3. <script src="~/App/PDFController.js"></script>  

Angular Module

You need to include the module as a dependency on your application.

  1. var PDf = angular  
  2. .module('PDf', ['ui.router''pdf']);   

If you have any doubt in configuration, visit the following of my articles -

Work in Angular PDF File Viewer

“PDF” is my Angular module's name, so I have added the “ng-app”.

  1. <div class="container body-content" ng-app="PDf">  
  2.    @RenderBody()  
  3. </div>   

Html Code 

  1. <div class="jumbotron text-center">  
  2.     <h1>Angular PDF Viewer</h1>  
  3. </div>  
  4. <div class="row" ng-controller="PdfController">  
  5.     <div class="col-md-2">  
  6.     </div>  
  7.     <div class="col-md-8">  
  8.         <pdf-viewer delegate-handle="relativity-special-general-theory"  
  9.                     url="pdfUrl"  
  10.                     scale="1"  
  11.                     show-toolbar="true"></pdf-viewer>  
  12.     </div>  
  13.     <div class="col-md-2">  
  14.     </div>  
  15. </div>   

The directive <pdf-viewer> can manage the URL, scale Request Header, and delegate-handler can be set using the attributes. The pdfDelegate delegate service allows you to access and control individual of a directive. This allows us to have multiple instances of the same directive in the same Controller.

Angular Controller

Initiate the File to object. This object will be loaded from local or URL. Create one folder in Solution Explorer then keep it all the PDF or you can use Angular service for URL, getting from Database.

MVC

  1. PDf.controller('PdfController'function ($scope, pdfDelegate, $log, $location) {  
  2.   
  3. $scope.pdfUrl = $location.$$protocol + '://'+$location.$$host+':'+$location.$$port+'/pdf/example.pdf';  
  4. });   

Click F5 button to run the application. Now, it will appear in browser and see the result.

Output 1

MVC

PDF is loaded in the same HTML page with tool bar.

Output 2

MVC

The default toolbar can be shown or hidden using the show-toolbar attribute. If needed, you can place the toolbar on a separate $scope.

If you have any queries, please tell me through the comments section. 

Happy Coding!...

Up Next
    Ebook Download
    View all
    Learn
    View all