Skip to content

Commit 6b6e121

Browse files
Merge pull request #21 from PSPDFKit/rad/document-password
Add the ability to pass the document's password
2 parents ffc0b62 + 831ca70 commit 6b6e121

File tree

8 files changed

+55
-24
lines changed

8 files changed

+55
-24
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ function showMyDocument() {
2828
page: 4,
2929
scrollDirection: PSPDFKit.PageScrollDirection.VERTICAL,
3030
scrollMode: PSPDFKit.ScrollMode.CONTINUOUS,
31-
useImmersiveMode: true
31+
useImmersiveMode: true,
32+
password: "my-document-password"
3233
});
3334
}
3435
```
@@ -88,7 +89,8 @@ var options {
8889
annotationEditing: {
8990
enabled: true, // activate annotation editing (default: true)
9091
creatorName: 'John Doe' // author name written into new annotations (default: null)
91-
}
92+
},
93+
password: "my-document-password" // use this to open encrypted PDF files
9294
};
9395

9496
PSPDFKit.showDocumentFromAssets('www/documents/myFile.pdf', options);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pspdfkit-cordova-android",
3-
"version": "3.2.0",
3+
"version": "3.3.3",
44
"description": "Integration of the PSPDFKit for Android library.",
55
"cordova": {
66
"id": "pspdfkit-cordova-android",

plugin.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
~ UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
1212
~ This notice may not be removed from this file.
1313
-->
14-
<plugin id="pspdfkit-cordova-android" version="3.2.0" xmlns="http://apache.org/cordova/ns/plugins/1.0"
14+
<plugin id="pspdfkit-cordova-android" version="3.3.3" xmlns="http://apache.org/cordova/ns/plugins/1.0"
1515
xmlns:android="http://schemas.android.com/apk/res/android">
1616
<name>PSPDFKit-Android</name>
1717
<description>Integration of the PSPDFKit for Android library.</description>

src/android/java/com/pspdfkit/cordova/PSPDFKitCordovaPlugin.java

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public class PSPDFKitCordovaPlugin extends CordovaPlugin {
5151

5252
private static final int ARG_DOCUMENT_URI = 0;
5353
private static final int ARG_OPTIONS = 1;
54+
private static final int ARG_DOCUMENT_PASSWORD = 2;
5455

5556
@Override public void initialize(CordovaInterface cordova, CordovaWebView webView) {
5657
super.initialize(cordova, webView);
@@ -77,13 +78,14 @@ public class PSPDFKitCordovaPlugin extends CordovaPlugin {
7778

7879
@Override public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
7980
final PdfActivityConfiguration configuration = parseOptionsToConfiguration(args.getJSONObject(ARG_OPTIONS));
81+
final String password = convertJsonNullToJavaNull(args.getString(ARG_DOCUMENT_PASSWORD));
8082

8183
if (action.equals("showDocument")) {
8284
final Uri documentUri = Uri.parse(args.getString(ARG_DOCUMENT_URI));
83-
this.showDocument(documentUri, configuration, callbackContext);
85+
this.showDocument(documentUri, password, configuration, callbackContext);
8486
return true;
8587
} else if (action.equals("showDocumentFromAssets")) {
86-
this.showDocumentFromAssets(args.getString(ARG_DOCUMENT_URI), configuration, callbackContext);
88+
this.showDocumentFromAssets(args.getString(ARG_DOCUMENT_URI), password, configuration, callbackContext);
8789
return true;
8890
}
8991

@@ -152,7 +154,7 @@ public class PSPDFKitCordovaPlugin extends CordovaPlugin {
152154
} else if ("toGrayscale".equals(option)) {
153155
builder.toGrayscale((Boolean) value);
154156
} else if ("title".equals(option)) {
155-
builder.title(fromJsonString(options.getString("title")));
157+
builder.title(convertJsonNullToJavaNull(options.getString("title")));
156158
} else if ("startZoomScale".equals(option)) {
157159
builder.startZoomScale((float) options.getDouble("startZoomScale"));
158160
} else if ("maxZoomScale".equals(option)) {
@@ -182,7 +184,7 @@ public class PSPDFKitCordovaPlugin extends CordovaPlugin {
182184
if ((Boolean) annotationEditingValue) builder.enableAnnotationEditing();
183185
else builder.disableAnnotationEditing();
184186
} else if ("creatorName".equals(annotationEditingOption)) {
185-
PSPDFKitPreferences.get(activity).setAnnotationCreator(fromJsonString(annotationEditing.getString("creatorName")));
187+
PSPDFKitPreferences.get(activity).setAnnotationCreator(convertJsonNullToJavaNull(annotationEditing.getString("creatorName")));
186188
} else {
187189
throw new IllegalArgumentException(String.format("Invalid annotation editing option '%s'", annotationEditingOption));
188190
}
@@ -201,37 +203,37 @@ public class PSPDFKitCordovaPlugin extends CordovaPlugin {
201203
/**
202204
* Ensures that Javascript "null" strings are correctly converted to javas <code>null</code>.
203205
*/
204-
@Nullable private String fromJsonString(@Nullable String creatorName) {
205-
if (creatorName == null || creatorName.equals("null")) return null;
206-
return creatorName;
206+
@Nullable private static String convertJsonNullToJavaNull(@Nullable String value) {
207+
if (value == null || value.equals("null")) return null;
208+
return value;
207209
}
208210

209211
/**
210212
* Starts the {@link PdfActivity} to show a single document.
211213
* @param documentUri Local filesystem Uri pointing to a document.
212-
* @param configuration PSPDFKit configuration.
214+
* @param configuration PSPDFKit configuration
215+
* @param password PDF password
213216
* @param callbackContext Cordova callback.
214217
*/
215218

216-
private void showDocument(@NonNull Uri documentUri, @NonNull final PdfActivityConfiguration configuration,
217-
@NonNull final CallbackContext callbackContext) {
218-
showDocumentForUri(documentUri, configuration);
219+
private void showDocument(@NonNull Uri documentUri, @Nullable final String password, @NonNull final PdfActivityConfiguration configuration, @NonNull final CallbackContext callbackContext) {
220+
showDocumentForUri(documentUri, password, configuration);
219221
callbackContext.success();
220222
}
221223

222224
/**
223225
* Starts the {@link PdfActivity} to show a single document stored within the app's assets.
224226
* @param assetPath Relative path inside the app's assets folder.
225227
* @param configuration PSPDFKit configuration.
228+
* @param password PDF password
226229
* @param callbackContext Cordova callback.
227230
*/
228-
private void showDocumentFromAssets(@NonNull final String assetPath, @NonNull final PdfActivityConfiguration configuration,
229-
@NonNull final CallbackContext callbackContext) {
231+
private void showDocumentFromAssets(@NonNull final String assetPath, @Nullable final String password, @NonNull final PdfActivityConfiguration configuration, @NonNull final CallbackContext callbackContext) {
230232
ExtractAssetTask.extract(assetPath, cordova.getActivity(), new ExtractAssetTask.OnDocumentExtractedCallback() {
231233
@Override
232234
public void onDocumentExtracted(File documentFile) {
233235
if (documentFile != null) {
234-
showDocumentForUri(Uri.fromFile(documentFile), configuration);
236+
showDocumentForUri(Uri.fromFile(documentFile), password, configuration);
235237
callbackContext.success();
236238
} else {
237239
callbackContext.error("Could not load '" + assetPath + "' from the assets.");
@@ -240,7 +242,7 @@ public void onDocumentExtracted(File documentFile) {
240242
});
241243
}
242244

243-
private void showDocumentForUri(@NonNull Uri uri, @NonNull final PdfActivityConfiguration configuration) {
244-
PdfActivity.showDocument(cordova.getActivity(), uri, configuration);
245+
private void showDocumentForUri(@NonNull Uri uri, @Nullable final String password, @NonNull final PdfActivityConfiguration configuration) {
246+
PdfActivity.showDocument(cordova.getActivity(), uri, password, configuration);
245247
}
246248
}

tests/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pspdfkit-cordova-android-tests",
3-
"version": "3.2.0",
3+
"version": "3.3.3",
44
"description": "Test suite of the PSPDFKit for Android library Cordova plugin.",
55
"cordova": {
66
"id": "pspdfkit-cordova-android-tests",

tests/plugin.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
-->
1414
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
1515
id="pspdfkit-cordova-android-tests"
16-
version="3.2.0">
16+
version="3.3.3">
1717
<name>PSPDFKit-Android Tests</name>
1818
<license>MIT</license>
1919

tests/tests.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,20 @@ exports.defineManualTests = function(contentEl, createActionButton) {
9090
console.log('Error while loading the document:' + error)
9191
});
9292
});
93+
94+
createActionButton('Password protected document', function() {
95+
var asset = 'www/password.pdf';
96+
var options = {
97+
title: "Password protected document",
98+
password: "test123"
99+
};
100+
101+
console.log('Opening encrypted document ' + asset);
102+
103+
window.PSPDFKit.showDocumentFromAssets(asset, options, function() {
104+
console.log("Document was successfully loaded.");
105+
}, function(error) {
106+
console.log('Error while loading the document:' + error)
107+
});
108+
});
93109
};

www/PSPDFKit.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@
1313

1414
var exec = require('cordova/exec');
1515

16+
/**
17+
* Retrieves a named property from the given target object while removing the property from the object.
18+
*/
19+
function getPropertyAndUnset(target, name) {
20+
var value = target.hasOwnProperty(name) ? target[name] : null;
21+
delete target[name];
22+
return value;
23+
};
24+
1625
/**
1726
* Opens the PSPDFActivity to show a document from the local device file system.
1827
*
@@ -23,7 +32,8 @@ var exec = require('cordova/exec');
2332
*/
2433
exports.showDocument = function (uri, options, success, error) {
2534
options = options || {};
26-
exec(success, error, "PSPDFKitCordovaPlugin", "showDocument", [uri, options]);
35+
var password = getPropertyAndUnset(options, "password");
36+
exec(success, error, "PSPDFKitCordovaPlugin", "showDocument", [uri, options, password]);
2737
};
2838

2939
/**
@@ -38,7 +48,8 @@ exports.showDocument = function (uri, options, success, error) {
3848
*/
3949
exports.showDocumentFromAssets = function (assetFile, options, success, error) {
4050
options = options || {};
41-
exec(success, error, "PSPDFKitCordovaPlugin", "showDocumentFromAssets", [assetFile, options]);
51+
var password = getPropertyAndUnset(options, "password");
52+
exec(success, error, "PSPDFKitCordovaPlugin", "showDocumentFromAssets", [assetFile, options, password]);
4253
};
4354

4455
/**

0 commit comments

Comments
 (0)