App.tsx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /**
  2. * Copyright © 2014-2025 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 { Image, Platform, TouchableOpacity } from 'react-native';
  11. import { ComPDFKit } from '@compdfkit_pdf_sdk/react_native';
  12. import { NavigationContainer } from '@react-navigation/native';
  13. import { createNativeStackNavigator } from '@react-navigation/native-stack';
  14. import RNFS from 'react-native-fs';
  15. import HomeScreen from './src/screens/HomeScreen';
  16. import CPDFReaderViewExampleScreen from './src/CPDFReaderViewExample';
  17. import SettingScreen from './src/screens/SettingScreen';
  18. import CPDFReaderViewControllerExampleScreen from './src/CPDFReaderViewControllerExample';
  19. import CPDFAnnotationsExampleScreen from './src/CPDFAnnotationsExample';
  20. import CPDFSecurityExampleScreen from './src/CPDFSecurityExample';
  21. var androidLicense = "c4r34cNxld6pdUSsM2EgGQ9purMEZN7JuumHbNx9w+OJ6lYqF7WRYAFV0jvA5CEEX7J1+wGrF68mo8YP3qL5b6lmS60TMw2/wJ+TPtmeKkJMRMaiQ2nF2pwSevBj2HIHbnTLfr2btYHL/TRKFbfG0SUp/tZVjUwOC91rcrlaLkThWpc13/3SOi/34uHyjQszJ6PmDeg8X+qxfNheOBX/JZofPOjKpWpAAXXKT9kgtbZKKc6VAmMPAdRth7FnJjPieX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ux+CsXiD9U3GqMAwDAuvCazhwWWYdZSeQEo/KzTL1/ZbF6AdfEFP9K7Gk2q7RcuwX0WaH22At997+WnwH2EpwfXQGtUSVYO7LGMn7kCs+m+CU6hMmG0S9YRz9oTm7jAk99+y3RaJXkIETmZj5OWTIPVGjNuDKhNASTddpPc68mSFrBmizUniO1Wu0lH1oY17eVN2TRW5amzrVRSqpTOyx2LGvW1Ilra90nzlp2dEBHH+rU3Jo93jy94eWecFWMwgKBD5sABvhJFteiZTpP6NufkmmJm5UhS1bbWwQ3416ecpKs8D9TAlLLO+rbIocuxdoPE2dxWFYLq6zF8kJV3z7dKYtAwQKdoQiS08ryGXVZybCx2GjZp97I7zNemiorRWKQUrxpNk0vCLwL1yz7NzjlB6YQ8UxvmTkX/GU7T7Ubg9LoyZuVo4tLHLplMSlHIcA4guqZL7JbQ6/jomhcJGFpGo+X7tbrCvMTnbvjZoxJRlcNN1+9x100WEfF4A2XbJZEjcpxV9tk1rTt+jS8dkX803ij16yHI1THSycP2aKkWjgDAsAUt98KJfMhDVdBtIWgg2xXbvUdZdPA+MDYbxOt/kTCqmTNMCBL4i4/N1Dqxvl8Me6vei5LeecqlBZbInEmA+87xsmNJ+d5MxP+EIac9+yqaQ0YIYE4wnAzUkp/0SfANyT5mw/EcTvSxlFmdNPXMpAygK4Q1n/S3QjnpAZJCMhg6hUDQi1TPHJO1WCWdGg50xVd4e9cvnNaN633IvvL9TFBGBu4xx7FHUdlWeiIkfzaiP8Y+58lPpC7x05cs28uc/hwXiZGx2KvdF+199pSLVUIrj85m/7+SqS01Dz6t4RwV7CZ/nQ4KOAyptnYBkUNyyOx7Q7j3CUXnxUN5HL5Yhqq2FFaD/HTTVwcjun3kBQWAiUV1AWYY/lHA5ERHysKmgUkhba8MlQCnsGD2o4KGBScfS0N2kQ3Eg6AtwSRVmQZHRUOuFrRzDh6w9DqXG/2btsFdOOfQ==";
  23. const Stack = createNativeStackNavigator();
  24. type Props = {
  25. navigation: any;
  26. };
  27. export default class App extends Component<Props> {
  28. constructor(props: Props) {
  29. super(props)
  30. this.initialize()
  31. }
  32. async initialize() {
  33. const fontDir = await copyAssetsFolderToStorage('extraFonts');
  34. console.log('ComPDFKitRN', "fontDir:", fontDir)
  35. await ComPDFKit.setImportFontDir(fontDir, true);
  36. // Online certification, Fill in your online license
  37. // Returns true if initialization is successful, otherwise returns false.
  38. // var result = await ComPDFKit.initialize('Njc4NzI1OGQ3ZGQyZQ==', 'Njc4NzI1OGQ3ZGQyZQ==')
  39. // console.log("ComPDFKitRN", "initialize:", result)
  40. // Offline authentication, Fill in your offline license
  41. var result = await ComPDFKit.init_(Platform.OS == "android" ? androidLicense : iosLicense)
  42. console.log("ComPDFKitRN", "init_:", result)
  43. }
  44. render() {
  45. return (
  46. <NavigationContainer>
  47. <Stack.Navigator>
  48. <Stack.Screen name="Home" component={HomeScreen} options={({ navigation }) => ({
  49. title: 'ComPDFKit PDF SDK for ReactNative',
  50. headerStyle: {
  51. backgroundColor: '#FAFCFF',
  52. },
  53. headerTitleStyle: { fontSize: 16 },
  54. headerRight: () => (
  55. <TouchableOpacity onPress={() => navigation.navigate('Settings')}>
  56. <Image source={require('./assets/ic_setting.png')} style={{ width: 24, height: 24 }} />
  57. </TouchableOpacity>
  58. ),
  59. })} />
  60. <Stack.Screen name='Settings' component={SettingScreen} options={{
  61. headerStyle: {
  62. backgroundColor: '#FAFCFF',
  63. },
  64. }}/>
  65. <Stack.Screen name='CPDFReaderViewExample' component={CPDFReaderViewExampleScreen} options={{
  66. headerShadowVisible: false,
  67. headerTitleStyle: { fontSize: 16 },
  68. headerStyle: {
  69. backgroundColor: '#FAFCFF',
  70. },
  71. }} />
  72. <Stack.Screen
  73. name='CPDFReaderViewControllerExample'
  74. component={CPDFReaderViewControllerExampleScreen}
  75. options={() => ({
  76. headerShown: false,
  77. })}
  78. />
  79. <Stack.Screen
  80. name='CPDFAnnotationsExample'
  81. component={CPDFAnnotationsExampleScreen}
  82. options={() => ({
  83. headerShown: false,
  84. })}
  85. />
  86. <Stack.Screen
  87. name='CPDFSecurityExample'
  88. component={CPDFSecurityExampleScreen}
  89. options={() => ({
  90. headerShown: false,
  91. })}
  92. />
  93. </Stack.Navigator>
  94. </NavigationContainer>
  95. );
  96. }
  97. }
  98. const copyAssetsFolderToStorage = async (folderName : string) => {
  99. try {
  100. // Define the target storage directory
  101. const targetDir = `${RNFS.DocumentDirectoryPath}/${folderName}`;
  102. // Ensure the target directory exists
  103. const dirExists = await RNFS.exists(targetDir);
  104. if (!dirExists) {
  105. await RNFS.mkdir(targetDir);
  106. }
  107. if (Platform.OS === 'android') {
  108. // Android: Read all files in the folder
  109. const files = await RNFS.readDirAssets(folderName); // Returns an array of file objects
  110. for (const file of files) {
  111. if (file.isFile()) {
  112. const fileContents = await RNFS.readFileAssets(`${folderName}/${file.name}`, 'base64'); // Read file from assets
  113. const targetFilePath = `${targetDir}/${file.name}`;
  114. await RNFS.writeFile(targetFilePath, fileContents, 'base64'); // Write to target
  115. }
  116. }
  117. } else if (Platform.OS === 'ios') {
  118. // iOS: Copy files directly
  119. const files = await RNFS.readDir(`${RNFS.MainBundlePath}/${folderName}`);
  120. for (const file of files) {
  121. if (file.isFile()) {
  122. const sourcePath = file.path;
  123. const targetFilePath = `${targetDir}/${file.name}`;
  124. if(await RNFS.exists(targetFilePath)){
  125. continue;
  126. }
  127. await RNFS.copyFile(sourcePath, targetFilePath);
  128. }
  129. }
  130. } else {
  131. throw new Error('Unsupported platform');
  132. }
  133. return targetDir; // Return the target directory path
  134. } catch (error) {
  135. console.error('Error copying folder:', error);
  136. throw new Error('Failed to copy folder to storage.');
  137. }
  138. };