Skip to content

运行APP

在从 NPM 或 GitHub 安装后,将 App.tsx 替换为以下代码。

确保按照上述步骤将示例文档复制到你的 Android 或 iOS 项目中。

以下是 App.tsx 的示例代码:

tsx
/**
 * Copyright © 2014-2024 PDF Technologies, Inc. All Rights Reserved.
 *
 * THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
 * AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
 * UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
 * This notice may not be removed from this file.
 */

import React, { Component } from 'react';
import { SafeAreaView } from 'react-native';
import { ComPDFKit, CPDFReaderView } from '@compdfkit_pdf_sdk/react_native';
import { Platform } from 'react-native';

type Props = {};

export default class App extends Component<Props> {

  constructor(props: Props) {
    super(props)
    this.initialize()
  }

  async initialize() {
    // 在线认证,填写你的在线许可证
    // 如果初始化成功,则返回 true,否则返回 false。
    // var result = await ComPDFKit.initialize('compdfkit android license', 'compdfkit ios license')
    // console.log("ComPDFKitRN", "initialize:", result)

    // 离线认证,请填写你的离线许可证。
    // var result;
    // if(Platform.OS == 'android') {
    //   result = await ComPDFKit.init_('your compdfkit license')
    // } else {
    //   result = await ComPDFKit.init_('your compdfkit license')
    // }
    // console.log("ComPDFKitRN", "init_:", result)
  }

  samplePDF = Platform.OS === 'android'
  ? 'file:///android_asset/PDF_Document.pdf'
  : 'PDF_Document.pdf';


  render() {
    return (
      <CPDFReaderView
        document={this.samplePDF}
        configuration={ComPDFKit.getDefaultConfig({})}
        style={{ flex: 1 }}
        />
    );
  }
}
  • (Android)对于本地存储文件路径:
tsx
document = '/storage/emulated/0/Download/PDF_document.pdf'
  • (Android)对于内容 Uri:
tsx
document = 'content://...'
  • (Android)对于资源路径:
tsx
document = "file:///android_asset/..."
  • (iOS)对于应用包文件路径:
tsx
document = "document.pdf"
  • (iOS)对于 URL 路径:
tsx
document = "file://xxxx/document.pdf"

应用现在已准备就绪!返回终端应用并运行:

shell
npx react-native run-android
npx react-native run-ios