App.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /**
  2. * Copyright © 2014-2024 PDF Technologies, Inc. All Rights Reserved.
  3. *
  4. * THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  5. * AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  6. * UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  7. * This notice may not be removed from this file.
  8. */
  9. import React, { Component } from 'react';
  10. import DocumentPicker from 'react-native-document-picker'
  11. import {
  12. StyleSheet,
  13. Text,
  14. View,
  15. TouchableOpacity,
  16. SafeAreaView
  17. } from 'react-native';
  18. import { ComPDFKit } from 'react-native-compdfkit-pdf';
  19. import { Platform } from 'react-native';
  20. type Props = {};
  21. export default class App extends Component<Props> {
  22. state = {
  23. versionCode: ''
  24. }
  25. constructor(props: Props) {
  26. super(props)
  27. this.initialize()
  28. this.getVersionCode()
  29. }
  30. async getVersionCode() {
  31. // Get the version code of ComPDFKit SDK
  32. var version = await ComPDFKit.getVersionCode()
  33. this.setState({
  34. versionCode: version
  35. })
  36. }
  37. async initialize() {
  38. // Online certification, Fill in your online license
  39. // Returns true if initialization is successful, otherwise returns false.
  40. var result = await ComPDFKit.initialize('vxwRHJC9RDKK0He2jTpepBkrolOnPQt7bxjfIiqqcJc=', 'vTXxqCm2rutpV67wRy+UnBSJzDBwG0/DLjiLL61zxxc=')
  41. console.log("ComPDFKitRN", "initialize:", result)
  42. // Offline authentication, Fill in your offline license
  43. // var result = await ComPDFKit.init_('your compdfkit license')
  44. // console.log("ComPDFKitRN", "init_:", result)
  45. }
  46. /**
  47. * Open the sample document embedded in Android or iOS project.
  48. */
  49. openSample(){
  50. var samplePDF: string = Platform.OS == 'android' ? 'file:///android_asset/PDF_Document.pdf' : 'PDF_Document.pdf'
  51. // We provide default UI and PDF property related configurations here, you can modify configuration options according to your needs.
  52. var config = ComPDFKit.getDefaultConfig({})
  53. ComPDFKit.openDocument(samplePDF, '', config)
  54. }
  55. /**
  56. * Pick a PDF file from the local storage of Android or iOS device, this example uses the `react-native-document-picker` package,
  57. * If you want to use this example, please add this package to your project first.
  58. * {@link https://www.npmjs.com/package/react-native-document-picker}
  59. *
  60. */
  61. pickPDFFile(){
  62. try {
  63. const pickerResult = DocumentPicker.pick({
  64. type: [DocumentPicker.types.pdf]
  65. });
  66. pickerResult.then(res => {
  67. ComPDFKit.openDocument(res[0]?.uri as string, '', ComPDFKit.getDefaultConfig({}))
  68. })
  69. } catch (err) {
  70. }
  71. }
  72. render() {
  73. return (
  74. <SafeAreaView style={{ flex: 1 }}>
  75. <View style={styles.scaffold}>
  76. <View style={styles.appBar}>
  77. <Text style={styles.mediumTitle}>
  78. ComPDFKit PDF SDK for ReactNative
  79. </Text>
  80. </View>
  81. <View style={styles.container}>
  82. <TouchableOpacity onPress={() => {
  83. this.openSample()
  84. }}>
  85. <View style={styles.funItem}>
  86. <Text style={{ fontWeight: 'bold' }}>{'Open Sample'}</Text>
  87. </View>
  88. </TouchableOpacity>
  89. <View style={styles.dividingLine} />
  90. <TouchableOpacity onPress={() => {
  91. this.pickPDFFile()
  92. }}>
  93. <View style={styles.funItem}>
  94. <Text style={{ fontWeight: 'bold' }}>{'Pick Document'}</Text>
  95. </View>
  96. <View style={styles.dividingLine} />
  97. </TouchableOpacity>
  98. <View style={styles.buttom}>
  99. <Text style={styles.body2}>ComPDFKit for {Platform.OS == 'android' ? 'Android' : 'iOS'} {this.state.versionCode}</Text>
  100. </View>
  101. </View>
  102. </View>
  103. </SafeAreaView>
  104. );
  105. }
  106. }
  107. const styles = StyleSheet.create({
  108. appBar: {
  109. height: 56,
  110. backgroundColor: '#FAFCFF',
  111. elevation: 4,
  112. flexDirection: "row",
  113. justifyContent: "space-between",
  114. alignItems: "center",
  115. padding: 16
  116. },
  117. mediumTitle: {
  118. fontSize: 16,
  119. },
  120. body2: {
  121. textAlign: 'center',
  122. fontSize: 12
  123. },
  124. scaffold: {
  125. flex: 1,
  126. },
  127. container: {
  128. marginHorizontal: 16,
  129. marginVertical: 8,
  130. flex: 1,
  131. // backgroundColor: '#F5FCFF',
  132. },
  133. funItem: {
  134. height: 56,
  135. justifyContent: 'center',
  136. textAlign: 'center'
  137. },
  138. dividingLine: {
  139. height: 0.5, backgroundColor: '#4D333333', width: '100%'
  140. },
  141. buttom: {
  142. flex: 1,
  143. justifyContent: 'flex-end',
  144. }
  145. });