翻譯|使用教程|編輯:龔雪|2024-04-30 10:56:30.947|閱讀 97 次
概述:本文將為大家介紹如何使用Kendo UI for Angular PDF Viewer創建一個強大的閱讀器,歡迎下載新版體驗~
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
如今當用戶需要處理PDF文件時,通常不得不下載應用程序或者瀏覽器插件,控制用戶如何與PDF交互并不是一件容易的事。如果我們提供PDF作為內容,用戶可以下載它并使用瀏覽器或PDF本身提供的控件進行交互。然而,一些企業可能希望控制用戶使用PDF的方式,以提供更好的體驗或在某些條件下限制下載。
構建這樣的解決方案需要在后端和前端都付出巨大的努力,然而如果有一種方法可以讓您在Angular PDF Viewer中用幾行代碼來管理PDF交互呢?
Kendo UI for Angular PDFViewer可以幫助大家解決上述的一些問題,它以最佳的方式應用到每個合適的場景中。
P.S:Kendo UI for Angular是專用于Angular開發的專業級Angular組件,這些組件是專門為Angular構建的,沒有任何jQuery依賴項。
Kendo UI for Angular 2024 Q1新版下載
為一所大學開發一個應用程序,管理部門希望為學生提供以下功能:
為了滿足大學的需求,我們使用Kendo UI for Angular PDFViewer,這個組件提供了大量的功能,當與Angular集成時可提供一個全面的解決方案。
首先,用命令ng new elearning-platform設置Angular應用。
ng new elearning-platform
cd elearning-platform
npm install
Kendo UI提供了一個schematics命令來注冊它的Angular PDF Viewer。
ng add @progress/kendo-angular-pdfviewer
i Using package manager: npm
√ Found compatible package version: @progress/kendo-angular-pdfviewer@14.0.0.
√ Package information loaded.
The package @progress/kendo-angular-pdfviewer@14.0.0 will be installed and executed.
Would you like to proceed? Yes
√ Packages successfully installed.
UPDATE src/app/app.module.ts (515 bytes)
UPDATE package.json (1708 bytes)
UPDATE angular.json (3165 bytes)
√ Packages installed successfully.
UPDATE src/main.ts (259 bytes)
UPDATE tsconfig.app.json (294 bytes)
UPDATE tsconfig.spec.json (300 bytes)
我們已經設置好了,現在開始為用戶和PDF Viewer定義布局和界面。
首先從app.component.html中刪除默認的HTML,添加以下HTML元素:
<h1>Welcome to E-learning Platform</h1> <h2>You can read online and save the state, also download the book (if you agree with the terms)</h2> <select> <option value="angular.pdf">Angular</option> <option value="signals.pdf">Signals</option> </select> <label for="acceptTerms"> Do you agree with the terms of download? </label> <input id="acceptTerms" type="checkbox" />
要添加kendo-pdfviewer和“paywall”橫幅,請在導入部分導入PDFViewerModule模塊。
import { Component } from '@angular/core'; import { CommonModule } from '@angular/common'; import { RouterOutlet } from '@angular/router'; import {PDFViewerModule} from "@progress/kendo-angular-pdfviewer"; @Component({ selector: 'app-root', standalone: true, imports: [CommonModule, RouterOutlet, PDFViewerModule], templateUrl: './app.component.html', styleUrl: './app.component.css' }) export class AppComponent { title = 'elearning-platform'; }
接下來,添加kendo-pdfviewer和pay-wall元素,這些元素應該只在用戶從下拉列表中選擇一個選項時出現。為了簡化,將它們封裝在一個ng容器中。
<ng-container> <kendo-pdfviewer > </kendo-pdfviewer> <div class="pay-wall"> <h1>You reach limit to read </h1> <button>Close</button> </div> </ng-container>
保存后,布局應該是這樣的:
我們現在有了一個沒有任何交互的布局,在繼續之前先在assets目錄中創建兩個PDF文件——名稱與下拉菜單中顯示的一致(angular.pdf和signals.pdf)。
其中一個主要特性是能夠在用戶返回平臺時記住他們離開的地方,這意味著當用戶打開PDF時,他們應該被帶到在上次會話中離開的確切頁面。
實現這一點的最簡單方法是在瀏覽器中使用本地存儲,然而為了減少app.component中的代碼量,我們將創建一個服務來封裝保存和存儲頁碼的邏輯。
要生成這個服務,使用Angular CLI命令ng g s services/reader。
ng g s services/reader
CREATE src/app/services/reader.service.spec.ts (357 bytes)
CREATE src/app/services/reader.service.ts (135 bytes)
打開reader.service.ts文件,執行如下操作:
public assetURL = '//localhost:4200/assets/'; private currentPage: number = 1; private storageKey: string = 'book-page'; savePage(page: number) { localStorage.setItem(this.storageKey, page.toString()); } getPage() { const savedPage = localStorage.getItem(this.storageKey) || this.currentPage; return +savedPage; }
reader.service有了第一個版本,讓我們將它與HTML標記和Kendo UI PDF查看器連接起來。
現在我們已經準備好了ReaderService,下一步是啟用第一個交互并顯示PDF。為此,我們需要在app.component.ts文件中工作,并注入ReaderService。
下面是我們將要介紹的內容:
首先,導入ReaderService,并使用Angular的依賴注入將其注入到組件中。
import { Component, inject } from '@angular/core'; import { ReaderService } from './services/reader.service'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.scss'], }) export class AppComponent { title = 'elearning-platform'; readerService = inject(ReaderService); ....
接下來,讓我們聲明必要的變量并實現selectBook方法。在這個方法中,我們將通過組合readerService來更新readerService.assetUrl和bookName。
方法如下:
export class AppComponent { title = 'elearning-platform'; readerService = inject(ReaderService); pdfAssetUrl = ''; bookName!: string; selectBook() { this.pdfAssetUrl = `${this.readerService.assetURL}${this.bookName}`; } }
我們如何將這些變量與方法聯系起來,并對變化做出反應?Angular提供了幾種方法來監聽事件并對變化做出反應,為了響應select元素中的change方法,我們可以使用(change)事件并將其鏈接到selectBook函數。
如何將選擇元素的值鏈接到bookName變量?別擔心,Angular提供了ngModel,它是FormsModule的一部分,它通過雙向數據綁定幫助我們對變化做出反應。
<select (change)="selectBook()" [(ngModel)]="bookName"> <option value="angular.pdf">Angular</option> <option value="signals.pdf">Signals</option> </select>
接下來,我們希望響應更改,以便將PDF加載到kendo-pdfviewer組件中。為了實現這一點,我們綁定了url和saveFileName屬性。
saveFileName屬性允許我們在用戶單擊下載工具欄時定義文件的名稱。
url屬性是將PDF綁定到組件的幾種方法之一,在本例中,我們提供存儲PDF的URL。
最后的代碼看起來像:
<kendo-pdfviewer [saveFileName]="bookName" [url]="pdfAssetUrl"> </kendo-pdfviewer>
保存更改,然后重新加載頁面并與下拉菜單交互來加載不同的PDF。
是的,我們已經成功加載了PDF!但是,仍然有一些功能需要完成,例如保存頁面位置和控制下載選項。篇幅有限,未完待續下期見~
Telerik_KendoUI產品技術交流群:726377843 歡迎一起進群討論
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都網