Skip to content

Commit 5fa6f7a

Browse files
authored
Merge pull request #76 from PSPDFKit/simone/feature/image-document
Image document support
2 parents 60bdcec + e8adc9c commit 5fa6f7a

File tree

8 files changed

+1567
-1190
lines changed

8 files changed

+1567
-1190
lines changed

android/src/main/java/com/pspdfkit/react/PSPDFKitModule.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,23 @@ public void present(@NonNull String document, @NonNull ReadableMap configuration
6868
}
6969
}
7070

71+
@ReactMethod
72+
public void presentImage(@NonNull String imageDocument, @NonNull ReadableMap configuration) {
73+
if (getCurrentActivity() != null) {
74+
if (resumedActivity == null) {
75+
// We register an activity lifecycle callback so we can get notified of the current activity.
76+
getCurrentActivity().getApplication().registerActivityLifecycleCallbacks(this);
77+
}
78+
ConfigurationAdapter configurationAdapter = new ConfigurationAdapter(getCurrentActivity(), configuration);
79+
// This is an edge case where file scheme is missing.
80+
if (Uri.parse(imageDocument).getScheme() == null) {
81+
imageDocument = FILE_SCHEME + imageDocument;
82+
}
83+
84+
PdfActivity.showImage(getCurrentActivity(), Uri.parse(imageDocument), configurationAdapter.build());
85+
}
86+
}
87+
7188
@ReactMethod
7289
public synchronized void setPageIndex(final int pageIndex, final boolean animated) {
7390
if (resumedActivity instanceof PdfActivity) {

samples/Catalog/Catalog.android.js

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,19 @@ import { YellowBox } from 'react-native'
3131
YellowBox.ignoreWarnings(['Warning: isMounted(...) is deprecated'])
3232

3333
var PSPDFKit = NativeModules.PSPDFKit;
34+
var RNFS = require('react-native-fs');
3435

3536
const pspdfkitColor = "#267AD4";
3637
const pspdfkitColorAlpha = "#267AD450";
3738

3839
const DOCUMENT = "file:///sdcard/Annual Report.pdf";
40+
const IMAGE_DOCUMENT = "file:///sdcard/android.png";
41+
const CONFIGURATION_IMAGE_DOCUMENT = {
42+
showPageNumberOverlay: false,
43+
showPageLabels: false,
44+
showThumbnailBar: "none"
45+
};
46+
3947
const CONFIGURATION = {
4048
startPage: 3,
4149
scrollContinuously: false,
@@ -57,10 +65,23 @@ var examples = [
5765
},
5866
{
5967
name: "Open local document",
60-
description: "Opens document from external storage directory.",
68+
description: "Open document from external storage directory.",
69+
action: () => {
70+
requestExternalStoragePermission(function() {
71+
extractFromAssetsIfMissing("Annual Report.pdf", function() {
72+
PSPDFKit.present(DOCUMENT, {});
73+
});
74+
});
75+
}
76+
},
77+
{
78+
name: "Open local image document",
79+
description: "Open image image document from external storage directory.",
6180
action: () => {
6281
requestExternalStoragePermission(function() {
63-
PSPDFKit.present(DOCUMENT, {});
82+
extractFromAssetsIfMissing("android.png", function() {
83+
PSPDFKit.presentImage(IMAGE_DOCUMENT, CONFIGURATION_IMAGE_DOCUMENT);
84+
});
6485
});
6586
}
6687
},
@@ -101,6 +122,39 @@ var examples = [
101122
}
102123
];
103124

125+
function extractFromAssetsIfMissing(assetFile, callback) {
126+
RNFS.exists("/sdcard/" + assetFile).then((exist) => {
127+
if (exist) {
128+
console.log(assetFile + " exists in the external storage directory.");
129+
callback();
130+
} else {
131+
console.log(assetFile + " does not exist, extracting it from assets folder to the external storage directory.");
132+
RNFS.existsAssets(assetFile).then((exist) => {
133+
// Check if the file is present in the assets folder.
134+
if(exist) {
135+
// File exists so it can be extracted to the external storage directory.
136+
RNFS.copyFileAssets(assetFile, "/sdcard/" + assetFile).then(() => {
137+
// File copied successfully from assets folder to external storage directory.
138+
callback();
139+
})
140+
.catch((error) => {
141+
console.log(error);
142+
});
143+
} else {
144+
// File does not exist, it should never happen.
145+
throw new Error(assetFile + " couldn't be extracted as it was not found in the project assets folder.");
146+
}
147+
})
148+
.catch((error) => {
149+
console.log(error);
150+
});
151+
}
152+
})
153+
.catch((error) => {
154+
console.log(error);
155+
});
156+
}
157+
104158
async function requestExternalStoragePermission(callback) {
105159
try {
106160
const granted = await PermissionsAndroid.request(

samples/Catalog/android/app/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,15 @@ android {
128128
}
129129
}
130130
}
131-
sourceSets { main { assets.srcDirs = ['src/main/assets', '../../../PDFs'] } }
131+
sourceSets { main { assets.srcDirs = ['src/main/assets', '../../../PDFs' , '../../../Images'] } }
132132
}
133133

134134
dependencies {
135135
compile fileTree(include: ['*.jar'], dir: 'libs')
136136
compile 'com.facebook.react:react-native:+'
137137
// From node_modules
138138
compile project(':react-native-pspdfkit')
139+
compile project(':react-native-fs')
139140
}
140141

141142
// Run this once to be able to run the application with BUCK

samples/Catalog/android/app/src/main/java/com/pspdfkit/react/catalog/MainApplication.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import android.app.Application;
1717

1818
import com.facebook.react.ReactApplication;
19+
import com.rnfs.RNFSPackage;
1920
import com.facebook.react.ReactNativeHost;
2021
import com.facebook.react.ReactPackage;
2122
import com.facebook.react.shell.MainReactPackage;
@@ -37,6 +38,7 @@ public boolean getUseDeveloperSupport() {
3738
protected List<ReactPackage> getPackages() {
3839
return Arrays.<ReactPackage>asList(
3940
new MainReactPackage(),
41+
new RNFSPackage(),
4042
new PSPDFKitPackage()
4143
);
4244
}

samples/Catalog/android/settings.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
rootProject.name = 'Catalog'
2+
include ':react-native-fs'
3+
project(':react-native-fs').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-fs/android')
24

35
include ':app'
46

samples/Catalog/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
22
"name": "Catalog",
3-
"version": "1.10.0",
3+
"version": "1.11.0",
44
"private": true,
55
"scripts": {
66
"start": "node node_modules/react-native/local-cli/cli.js start"
77
},
88
"dependencies": {
99
"react": "16.3.1",
1010
"react-native": "0.55.4",
11-
"react-native-fs": "2.9.0",
11+
"react-native-fs": "2.10.14",
1212
"react-native-pspdfkit": "file:../../",
1313
"react-native-windows": "0.51.0-rc.0",
1414
"react-navigation": "^1.0.3",

0 commit comments

Comments
 (0)