Skip to content

Commit 7edb28b

Browse files
Implement version-and-bookmark (#138)
* Implement version-and-bookmark * Update ver and platformVer to RNPdftron * update README for new functionalities * Fix some problems with version-and-bookmark * Update bookmark event doc check * cleanup unused import * return promise for import-bookmark-json * update selector if check for bookmark event in iOS
1 parent 2268d91 commit 7edb28b

File tree

12 files changed

+328
-19
lines changed

12 files changed

+328
-19
lines changed

README.md

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -311,11 +311,23 @@ initialize(string)
311311
#### enableJavaScript
312312
enableJavaScript(bool)
313313
314+
#### getVersion
315+
getVersion()
316+
317+
Return a promise with the version of the PDFNet version used.
318+
319+
### getPlatformVersion
320+
getPlatformVersion()
321+
322+
Return a promise with the version of current platform (Android/iOS).
323+
314324
#### encryptDocument
315-
encryptDocument(string, string, string, Promise)
325+
encryptDocument(string, string, string)
316326
317327
This function does not lock around the document so be sure to not use it while the document is opened in the viewer.
318328
329+
Return a promise.
330+
319331
Example:
320332
321333
```js
@@ -331,7 +343,6 @@ Name | Type | Description
331343
file path | string | the local file path to the file
332344
password | string | the password
333345
current password | string | the current password, use empty string if no password
334-
335346
## Components
336347
337348
### DocumentView
@@ -375,6 +386,7 @@ A component for displaying documents of different types such as PDF, docx, pptx,
375386
- [useStylusAsPen](#usestylusaspen)
376387
- [signSignatureFieldsWithStamps](#signsignaturefieldswithstamps)
377388
- [longPressMenuEnabled](#longPressMenuEnabled)
389+
- [onBookmarkChanged](#onBookmarkChanged)
378390
379391
##### document
380392
string, required
@@ -571,6 +583,26 @@ bool, optional, default to false
571583
572584
If true, annotation's flags will be taken into account when it is selected, for example, a locked annotation can not be resized or moved.
573585
586+
##### onFormFieldValueChanged
587+
function, optional
588+
589+
Parameters:
590+
591+
Name | Type | Description
592+
--- | --- | ---
593+
fields | array | array of field data in the format `{fieldName: string, fieldValue: string}`
594+
595+
##### onBookmarkChanged
596+
function, optional
597+
598+
Defines what happens if a change has been made to bookmarks
599+
600+
Parameters:
601+
602+
Name | Type | Description
603+
--- | --- | ---
604+
bookmarkJson | string | the list of current bookmarks in JSON format
605+
574606
Example:
575607
576608
```js
@@ -595,19 +627,10 @@ import { DocumentView, Config } from 'react-native-pdftron';
595627
onPageChanged={({previousPageNumber, pageNumber}) => { console.log('page changed'); }}
596628
onAnnotationChanged={({action, annotations}) => { console.log('annotations changed'); }}
597629
annotationPermissionCheckEnabled={false}
630+
onBookmarkChanged={({bookmarkJson}) => { console.log('bookmark changed'); }}
598631
/>
599632
```
600633
601-
##### onFormFieldValueChanged
602-
function, optional
603-
604-
Parameters:
605-
606-
Name | Type | Description
607-
--- | --- | ---
608-
fields | array | array of field data in the format `{fieldName: string, fieldValue: string}`
609-
610-
611634
#### Methods
612635
- [setToolMode](#settoolmode)
613636
- [commitTool](#committool)
@@ -625,6 +648,7 @@ fields | array | array of field data in the format `{fieldName: string, fieldVal
625648
- [setFlagForAnnotations](#setFlagForAnnotations)
626649
- [setPropertyForAnnotation](#setPropertyForAnnotation)
627650
- [getPageCropBox](#getPageCropBox)
651+
- [importBookmarkJson](#importBookmarkJson)
628652
- [setCurrentPage](#setCurrentPage)
629653
- [getDocumentPath](#getDocumentPath)
630654
@@ -898,6 +922,21 @@ this._viewer.getPageCropBox(1).then((cropBox) => {
898922
});
899923
```
900924
925+
##### importBookmarkJson
926+
Imports user bookmarks to the document. The input needs to be a valid bookmark JSON format, for example {"0":"Page 1"}.
927+
928+
Parameters:
929+
930+
Name | Type | Description
931+
--- | --- | ---
932+
bookmarkJson | String | needs to be in valid bookmark JSON format, for example {"0": "Page 1"}. The page numbers are 1-indexed
933+
934+
Return a Promise.
935+
936+
```js
937+
this._viewer.importBookmarkJson("{\"0\": \"Page 1\", \"3\": \"Page 4\"}");
938+
```
939+
901940
##### setCurrentPage
902941
Set current page of the document.
903942

android/src/main/java/com/pdftron/reactnative/modules/DocumentViewModule.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@ public String getName() {
3131
return REACT_CLASS;
3232
}
3333

34+
@ReactMethod
35+
public void importBookmarkJson(final int tag, final String bookmarkJson, final Promise promise) {
36+
getReactApplicationContext().runOnUiQueueThread(new Runnable() {
37+
@Override
38+
public void run() {
39+
try {
40+
mDocumentViewInstance.importBookmarkJson(tag, bookmarkJson);
41+
promise.resolve(null);
42+
} catch (Exception ex) {
43+
promise.reject(ex);
44+
}
45+
}
46+
});
47+
}
48+
3449
@ReactMethod
3550
public void importAnnotationCommand(final int tag, final String xfdfCommand, final boolean initialLoad, final Promise promise) {
3651
getReactApplicationContext().runOnUiQueueThread(new Runnable() {

android/src/main/java/com/pdftron/reactnative/modules/RNPdftronModule.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,32 @@ public void encryptDocument(final String filePath, final String password, final
6666
promise.reject(ex);
6767
}
6868
}
69+
70+
@ReactMethod
71+
public void getVersion(final Promise promise) {
72+
getReactApplicationContext().runOnUiQueueThread(new Runnable() {
73+
@Override
74+
public void run() {
75+
try {
76+
promise.resolve(Double.toString(PDFNet.getVersion()));
77+
} catch (Exception ex) {
78+
promise.reject(ex);
79+
}
80+
}
81+
});
82+
}
83+
84+
@ReactMethod
85+
public void getPlatformVersion(final Promise promise) {
86+
getReactApplicationContext().runOnUiQueueThread(new Runnable() {
87+
@Override
88+
public void run() {
89+
try {
90+
promise.resolve("Android " + android.os.Build.VERSION.RELEASE);
91+
} catch (Exception ex) {
92+
promise.reject(ex);
93+
}
94+
}
95+
});
96+
}
6997
}

android/src/main/java/com/pdftron/reactnative/viewmanagers/DocumentViewViewManager.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,15 @@ public void setAnnotationPermissionCheckEnabled(DocumentView documentView, boole
247247
documentView.setAnnotationPermissionCheckEnabled(annotPermissionCheckEnabled);
248248
}
249249

250+
public void importBookmarkJson(int tag, String bookmarkJson) throws PDFNetException {
251+
DocumentView documentView = mDocumentViews.get(tag);
252+
if (documentView != null) {
253+
documentView.importBookmarkJson(bookmarkJson);
254+
} else {
255+
throw new PDFNetException("", 0L, getName(), "importBookmarkJson", "Unable to find DocumentView.");
256+
}
257+
}
258+
250259
public void importAnnotationCommand(int tag, String xfdfCommand, boolean initialLoad) throws PDFNetException {
251260
DocumentView documentView = mDocumentViews.get(tag);
252261
if (documentView != null) {

android/src/main/java/com/pdftron/reactnative/views/DocumentView.java

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import com.pdftron.pdf.tools.ToolManager;
5858
import com.pdftron.pdf.utils.ActionUtils;
5959
import com.pdftron.pdf.utils.AnnotUtils;
60+
import com.pdftron.pdf.utils.BookmarkManager;
6061
import com.pdftron.pdf.utils.CommonToast;
6162
import com.pdftron.pdf.utils.PdfDocManager;
6263
import com.pdftron.pdf.utils.PdfViewCtrlSettingsManager;
@@ -801,6 +802,7 @@ protected void onDetachedFromWindow() {
801802
if (getToolManager() != null) {
802803
getToolManager().removeAnnotationModificationListener(mAnnotationModificationListener);
803804
getToolManager().removeAnnotationsSelectionListener(mAnnotationsSelectionListener);
805+
getToolManager().removePdfDocModificationListener(mPdfDocModificationListener);
804806
}
805807
if (getPdfViewCtrlTabFragment() != null) {
806808
getPdfViewCtrlTabFragment().removeQuickMenuListener(mQuickMenuListener);
@@ -1161,6 +1163,64 @@ public void annotationsCouldNotBeAdded(String s) {
11611163
}
11621164
};
11631165

1166+
private ToolManager.PdfDocModificationListener mPdfDocModificationListener = new ToolManager.PdfDocModificationListener() {
1167+
@Override
1168+
public void onBookmarkModified() {
1169+
if (getPdfDoc() != null) {
1170+
WritableMap params = Arguments.createMap();
1171+
params.putString(ON_BOOKMARK_CHANGED, ON_BOOKMARK_CHANGED);
1172+
String bookmarkJson = null;
1173+
try {
1174+
bookmarkJson = BookmarkManager.exportPdfBookmarks(getPdfDoc());
1175+
} catch (JSONException ex) {
1176+
ex.printStackTrace();
1177+
}
1178+
params.putString(KEY_bookmark_json, bookmarkJson);
1179+
onReceiveNativeEvent(params);
1180+
}
1181+
}
1182+
1183+
@Override
1184+
public void onPagesCropped() {
1185+
1186+
}
1187+
1188+
@Override
1189+
public void onPagesAdded(List<Integer> list) {
1190+
1191+
}
1192+
1193+
@Override
1194+
public void onPagesDeleted(List<Integer> list) {
1195+
1196+
}
1197+
1198+
@Override
1199+
public void onPagesRotated(List<Integer> list) {
1200+
1201+
}
1202+
1203+
@Override
1204+
public void onPageMoved(int i, int i1) {
1205+
1206+
}
1207+
1208+
@Override
1209+
public void onPageLabelsChanged() {
1210+
1211+
}
1212+
1213+
@Override
1214+
public void onAllAnnotationsRemoved() {
1215+
1216+
}
1217+
1218+
@Override
1219+
public void onAnnotationAction() {
1220+
1221+
}
1222+
};
1223+
11641224
private void handleAnnotationChanged(String action, Map<Annot, Integer> map) {
11651225
WritableMap params = Arguments.createMap();
11661226
params.putString(ON_ANNOTATION_CHANGED, ON_ANNOTATION_CHANGED);
@@ -1250,6 +1310,7 @@ public void onTabDocumentLoaded(String tag) {
12501310

12511311
getToolManager().addAnnotationModificationListener(mAnnotationModificationListener);
12521312
getToolManager().addAnnotationsSelectionListener(mAnnotationsSelectionListener);
1313+
getToolManager().addPdfDocModificationListener(mPdfDocModificationListener);
12531314

12541315
getToolManager().setStylusAsPen(mUseStylusAsPen);
12551316
getToolManager().setSignSignatureFieldsWithStamps(mSignWithStamps);
@@ -1316,6 +1377,42 @@ public boolean onOpenDocError() {
13161377
return true;
13171378
}
13181379

1380+
public void importBookmarkJson(String bookmarkJson) throws PDFNetException {
1381+
PDFViewCtrl pdfViewCtrl = getPdfViewCtrl();
1382+
1383+
PDFDoc pdfDoc = pdfViewCtrl.getDoc();
1384+
1385+
boolean shouldUnlockRead = false;
1386+
try {
1387+
pdfViewCtrl.docLockRead();
1388+
shouldUnlockRead = true;
1389+
1390+
if (pdfDoc.hasDownloader()) {
1391+
// still downloading file, let's wait for next call
1392+
return;
1393+
}
1394+
} finally {
1395+
if (shouldUnlockRead) {
1396+
pdfViewCtrl.docUnlockRead();
1397+
}
1398+
}
1399+
1400+
boolean shouldUnlock = false;
1401+
try {
1402+
pdfViewCtrl.docLock(true);
1403+
shouldUnlock = true;
1404+
1405+
BookmarkManager.importPdfBookmarks(pdfViewCtrl, bookmarkJson);
1406+
pdfViewCtrl.update(true);
1407+
} catch (JSONException ex) {
1408+
throw new PDFNetException("", 0L, TAG, "importBookmarkJson", "Unable to parse bookmark json.");
1409+
} finally {
1410+
if (shouldUnlock) {
1411+
pdfViewCtrl.docUnlock();
1412+
}
1413+
}
1414+
}
1415+
13191416
public void importAnnotationCommand(String xfdfCommand, boolean initialLoad) throws PDFNetException {
13201417
if (mCollabManager != null) {
13211418
mCollabManager.importAnnotationCommand(xfdfCommand, initialLoad);

ios/RNPdftron.m

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,28 @@ - (dispatch_queue_t)methodQueue
5252
}
5353
}
5454

55+
RCT_EXPORT_METHOD(getVersion:(RCTPromiseResolveBlock)resolve
56+
rejecter:(RCTPromiseRejectBlock)reject)
57+
{
58+
@try {
59+
resolve([@"PDFNet " stringByAppendingFormat:@"%f", [PTPDFNet GetVersion]]);
60+
}
61+
@catch (NSException *exception) {
62+
reject(@"get_failed", @"Failed to get PDFNet version", [self errorFromException:exception]);
63+
}
64+
}
65+
66+
RCT_EXPORT_METHOD(getPlatformVersion:(RCTPromiseResolveBlock)resolve
67+
rejecter:(RCTPromiseRejectBlock)reject)
68+
{
69+
@try {
70+
resolve([@"iOS " stringByAppendingString:[[UIDevice currentDevice] systemVersion]]);
71+
}
72+
@catch (NSException *exception) {
73+
reject(@"get_failed", @"Failed to get platform version", [self errorFromException:exception]);
74+
}
75+
}
76+
5577
- (void)setPassword:(NSString *)password onPDFDoc:(PTPDFDoc *)pdfDoc
5678
{
5779
if (!pdfDoc) {

ios/RNTPTDocumentView.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ static NSString * const PTFormFieldValueKey = @"fieldValue";
177177

178178
- (void)longPressMenuPressed:(RNTPTDocumentView *)sender longPressMenu:(NSString *)longPressMenu longPressText:(NSString *)longPressText;
179179

180+
- (void)bookmarkChanged:(RNTPTDocumentView *)sender bookmarkJson:(NSString *)bookmarkJson;
181+
180182
@end
181183

182184
@interface RNTPTDocumentView : UIView
@@ -260,9 +262,12 @@ static NSString * const PTFormFieldValueKey = @"fieldValue";
260262

261263
- (int)getPageCount;
262264

265+
- (void)importBookmarkJson:(NSString *)bookmarkJson;
266+
263267
- (NSString *)getDocumentPath;
264268

265269
- (nullable NSString *)exportAnnotationsWithOptions:(NSDictionary *)options;
270+
266271
- (void)importAnnotations:(NSString *)xfdfString;
267272

268273
- (void)flattenAnnotations:(BOOL)formsOnly;

0 commit comments

Comments
 (0)