forked from PSPDFKit/react-native
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
2136 lines (1981 loc) · 85.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright © 2018-2024 PSPDFKit GmbH. All rights reserved.
//
// 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 PSPDFKIT LICENSE AGREEMENT.
// UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
// This notice may not be removed from this file.
//
import PropTypes from 'prop-types';
import * as React from 'react';
import {
findNodeHandle,
NativeModules,
Platform,
requireNativeComponent,
UIManager,
} from 'react-native';
/**
* PSPDFKitView is a React Native component used to view PDF documents on iOS and Android.
* @augments {React.Component<Props, *>}
* @hideconstructor
* @example
* <PSPDFKitView
* document={DOCUMENT_PATH}
* configuration={{
* showThumbnailBar: PDFConfiguration.ShowThumbnailBar.SCROLLABLE,
* pageTransition: PDFConfiguration.PageTransition.SCROLL_CONTINUOUS,
* scrollDirection: PDFConfiguration.ScrollDirection.VERTICAL,
* }}
* ref={this.pdfRef}
* fragmentTag="PDF1"
* style={{ flex: 1 }}
* />
*/
class PSPDFKitView extends React.Component {
/**
* @ignore
*/
_nextRequestId = 1;
/**
* @ignore
*/
_requestMap = new Map();
/**
* @ignore
*/
_pdfDocument = null;
/**
* @ignore
*/
_componentRef = React.createRef(this);
render() {
if (Platform.OS === 'ios' || Platform.OS === 'android') {
const onCloseButtonPressedHandler = this.props.onCloseButtonPressed
? event => {
this.props.onCloseButtonPressed(event.nativeEvent);
}
: null;
return (
<RCTPSPDFKitView
ref={this._componentRef}
fragmentTag="PSPDFKitView.FragmentTag"
{...this.props}
onCloseButtonPressed={onCloseButtonPressedHandler}
onStateChanged={this._onStateChanged}
onDocumentSaved={this._onDocumentSaved}
onDocumentLoaded={this._onDocumentLoaded}
onDocumentSaveFailed={this._onDocumentSaveFailed}
onDocumentLoadFailed={this._onDocumentLoadFailed}
onAnnotationTapped={this._onAnnotationTapped}
onAnnotationsChanged={this._onAnnotationsChanged}
onNavigationButtonClicked={this._onNavigationButtonClicked}
onDataReturned={this._onDataReturned}
onCustomToolbarButtonTapped={this._onCustomToolbarButtonTapped}
onCustomAnnotationContextualMenuItemTapped={
this._onCustomAnnotationContextualMenuItemTapped
}
/>
);
} else {
return null;
}
}
/**
* @ignore
*/
_onStateChanged = event => {
if (this.props.onStateChanged) {
this.props.onStateChanged(event.nativeEvent);
}
};
/**
* @ignore
*/
_onDocumentLoaded = event => {
if (this.props.onDocumentLoaded) {
this.props.onDocumentLoaded(event.nativeEvent);
}
};
/**
* @ignore
*/
_onDocumentSaved = event => {
if (this.props.onDocumentSaved) {
this.props.onDocumentSaved(event.nativeEvent);
}
};
/**
* @ignore
*/
_onDocumentSaveFailed = event => {
if (this.props.onDocumentSaveFailed) {
this.props.onDocumentSaveFailed(event.nativeEvent);
}
};
/**
* @ignore
*/
_onDocumentLoadFailed = event => {
if (this.props.onDocumentLoadFailed) {
this.props.onDocumentLoadFailed(event.nativeEvent);
}
};
/**
* @ignore
*/
_onAnnotationTapped = event => {
if (this.props.onAnnotationTapped) {
this.props.onAnnotationTapped(event.nativeEvent);
}
};
/**
* @ignore
*/
_onAnnotationsChanged = event => {
if (this.props.onAnnotationsChanged) {
this.props.onAnnotationsChanged(event.nativeEvent);
}
};
/**
* @ignore
*/
_onNavigationButtonClicked = event => {
if (this.props.onNavigationButtonClicked) {
this.props.onNavigationButtonClicked(event.nativeEvent);
}
};
/**
* @ignore
*/
_onDataReturned = event => {
let { requestId, result, error } = event.nativeEvent;
let promise = this._requestMap[requestId];
if (result !== undefined) {
promise.resolve(result);
} else {
promise.reject(error);
}
this._requestMap.delete(requestId);
};
/**
* @ignore
*/
_onCustomToolbarButtonTapped = event => {
if (this.props.onCustomToolbarButtonTapped) {
this.props.onCustomToolbarButtonTapped(event.nativeEvent);
}
};
/**
* @ignore
*/
_onCustomAnnotationContextualMenuItemTapped = event => {
if (this.props.onCustomAnnotationContextualMenuItemTapped) {
this.props.onCustomAnnotationContextualMenuItemTapped(event.nativeEvent);
}
};
/**
* Enters annotation creation mode, showing the annotation creation toolbar.
* @method enterAnnotationCreationMode
* @example
* this.pdfRef.current.enterAnnotationCreationMode();
* @memberof PSPDFKitView
*/
enterAnnotationCreationMode = function () {
if (Platform.OS === 'android') {
UIManager.dispatchViewManagerCommand(
findNodeHandle(this._componentRef.current),
this._getViewManagerConfig('RCTPSPDFKitView').Commands
.enterAnnotationCreationMode,
[],
);
} else if (Platform.OS === 'ios') {
return NativeModules.PSPDFKitViewManager.enterAnnotationCreationMode(
findNodeHandle(this._componentRef.current),
);
}
};
/**
* Exits the currently active mode, hiding all toolbars.
* @method exitCurrentlyActiveMode
* @example
* this.pdfRef.current.exitCurrentlyActiveMode();
* @memberof PSPDFKitView
*/
exitCurrentlyActiveMode = function () {
if (Platform.OS === 'android') {
UIManager.dispatchViewManagerCommand(
findNodeHandle(this._componentRef.current),
this._getViewManagerConfig('RCTPSPDFKitView').Commands
.exitCurrentlyActiveMode,
[],
);
} else if (Platform.OS === 'ios') {
return NativeModules.PSPDFKitViewManager.exitCurrentlyActiveMode(
findNodeHandle(this._componentRef.current),
);
}
};
/**
* Saves the document that’s currently open.
* @deprecated Since PSPDFKit for React Native 2.12. Use ```this.pdfRef.current?.getDocument().save()``` instead.
* See {@link https://pspdfkit.com/api/react-native/PDFDocument.html#.save|save()}.
* @method saveCurrentDocument
* @memberof PSPDFKitView
* @example
* const result = await this.pdfRef.current.saveCurrentDocument();
*
* @returns { Promise<boolean> } A promise resolving to ```true``` if the document was saved, and ```false``` if not.
*/
saveCurrentDocument = function () {
if (Platform.OS === 'android') {
let requestId = this._nextRequestId++;
let requestMap = this._requestMap;
// We create a promise here that will be resolved once onDataReturned is called.
let promise = new Promise(function (resolve, reject) {
requestMap[requestId] = { resolve: resolve, reject: reject };
});
UIManager.dispatchViewManagerCommand(
findNodeHandle(this._componentRef.current),
this._getViewManagerConfig('RCTPSPDFKitView').Commands
.saveCurrentDocument,
[requestId],
);
return promise;
} else if (Platform.OS === 'ios') {
return NativeModules.PSPDFKitViewManager.saveCurrentDocument(
findNodeHandle(this._componentRef.current),
);
}
};
/**
* Get the current PDF document.
* @method getDocument
* @example
* const document = this.pdfRef.current?.getDocument();
* @see {@link https://pspdfkit.com/api/react-native/PDFDocument.html} for available methods.
* @memberof PSPDFKitView
* @returns { PDFDocument } A reference to the document that is currently loaded in the PSPDFKitView component.
*/
getDocument () {
if (this._pdfDocument == null) {
this._pdfDocument = new PDFDocument(this._componentRef.current);
return this._pdfDocument;
} else {
return this._pdfDocument;
}
};
/**
* @method clearSelectedAnnotations
* @memberof PSPDFKitView
* @description Clears all currently selected Annotations.
* @example
* const result = await this.pdfRef.current?.clearSelectedAnnotations();
* @returns { Promise<any> } A promise containing the result of the operation. ```true``` if the annotations selection were cleared, ```false``` otherwise.
*/
clearSelectedAnnotations = function () {
if (Platform.OS === 'android') {
let requestId = this._nextRequestId++;
let requestMap = this._requestMap;
// We create a promise here that will be resolved once onDataReturned is called.
let promise = new Promise(function (resolve, reject) {
requestMap[requestId] = { resolve: resolve, reject: reject };
});
UIManager.dispatchViewManagerCommand(
findNodeHandle(this._componentRef.current),
this._getViewManagerConfig('RCTPSPDFKitView').Commands
.clearSelectedAnnotations,
[requestId],
);
return promise;
} else if (Platform.OS === 'ios') {
return NativeModules.PSPDFKitViewManager.clearSelectedAnnotations(
findNodeHandle(this._componentRef.current),
);
}
};
/**
* @method selectAnnotations
* @memberof PSPDFKitView
* @param { object } annotations An array of the annotations to select in Instant JSON format.
* @description Select one or more annotations.
* @example
* const result = await this.pdfRef.current?.selectAnnotations(annotations);
* @returns { Promise<any> } A promise containing the result of the operation. ```true``` if the annotations were selected, ```false``` otherwise.
*/
selectAnnotations = function (annotations) {
if (Platform.OS === 'android') {
let requestId = this._nextRequestId++;
let requestMap = this._requestMap;
// We create a promise here that will be resolved once onDataReturned is called.
let promise = new Promise(function (resolve, reject) {
requestMap[requestId] = { resolve: resolve, reject: reject };
});
UIManager.dispatchViewManagerCommand(
findNodeHandle(this._componentRef.current),
this._getViewManagerConfig('RCTPSPDFKitView').Commands
.selectAnnotations,
[requestId, annotations],
);
return promise;
} else if (Platform.OS === 'ios') {
return NativeModules.PSPDFKitViewManager.selectAnnotations(
annotations,
findNodeHandle(this._componentRef.current),
);
}
};
/**
* Gets all annotations of the given type from the specified page.
*
* @method getAnnotations
* @deprecated Since PSPDFKit for React Native 2.12. Use ```this.pdfRef.current?.getDocument().getAnnotations()``` or ```getAnnotationsForPage()``` instead.
* See {@link https://pspdfkit.com/api/react-native/PDFDocument.html#.getAnnotations|getAnnotations()} and {@link https://pspdfkit.com/api/react-native/PDFDocument.html#.getAnnotationsForPage|getAnnotationsForPage()}.
* @memberof PSPDFKitView
* @param { number } pageIndex The page index to get the annotations for, starting at 0.
* @param { string } [type] The type of annotations to get. If not specified or ```null```, all annotation types will be returned.
* @example
* const result = await this.pdfRef.current.getAnnotations(3, 'pspdfkit/ink');
* @see {@link https://pspdfkit.com/guides/web/json/schema/annotations/} for supported types.
*
* @returns { Promise } A promise containing an object with an array of InstantJSON objects.
*/
getAnnotations = function (pageIndex, type) {
if (Platform.OS === 'android') {
let requestId = this._nextRequestId++;
let requestMap = this._requestMap;
// We create a promise here that will be resolved once onDataReturned is called.
let promise = new Promise(function (resolve, reject) {
requestMap[requestId] = { resolve: resolve, reject: reject };
});
UIManager.dispatchViewManagerCommand(
findNodeHandle(this._componentRef.current),
this._getViewManagerConfig('RCTPSPDFKitView').Commands.getAnnotations,
[requestId, pageIndex, type],
);
return promise;
} else if (Platform.OS === 'ios') {
return NativeModules.PSPDFKitViewManager.getAnnotations(
pageIndex,
type,
findNodeHandle(this._componentRef.current),
);
}
};
/**
* Adds a new annotation to the current document.
*
* @method addAnnotation
* @deprecated Since PSPDFKit for React Native 2.12. Use ```this.pdfRef.current?.getDocument().addAnnotations()``` instead.
* @memberof PSPDFKitView
* @param { object } annotation The InstantJSON of the annotation to add.
* @example
* const result = await this.pdfRef.current.addAnnotation(annotationJSON);
* @see {@link https://pspdfkit.com/guides/web/json/schema/annotations/} for document JSON structure.
*
* @returns { Promise<boolean> } A promise resolving to ```true``` if the annotation was added successfully, and ```false``` if an error occurred.
*/
addAnnotation = function (annotation) {
if (Platform.OS === 'android') {
let requestId = this._nextRequestId++;
let requestMap = this._requestMap;
// We create a promise here that will be resolved once onDataReturned is called.
let promise = new Promise(function (resolve, reject) {
requestMap[requestId] = { resolve: resolve, reject: reject };
});
UIManager.dispatchViewManagerCommand(
findNodeHandle(this._componentRef.current),
this._getViewManagerConfig('RCTPSPDFKitView').Commands.addAnnotation,
[requestId, annotation],
);
return promise;
} else if (Platform.OS === 'ios') {
return NativeModules.PSPDFKitViewManager.addAnnotation(
annotation,
findNodeHandle(this._componentRef.current),
);
}
};
/**
* Removes an existing annotation from the current document.
*
* @method removeAnnotation
* @deprecated Since PSPDFKit for React Native 2.12. Use ```this.pdfRef.current?.getDocument().removeAnnotations()``` instead.
* See {@link https://pspdfkit.com/api/react-native/PDFDocument.html#.removeAnnotations|removeAnnotations()}.
* @memberof PSPDFKitView
* @param { object } annotation The InstantJSON of the annotation to remove.
* @example
* const result = await this.pdfRef.current.removeAnnotation(instantJSON);
*
* @returns { Promise } A promise resolving to ```true``` if the annotation was removed successfully, and ```false``` if the annotation couldn’t be found or an error occurred.
*/
removeAnnotation = function (annotation) {
if (Platform.OS === 'android') {
let requestId = this._nextRequestId++;
let requestMap = this._requestMap;
// We create a promise here that will be resolved once onDataReturned is called.
let promise = new Promise(function (resolve, reject) {
requestMap[requestId] = { resolve: resolve, reject: reject };
});
UIManager.dispatchViewManagerCommand(
findNodeHandle(this._componentRef.current),
this._getViewManagerConfig('RCTPSPDFKitView').Commands.removeAnnotation,
[requestId, annotation],
);
return promise;
} else if (Platform.OS === 'ios') {
return NativeModules.PSPDFKitViewManager.removeAnnotation(
annotation,
findNodeHandle(this._componentRef.current),
);
}
};
/**
* Removes the supplied document InstantJSON from the current document.
*
* @method removeAnnotations
* @deprecated Since PSPDFKit for React Native 2.12. Use ```this.pdfRef.current?.getDocument().removeAnnotations()``` instead.
* See {@link https://pspdfkit.com/api/react-native/PDFDocument.html#.removeAnnotations|removeAnnotations()}.
* @memberof PSPDFKitView
* @param { object } annotation The InstantJSON of the annotations to remove.
* @example
* const result = await this.pdfRef.current.removeAnnotations(instantJSON);
*
* @returns { Promise } A promise resolving to ```true``` if the annotations were removed successfully, and ```false``` if the annotations couldn’t be found or an error occurred.
*/
removeAnnotations = function (annotations) {
if (Platform.OS === 'android') {
let requestId = this._nextRequestId++;
let requestMap = this._requestMap;
// We create a promise here that will be resolved once onDataReturned is called.
let promise = new Promise(function (resolve, reject) {
requestMap[requestId] = { resolve: resolve, reject: reject };
});
UIManager.dispatchViewManagerCommand(
findNodeHandle(this._componentRef.current),
this._getViewManagerConfig('RCTPSPDFKitView').Commands
.removeAnnotations,
[requestId, annotations],
);
return promise;
} else if (Platform.OS === 'ios') {
return NativeModules.PSPDFKitViewManager.removeAnnotations(
annotations,
findNodeHandle(this._componentRef.current),
);
}
};
/**
* Gets all unsaved changes to annotations.
*
* @method getAllUnsavedAnnotations
* @deprecated Since PSPDFKit for React Native 2.12. Use ```this.pdfRef.current?.getDocument().getAllUnsavedAnnotations()``` instead.
* See {@link https://pspdfkit.com/api/react-native/PDFDocument.html#.getAllUnsavedAnnotations|getAllUnsavedAnnotations()}.
* @memberof PSPDFKitView
* @returns { Promise } A promise containing document InstantJSON.
* @see {@link https://pspdfkit.com/guides/android/current/importing-exporting/instant-json/#instant-document-json-api-a56628} for more information.
*/
getAllUnsavedAnnotations = function () {
if (Platform.OS === 'android') {
let requestId = this._nextRequestId++;
let requestMap = this._requestMap;
// We create a promise here that will be resolved once onDataReturned is called.
let promise = new Promise(function (resolve, reject) {
requestMap[requestId] = { resolve: resolve, reject: reject };
});
UIManager.dispatchViewManagerCommand(
findNodeHandle(this._componentRef.current),
this._getViewManagerConfig('RCTPSPDFKitView').Commands
.getAllUnsavedAnnotations,
[requestId],
);
return promise;
} else if (Platform.OS === 'ios') {
return NativeModules.PSPDFKitViewManager.getAllUnsavedAnnotations(
findNodeHandle(this._componentRef.current),
);
}
};
/**
* Gets all annotations of the given type.
*
* @method getAllAnnotations
* @deprecated Since PSPDFKit for React Native 2.12. Use ```this.pdfRef.current?.getDocument().getAnnotations()``` instead.
* See {@link https://pspdfkit.com/api/react-native/PDFDocument.html#.getAnnotations|getAnnotations()}.
* @memberof PSPDFKitView
* @param { string } [type] The type of annotations to get. If not specified or ```null```, all annotation types will be returned.
* @see {@link https://pspdfkit.com/guides/web/json/schema/annotations/} for supported types.
* @example
* const result = await this.pdfRef.current.getAllAnnotations('all');
* // result: {'annotations' : [instantJson]}
*
* @returns { Promise } A promise containing an object with an array of InstantJSON objects.
*/
getAllAnnotations = function (type) {
if (Platform.OS === 'android') {
let requestId = this._nextRequestId++;
let requestMap = this._requestMap;
// We create a promise here that will be resolved once onDataReturned is called.
let promise = new Promise(function (resolve, reject) {
requestMap[requestId] = { resolve: resolve, reject: reject };
});
UIManager.dispatchViewManagerCommand(
findNodeHandle(this._componentRef.current),
this._getViewManagerConfig('RCTPSPDFKitView').Commands
.getAllAnnotations,
[requestId, type],
);
return promise;
} else if (Platform.OS === 'ios') {
return NativeModules.PSPDFKitViewManager.getAllAnnotations(
type,
findNodeHandle(this._componentRef.current),
);
}
};
/**
* Applies the supplied document InstantJSON to the current document.
*
* @method addAnnotations
* @deprecated Since PSPDFKit for React Native 2.12. Use ```this.pdfRef.current?.getDocument().addAnnotations()``` instead.
* See {@link https://pspdfkit.com/api/react-native/PDFDocument.html#.addAnnotations|addAnnotations()}.
* @memberof PSPDFKitView
* @param { object } annotations The document InstantJSON to apply to the current document.
* @example
* const result = await this.pdfRef.current.addAnnotations(annotationsJSON);
* @see {@link https://pspdfkit.com/guides/web/json/schema/annotations/} for document JSON structure.
*
* @returns { Promise<boolean> } A promise resolving to ```true``` if the annotations were added successfully, and ```false``` if an error occurred.
*/
addAnnotations = function (annotations) {
if (Platform.OS === 'android') {
let requestId = this._nextRequestId++;
let requestMap = this._requestMap;
// We create a promise here that will be resolved once onDataReturned is called.
let promise = new Promise(function (resolve, reject) {
requestMap[requestId] = { resolve: resolve, reject: reject };
});
UIManager.dispatchViewManagerCommand(
findNodeHandle(this._componentRef.current),
this._getViewManagerConfig('RCTPSPDFKitView').Commands.addAnnotations,
[requestId, annotations],
);
return promise;
} else if (Platform.OS === 'ios') {
return NativeModules.PSPDFKitViewManager.addAnnotations(
annotations,
findNodeHandle(this._componentRef.current),
);
}
};
/**
* Sets the flags of the specified annotation.
*
* @method setAnnotationFlags
* @memberof PSPDFKitView
* @param { string } uuid The UUID of the annotation to update.
* @param { Annotation.Flags[] } flags The flags to apply to the annotation.
* @example
* const result = await this.pdfRef.current.setAnnotationFlags('bb61b1bf-eacd-4227-a5bf-db205e591f5a', ['locked', 'hidden']);
*
* @returns { Promise<boolean> } A promise resolving to ```true``` if the annotations were added successfully, and ```false``` if an error occurred.
*/
setAnnotationFlags = function (uuid, flags) {
if (Platform.OS === 'android') {
let requestId = this._nextRequestId++;
let requestMap = this._requestMap;
// We create a promise here that will be resolved once onDataReturned is called.
let promise = new Promise(function (resolve, reject) {
requestMap[requestId] = { resolve: resolve, reject: reject };
});
UIManager.dispatchViewManagerCommand(
findNodeHandle(this._componentRef.current),
this._getViewManagerConfig('RCTPSPDFKitView').Commands
.setAnnotationFlags,
[requestId, uuid, flags],
);
return promise;
} else if (Platform.OS === 'ios') {
return NativeModules.PSPDFKitViewManager.setAnnotationFlags(
uuid,
flags,
findNodeHandle(this._componentRef.current),
);
}
};
/**
* Gets the flags for the specified annotation.
*
* @method getAnnotationFlags
* @memberof PSPDFKitView
* @param { string } uuid The UUID of the annotation to query.
* @example
* const flags = await this.pdfRef.current.getAnnotationFlags('bb61b1bf-eacd-4227-a5bf-db205e591f5a');
*
* @returns { Promise<Annotation.Flags[]> } A promise containing the flags of the specified annotation.
*/
getAnnotationFlags = function (uuid) {
if (Platform.OS === 'android') {
let requestId = this._nextRequestId++;
let requestMap = this._requestMap;
// We create a promise here that will be resolved once onDataReturned is called.
let promise = new Promise(function (resolve, reject) {
requestMap[requestId] = { resolve: resolve, reject: reject };
});
UIManager.dispatchViewManagerCommand(
findNodeHandle(this._componentRef.current),
this._getViewManagerConfig('RCTPSPDFKitView').Commands
.getAnnotationFlags,
[requestId, uuid],
);
return promise;
} else if (Platform.OS === 'ios') {
return NativeModules.PSPDFKitViewManager.getAnnotationFlags(
uuid,
findNodeHandle(this._componentRef.current),
);
}
};
/**
* Imports the supplied XFDF file into the current document.
*
* @method importXFDF
* @deprecated Since PSPDFKit for React Native 2.12. Use ```this.pdfRef.current?.getDocument().importXFDF()``` instead.
* See {@link https://pspdfkit.com/api/react-native/PDFDocument.html#.importXFDF|importXFDF()}.
* @memberof PSPDFKitView
* @param { string } filePath The path to the XFDF file to import.
* @example
* const result = await this.pdfRef.current.importXFDF('path/to/XFDF.xfdf');
*
* @returns { Promise<any> } A promise containing an object with the result. ```true``` if the xfdf file was imported successfully, and ```false``` if an error occurred.
*/
importXFDF = function (filePath) {
if (Platform.OS === 'android') {
let requestId = this._nextRequestId++;
let requestMap = this._requestMap;
// We create a promise here that will be resolved once onDataReturned is called.
let promise = new Promise(function (resolve, reject) {
requestMap[requestId] = { resolve: resolve, reject: reject };
});
UIManager.dispatchViewManagerCommand(
findNodeHandle(this._componentRef.current),
this._getViewManagerConfig('RCTPSPDFKitView').Commands.importXFDF,
[requestId, filePath],
);
return promise;
} else if (Platform.OS === 'ios') {
return NativeModules.PSPDFKitViewManager.importXFDF(
filePath,
findNodeHandle(this._componentRef.current),
);
}
};
/**
* Exports the annotations from the current document to a XFDF file.
*
* @method exportXFDF
* @deprecated Since PSPDFKit for React Native 2.12. Use ```this.pdfRef.current?.getDocument().exportXFDF()``` instead.
* See {@link https://pspdfkit.com/api/react-native/PDFDocument.html#.exportXFDF|exportXFDF()}.
* @memberof PSPDFKitView
* @param { string } filePath The path where the XFDF file should be exported to.
* @example
* const result = await this.pdfRef.current.exportXFDF('path/to/XFDF.xfdf');
*
* @returns { Promise<any> } A promise containing an object with the exported file path and result. ```true``` if the xfdf file was exported successfully, and ```false``` if an error occurred.
*/
exportXFDF = function (filePath) {
if (Platform.OS === 'android') {
let requestId = this._nextRequestId++;
let requestMap = this._requestMap;
// We create a promise here that will be resolved once onDataReturned is called.
let promise = new Promise(function (resolve, reject) {
requestMap[requestId] = { resolve: resolve, reject: reject };
});
UIManager.dispatchViewManagerCommand(
findNodeHandle(this._componentRef.current),
this._getViewManagerConfig('RCTPSPDFKitView').Commands.exportXFDF,
[requestId, filePath],
);
return promise;
} else if (Platform.OS === 'ios') {
return NativeModules.PSPDFKitViewManager.exportXFDF(
filePath,
findNodeHandle(this._componentRef.current),
);
}
};
/**
* @typedef FormFieldResult
* @property { string } [formElement] The form field value
* @property { string } [error] The error description
*/
/**
* Gets the form field value of the supplied form field name.
*
* @method getFormFieldValue
* @memberof PSPDFKitView
* @param { string } fullyQualifiedName The fully qualified name of the form element.
* @example
* const result = await this.pdfRef.current.getFormFieldValue('myFormElement');
* // result: {'myFormElement' : value}
* // or
* // {'error' : 'Failed to get the form field value.'}
*
* @returns { Promise<FormFieldResult> } A promise containing an object with the value, or an error if the form field could not be found.
*/
getFormFieldValue = function (fullyQualifiedName) {
if (Platform.OS === 'android') {
let requestId = this._nextRequestId++;
let requestMap = this._requestMap;
// We create a promise here that will be resolved once onDataReturned is called.
let promise = new Promise(function (resolve, reject) {
requestMap[requestId] = { resolve: resolve, reject: reject };
});
UIManager.dispatchViewManagerCommand(
findNodeHandle(this._componentRef.current),
this._getViewManagerConfig('RCTPSPDFKitView').Commands
.getFormFieldValue,
[requestId, fullyQualifiedName],
);
return promise;
} else if (Platform.OS === 'ios') {
return NativeModules.PSPDFKitViewManager.getFormFieldValue(
fullyQualifiedName,
findNodeHandle(this._componentRef.current),
);
}
};
/**
* Sets the form field value of the supplied form field name.
*
* @method setFormFieldValue
* @memberof PSPDFKitView
* @param { string } fullyQualifiedName The fully qualified name of the form element. When using form elements such as radio buttons, the individual elements can be accessed by appending the index to the fully qualified name, for example ```choiceElement.0``` and ```choiceElement.1```.
* @param { string } value The new string value of the form element. For button form elements, pass ```selected``` or ```deselected```. For choice form elements, pass the index of the choice to select, for example ```1```.
* @example
* const result = await this.pdfRef.current.setFormFieldValue('Name_Last', 'Appleseed');
*
* @returns { Promise<boolean> } A promise resolving to ```true``` if the value was set successfully, and ```false``` if an error occurred.
*/
setFormFieldValue = function (fullyQualifiedName, value) {
if (Platform.OS === 'android') {
let requestId = this._nextRequestId++;
let requestMap = this._requestMap;
// We create a promise here that will be resolved once onDataReturned is called.
let promise = new Promise(function (resolve, reject) {
requestMap[requestId] = { resolve: resolve, reject: reject };
});
UIManager.dispatchViewManagerCommand(
findNodeHandle(this._componentRef.current),
this._getViewManagerConfig('RCTPSPDFKitView').Commands
.setFormFieldValue,
[requestId, fullyQualifiedName, value],
);
return promise;
} else if (Platform.OS === 'ios') {
return NativeModules.PSPDFKitViewManager.setFormFieldValue(
value,
fullyQualifiedName,
findNodeHandle(this._componentRef.current),
);
}
};
/**
* Sets the left bar button items for the specified view mode.
* Note: The same button item cannot be added to both the left and right bar button items simultaneously.
*
* @method setLeftBarButtonItems
* @memberof PSPDFKitView
* @param { Array<string> } items The list of bar button items.
* @see {@link https://github.com/PSPDFKit/react-native/blob/master/ios/RCTPSPDFKit/Converters/RCTConvert+UIBarButtonItem.m} for supported button items.
* @param { string } [viewMode] The view mode for which the bar buttons should be set. Options are: ```document```, ```thumbnails```, ```documentEditor```, or ```null```. If ```null``` is passed, the bar button items for all the view modes are set.
* @param { boolean } [animated] Specifies whether changing the bar buttons should be animated.
* @example
* const result = await this.pdfRef.current.setLeftBarButtonItems(
* ['searchButtonItem', 'thumbnailsButtonItem'],
* 'document',
* true);
*
*/
setLeftBarButtonItems = function (items, viewMode, animated) {
if (Platform.OS === 'ios') {
NativeModules.PSPDFKitViewManager.setLeftBarButtonItems(
items,
viewMode,
animated,
findNodeHandle(this._componentRef.current),
);
}
};
/**
* Gets the left bar button items for the specified view mode.
*
* @method getLeftBarButtonItemsForViewMode
* @memberof PSPDFKitView
* @param { string } [viewMode] The view mode to query. Options are: ```document```, ```thumbnails```, ```documentEditor```, or ```null```. If ```null``` is passed, the bar button items for the current view mode are returned.
*
* @returns { Promise<Array<string>> } A promise containing an array of bar button items, or an error if the items couldn’t be retrieved.
* @example
* const leftBarButtonItems = await this.pdfRef.current.getLeftBarButtonItemsForViewMode('document');
* // leftBarButtonItems: ['outlineButtonItem', 'searchButtonItem']
* // or
* // {'error' : 'Failed to get the left bar button items.'}
*
*/
getLeftBarButtonItemsForViewMode = function (viewMode) {
if (Platform.OS === 'ios') {
return NativeModules.PSPDFKitViewManager.getLeftBarButtonItemsForViewMode(
viewMode,
findNodeHandle(this._componentRef.current),
);
}
};
/**
* Sets the right bar button items for the specified view mode.
* Note: The same button item cannot be added to both the left and right bar button items simultaneously.
*
* @method setRightBarButtonItems
* @memberof PSPDFKitView
* @param { Array<string> } items The list of bar button items.
* @see {@link https://github.com/PSPDFKit/react-native/blob/master/ios/RCTPSPDFKit/Converters/RCTConvert+UIBarButtonItem.m} for supported button items.
* @param { string } [viewMode] The view mode for which the bar buttons should be set. Options are: ```document```, ```thumbnails```, ```documentEditor```, or ```null```. If ```null``` is passed, the bar button items for all the view modes are set.
* @param { boolean } [animated] Specifies whether changing the bar buttons should be animated.
* @example
* const result = await this.pdfRef.current.setRightBarButtonItems(
* ['searchButtonItem', 'thumbnailsButtonItem'],
* 'document',
* true);
*
*/
setRightBarButtonItems = function (items, viewMode, animated) {
if (Platform.OS === 'ios') {
NativeModules.PSPDFKitViewManager.setRightBarButtonItems(
items,
viewMode,
animated,
findNodeHandle(this._componentRef.current),
);
}
};
/**
* Gets the right bar button items for the specified view mode.
*
* @method getRightBarButtonItemsForViewMode
* @memberof PSPDFKitView
* @param { string } [viewMode] The view mode to query. Options are: ```document```, ```thumbnails```, ```documentEditor```, or ```null```. If ```null``` is passed, the bar button items for the current view mode are returned.
*
* @returns { Promise<Array<string>> } A promise containing an array of bar button items, or an error if the items couldn’t be retrieved.
* @example
* const rightBarButtonItems = await this.pdfRef.current.getRightBarButtonItemsForViewMode('document');
* // rightBarButtonItems: ['outlineButtonItem', 'searchButtonItem']
* // or
* // {'error' : 'Failed to get the right bar button items.'}
*
*/
getRightBarButtonItemsForViewMode = function (viewMode) {
if (Platform.OS === 'ios') {
return NativeModules.PSPDFKitViewManager.getRightBarButtonItemsForViewMode(
viewMode,
findNodeHandle(this._componentRef.current),
);
}
};
/**
* Sets the Toolbar object to customize the toolbar appearance and behaviour.
*
* @method setToolbar
* @memberof PSPDFKitView
* @param { Toolbar } toolbar The toolbar object.
* @example
* const toolbar = {
* // iOS
* rightBarButtonItems: {
* viewMode: Toolbar.PDFViewMode.VIEW_MODE_DOCUMENT,
* animated: true,
* buttons: ['searchButtonItem', 'readerViewButtonItem']
* },
* // Android
* toolbarMenuItems: {
* buttons: ['searchButtonItem', 'readerViewButtonItem']
* },
* }
* this.pdfRef.current.setToolbar(toolbar);
*
*/
setToolbar = function (toolbar) {
if (Platform.OS === 'ios') {
NativeModules.PSPDFKitViewManager.setToolbar(
toolbar,
findNodeHandle(this._componentRef.current),
);
} else if (Platform.OS === 'android') {
UIManager.dispatchViewManagerCommand(
findNodeHandle(this._componentRef.current),
this._getViewManagerConfig('RCTPSPDFKitView').Commands.setToolbar,
[toolbar],