123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- /**
- * Copyright © 2014-2024 PDF Technologies, Inc. All Rights Reserved.
- * <p>
- * 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.
- */
- package com.compdfkitpdf.reactnative;
- import android.content.Intent;
- import android.net.Uri;
- import android.util.Log;
- import androidx.annotation.NonNull;
- import com.compdfkit.core.document.CPDFSdk;
- import com.compdfkit.tools.common.pdf.CPDFConfigurationUtils;
- import com.compdfkit.tools.common.pdf.CPDFDocumentActivity;
- import com.compdfkit.tools.common.pdf.config.CPDFConfiguration;
- import com.compdfkit.tools.common.utils.CFileUtils;
- import com.facebook.react.bridge.Promise;
- import com.facebook.react.bridge.ReactApplicationContext;
- import com.facebook.react.bridge.ReactContext;
- import com.facebook.react.bridge.ReactContextBaseJavaModule;
- import com.facebook.react.bridge.ReactMethod;
- /**
- * RN and Android native ComPDFKit SDK interaction class
- *
- */
- public class CompdfkitPdfModule extends ReactContextBaseJavaModule {
- private static final String TAG = "ComPDFKit-RN";
- public static final String NAME = "ComPDFKit";
- public static final String ASSETS_SCHEME = "file://android_assets";
- public static final String CONTENT_SCHEME = "content://";
- private ReactContext mReactContext;
- public CompdfkitPdfModule(ReactApplicationContext reactContext) {
- super(reactContext);
- this.mReactContext = reactContext;
- }
- @Override
- @NonNull
- public String getName() {
- return NAME;
- }
- /**
- * Get the version number of the ComPDFKit SDK.<br/>
- * For example: "2.0.0".<br/>
- * <p></p>
- * Usage example:<br/><br/>
- * <pre>
- * ComPDFKit.getVersionCode().then((versionCode : string) => {
- * console.log('ComPDFKit SDK Version:', versionCode)
- * })
- * </pre>
- *
- */
- @ReactMethod
- public void getVersionCode(final Promise promise) {
- promise.resolve(CPDFSdk.getSDKVersion());
- }
- /**
- * Get the build tag of the ComPDFKit PDF SDK.<br/>
- * For example: "build_beta_2.0.0_42db96987_202404081007"<br/>
- * <p></p>
- *
- * Usage example:<br/>
- * <pre>
- * ComPDFKit.getSDKBuildTag().then((buildTag : string) => {
- * console.log('ComPDFKit Build Tag:', buildTag)
- * })
- * </pre>
- *
- */
- @ReactMethod
- public void getSDKBuildTag(final Promise promise) {
- promise.resolve(CPDFSdk.getSDKBuildTag());
- }
- /**
- * Initialize the ComPDFKit PDF SDK using offline authentication.<br/>
- * <p></p>
- * Usage example:<br/>
- * <pre>
- * ComPDFKit.init_('license')
- * </pre>
- *
- * @param license The offline license.
- */
- @ReactMethod
- public void init_(String license) {
- CPDFSdk.init(mReactContext, license, true, (code, msg) -> {
- Log.e(TAG, "init_: code:" + code + ", msg:" + msg);
- });
- }
- /**
- * Initialize the ComPDFKit PDF SDK using online authentication. <br/>
- * Requires internet connection. Please ensure that the network permission has been added in [AndroidManifest.xml] file. <br/>
- * {@link android.Manifest.permission#INTERNET} <br/>
- * <p></p>
- * Usage example:
- * <pre>
- * ComPDFKit.initialize(androidLicense, iosLicense)
- * </pre>
- *
- * @param androidOnlineLicense The online license for the ComPDFKit SDK on Android platform.
- * @param iosOnlineLicense The online license for the ComPDFKit SDK on iOS platform.
- */
- @ReactMethod
- public void initialize(String androidOnlineLicense, String iosOnlineLicense) {
- CPDFSdk.init(mReactContext, androidOnlineLicense, false, (code, msg) -> {
- Log.e(TAG, "initialize: code:" + code + ", msg:" + msg);
- });
- }
- /**
- * Display a PDF.<br/>
- *
- * Usage example:<br/>
- * <pre>
- * ComPDFKit.openDocument(document, password, configurationJson)
- * </pre>
- *
- * (Android) For local storage file path: <br/>
- * <pre>
- * document = "file:///storage/emulated/0/Download/sample.pdf";<br/>
- * </pre>
- *
- * (Android) For content Uri: <br/>
- * <pre>
- * document = "content://...";
- * </pre>
- *
- * (Android) For assets path: <br/>
- * <pre>
- * document = "file://android_assets/..."
- * </pre>
- *
- * @param document The document URI or file path.
- * @param password The document password.
- * @param configurationJson Configuration data in JSON format.
- */
- @ReactMethod
- public void openDocument(String document, String password, String configurationJson) {
- Intent intent = new Intent(mReactContext, CPDFDocumentActivity.class);
- intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- parseDocument(document, intent);
- intent.putExtra(CPDFDocumentActivity.EXTRA_FILE_PASSWORD, password);
- CPDFConfiguration configuration = CPDFConfigurationUtils.fromJson(configurationJson);
- intent.putExtra(CPDFDocumentActivity.EXTRA_CONFIGURATION, configuration);
- mReactContext.startActivity(intent);
- }
- private void parseDocument(String document, Intent intent) {
- if (document.startsWith(ASSETS_SCHEME)) {
- String assetsPath = document.replace(ASSETS_SCHEME + "/","");
- String[] strs = document.split("/");
- String fileName = strs[strs.length -1];
- String samplePDFPath = CFileUtils.getAssetsTempFile(mReactContext, assetsPath, fileName);
- intent.putExtra(CPDFDocumentActivity.EXTRA_FILE_PATH, samplePDFPath);
- } else if (document.startsWith(CONTENT_SCHEME)) {
- Uri uri = Uri.parse(document);
- intent.setData(uri);
- }
- }
- }
|