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() {
    var result = await ComPDFKit.initWithPath(Platform.OS == "android" ? "assets://license_key_rn.xml" : "license_key_rn.xml")
    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