App.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. })
  54. ComPDFKit.openDocument(samplePDF, '', config)
  55. }
  56. /**
  57. * Pick a PDF file from the local storage of Android or iOS device, this example uses the `react-native-document-picker` package,
  58. * If you want to use this example, please add this package to your project first.
  59. * {@link https://www.npmjs.com/package/react-native-document-picker}
  60. *
  61. */
  62. pickPDFFile(){
  63. try {
  64. const pickerResult = DocumentPicker.pick({
  65. type: [DocumentPicker.types.pdf]
  66. });
  67. pickerResult.then(res => {
  68. ComPDFKit.openDocument(res[0]?.uri as string, '', ComPDFKit.getDefaultConfig({}))
  69. })
  70. } catch (err) {
  71. }
  72. }
  73. render() {
  74. return (
  75. <SafeAreaView style={{ flex: 1 }}>
  76. <View style={styles.scaffold}>
  77. <View style={styles.appBar}>
  78. <Text style={styles.mediumTitle}>
  79. ComPDFKit PDF SDK for ReactNative
  80. </Text>
  81. </View>
  82. <View style={styles.container}>
  83. <TouchableOpacity onPress={() => {
  84. this.openSample()
  85. }}>
  86. <View style={styles.funItem}>
  87. <Text style={{ fontWeight: 'bold' }}>{'Open Sample'}</Text>
  88. </View>
  89. </TouchableOpacity>
  90. <View style={styles.dividingLine} />
  91. <TouchableOpacity onPress={() => {
  92. this.pickPDFFile()
  93. }}>
  94. <View style={styles.funItem}>
  95. <Text style={{ fontWeight: 'bold' }}>{'Pick Document'}</Text>
  96. </View>
  97. <View style={styles.dividingLine} />
  98. </TouchableOpacity>
  99. <View style={styles.buttom}>
  100. <Text style={styles.body2}>ComPDFKit for {Platform.OS == 'android' ? 'Android' : 'iOS'} {this.state.versionCode}</Text>
  101. </View>
  102. </View>
  103. </View>
  104. </SafeAreaView>
  105. );
  106. }
  107. }
  108. const styles = StyleSheet.create({
  109. appBar: {
  110. height: 56,
  111. backgroundColor: '#FAFCFF',
  112. elevation: 4,
  113. flexDirection: "row",
  114. justifyContent: "space-between",
  115. alignItems: "center",
  116. padding: 16
  117. },
  118. mediumTitle: {
  119. fontSize: 16,
  120. },
  121. body2: {
  122. textAlign: 'center',
  123. fontSize: 12
  124. },
  125. scaffold: {
  126. flex: 1,
  127. },
  128. container: {
  129. marginHorizontal: 16,
  130. marginVertical: 8,
  131. flex: 1,
  132. // backgroundColor: '#F5FCFF',
  133. },
  134. funItem: {
  135. height: 56,
  136. justifyContent: 'center',
  137. textAlign: 'center'
  138. },
  139. dividingLine: {
  140. height: 0.5, backgroundColor: '#4D333333', width: '100%'
  141. },
  142. buttom: {
  143. flex: 1,
  144. justifyContent: 'flex-end',
  145. }
  146. });