CompdfkitPdfModule.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /**
  2. * Copyright © 2014-2024 PDF Technologies, Inc. All Rights Reserved.
  3. * <p>
  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. package com.compdfkitpdf.reactnative;
  10. import android.content.Intent;
  11. import android.net.Uri;
  12. import android.util.Log;
  13. import androidx.annotation.NonNull;
  14. import com.compdfkit.core.document.CPDFSdk;
  15. import com.compdfkit.tools.common.pdf.CPDFConfigurationUtils;
  16. import com.compdfkit.tools.common.pdf.CPDFDocumentActivity;
  17. import com.compdfkit.tools.common.pdf.config.CPDFConfiguration;
  18. import com.compdfkit.tools.common.utils.CFileUtils;
  19. import com.facebook.react.bridge.Promise;
  20. import com.facebook.react.bridge.ReactApplicationContext;
  21. import com.facebook.react.bridge.ReactContext;
  22. import com.facebook.react.bridge.ReactContextBaseJavaModule;
  23. import com.facebook.react.bridge.ReactMethod;
  24. /**
  25. * RN and Android native ComPDFKit SDK interaction class
  26. *
  27. */
  28. public class CompdfkitPdfModule extends ReactContextBaseJavaModule {
  29. private static final String TAG = "ComPDFKit-RN";
  30. public static final String NAME = "ComPDFKit";
  31. public static final String ASSETS_SCHEME = "file://android_assets";
  32. public static final String CONTENT_SCHEME = "content://";
  33. private ReactContext mReactContext;
  34. public CompdfkitPdfModule(ReactApplicationContext reactContext) {
  35. super(reactContext);
  36. this.mReactContext = reactContext;
  37. }
  38. @Override
  39. @NonNull
  40. public String getName() {
  41. return NAME;
  42. }
  43. /**
  44. * Get the version number of the ComPDFKit SDK.<br/>
  45. * For example: "2.0.0".<br/>
  46. * <p></p>
  47. * Usage example:<br/><br/>
  48. * <pre>
  49. * ComPDFKit.getVersionCode().then((versionCode : string) => {
  50. * console.log('ComPDFKit SDK Version:', versionCode)
  51. * })
  52. * </pre>
  53. *
  54. */
  55. @ReactMethod
  56. public void getVersionCode(final Promise promise) {
  57. promise.resolve(CPDFSdk.getSDKVersion());
  58. }
  59. /**
  60. * Get the build tag of the ComPDFKit PDF SDK.<br/>
  61. * For example: "build_beta_2.0.0_42db96987_202404081007"<br/>
  62. * <p></p>
  63. *
  64. * Usage example:<br/>
  65. * <pre>
  66. * ComPDFKit.getSDKBuildTag().then((buildTag : string) => {
  67. * console.log('ComPDFKit Build Tag:', buildTag)
  68. * })
  69. * </pre>
  70. *
  71. */
  72. @ReactMethod
  73. public void getSDKBuildTag(final Promise promise) {
  74. promise.resolve(CPDFSdk.getSDKBuildTag());
  75. }
  76. /**
  77. * Initialize the ComPDFKit PDF SDK using offline authentication.<br/>
  78. * <p></p>
  79. * Usage example:<br/>
  80. * <pre>
  81. * ComPDFKit.init_('license')
  82. * </pre>
  83. *
  84. * @param license The offline license.
  85. */
  86. @ReactMethod
  87. public void init_(String license) {
  88. CPDFSdk.init(mReactContext, license, true, (code, msg) -> {
  89. Log.e(TAG, "init_: code:" + code + ", msg:" + msg);
  90. });
  91. }
  92. /**
  93. * Initialize the ComPDFKit PDF SDK using online authentication. <br/>
  94. * Requires internet connection. Please ensure that the network permission has been added in [AndroidManifest.xml] file. <br/>
  95. * {@link android.Manifest.permission#INTERNET} <br/>
  96. * <p></p>
  97. * Usage example:
  98. * <pre>
  99. * ComPDFKit.initialize(androidLicense, iosLicense)
  100. * </pre>
  101. *
  102. * @param androidOnlineLicense The online license for the ComPDFKit SDK on Android platform.
  103. * @param iosOnlineLicense The online license for the ComPDFKit SDK on iOS platform.
  104. */
  105. @ReactMethod
  106. public void initialize(String androidOnlineLicense, String iosOnlineLicense) {
  107. CPDFSdk.init(mReactContext, androidOnlineLicense, false, (code, msg) -> {
  108. Log.e(TAG, "initialize: code:" + code + ", msg:" + msg);
  109. });
  110. }
  111. /**
  112. * Display a PDF.<br/>
  113. *
  114. * Usage example:<br/>
  115. * <pre>
  116. * ComPDFKit.openDocument(document, password, configurationJson)
  117. * </pre>
  118. *
  119. * (Android) For local storage file path: <br/>
  120. * <pre>
  121. * document = "file:///storage/emulated/0/Download/sample.pdf";<br/>
  122. * </pre>
  123. *
  124. * (Android) For content Uri: <br/>
  125. * <pre>
  126. * document = "content://...";
  127. * </pre>
  128. *
  129. * (Android) For assets path: <br/>
  130. * <pre>
  131. * document = "file://android_assets/..."
  132. * </pre>
  133. *
  134. * @param document The document URI or file path.
  135. * @param password The document password.
  136. * @param configurationJson Configuration data in JSON format.
  137. */
  138. @ReactMethod
  139. public void openDocument(String document, String password, String configurationJson) {
  140. Intent intent = new Intent(mReactContext, CPDFDocumentActivity.class);
  141. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  142. parseDocument(document, intent);
  143. intent.putExtra(CPDFDocumentActivity.EXTRA_FILE_PASSWORD, password);
  144. CPDFConfiguration configuration = CPDFConfigurationUtils.fromJson(configurationJson);
  145. intent.putExtra(CPDFDocumentActivity.EXTRA_CONFIGURATION, configuration);
  146. mReactContext.startActivity(intent);
  147. }
  148. private void parseDocument(String document, Intent intent) {
  149. if (document.startsWith(ASSETS_SCHEME)) {
  150. String assetsPath = document.replace(ASSETS_SCHEME + "/","");
  151. String[] strs = document.split("/");
  152. String fileName = strs[strs.length -1];
  153. String samplePDFPath = CFileUtils.getAssetsTempFile(mReactContext, assetsPath, fileName);
  154. intent.putExtra(CPDFDocumentActivity.EXTRA_FILE_PATH, samplePDFPath);
  155. } else if (document.startsWith(CONTENT_SCHEME)) {
  156. Uri uri = Uri.parse(document);
  157. intent.setData(uri);
  158. }
  159. }
  160. }