原創|行業資訊|編輯:何躍|2022-01-20 10:27:52.560|閱讀 277 次
概述:在本教程中,我們展示了如何使用PDFTron SDK(具有完整的文檔編輯和注釋能力)和React Native創建一個簡單的、類型安全的PDF查看器。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
我們將使用PDFTron最近發布的TypeScript對PDFTron React Native SDK的支持。如果你對React Native感到陌生,它是一個開源的框架,用于在一個代碼庫中構建原生移動應用程序。PDFTron SDK使用React Native來創建跨平臺的PDF瀏覽器,具有傳統原生應用程序的流暢外觀和感覺。
在本教程結束時,你將建立這樣的東西:
首先,讓我們使用TypeScript模板創建一個簡單的React Native應用。
注意:如果你之前在全局范圍內安裝了react-native-cli,請先刪除它,以防止出現意外行為。你可以按照這些步驟進行npm或yarn的安裝。
// npm npm i -g @react-native-community/cli react-native init PDFDemo --template react-native-template-typescript --npm cd PDFDemo //yarn yarn global add @react-native-community/cli react-native init PDFDemo --template react-native-template-typescript cd PDFDemo
通過調用以下方式安裝 react-native-pdftron。
//npm npm install github:PDFTron/pdftron-react-native --save npm install @react-native-community/cli --save-dev npm install @react-native-community/cli-platform-android --save-dev npm install @react-native-community/cli-platform-ios --save-dev npm install //yarn yarn add github:PDFTron/pdftron-react-native yarn add @react-native-community/cli --dev yarn add @react-native-community/cli-platform-android --dev yarn add @react-native-community/cli-platform-ios --dev yarn install
我們將告訴你如何將PDFTron的React Native模塊添加到應用程序中。安卓系統按照步驟1-5,iOS系統按照步驟1-2。
用下面的內容替換App.tsx。
import React, { Component } from 'react'; import { Platform, StyleSheet, Text, View, PermissionsAndroid, BackHandler, NativeModules, Alert } from 'react-native'; import { DocumentView, RNPdftron } from 'react-native-pdftron'; type Props = {}; export default class App extends Component<Props> { constructor(props: Props) { super(props); RNPdftron.initialize("Insert commercial license key here after purchase"); RNPdftron.enableJavaScript(true); } onLeadingNavButtonPressed = () => { console.log('leading nav button pressed'); if (Platform.OS === 'ios') { Alert.alert( 'App', 'onLeadingNavButtonPressed', [ {text: 'OK', onPress: () => console.log('OK Pressed')}, ], { cancelable: true } ) } else { BackHandler.exitApp(); } } render() { const path = "http://pdftron.s3.amazonaws.com/downloads/pl/PDFTRON_mobile_about.pdf"; return ( <DocumentView document={path} showLeadingNavButton={true} leadingNavButtonIcon={Platform.OS === 'ios' ? 'ic_close_black_24px.png' : 'ic_arrow_back_white_24dp'} onLeadingNavButtonPressed={this.onLeadingNavButtonPressed} /> ); } } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#F5FCFF', } });最后,你可以通過以下命令來運行該應用程序:
iOS:npm run ios, yarn run ios, 或者在Xcode中打開ios/PDFDemo.xcworkspace,然后點擊三角形的運行按鈕。
Android:npm run android, yarn run android或在Android Studio中打開android/,然后點擊三角形的運行按鈕。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn