File tree Expand file tree Collapse file tree 5 files changed +62
-0
lines changed
Expand file tree Collapse file tree 5 files changed +62
-0
lines changed Original file line number Diff line number Diff line change @@ -276,6 +276,25 @@ class PSPDFKitView extends React.Component {
276276 }
277277 } ;
278278
279+ /**
280+ * Gets all annotations of the given type.
281+ *
282+ * @param type The type of annotations to get (See here for types https://pspdfkit.com/guides/server/current/api/json-format/) or null to get all annotations.
283+ *
284+ * Returns a promise resolving an array with the following structure:
285+ * {'annotations' : [instantJson]}
286+ */
287+ getAllAnnotations = function ( type ) {
288+ if ( Platform . OS === "android" ) {
289+ //TODO: Implement Android here.
290+ } else if ( Platform . OS === "ios" ) {
291+ return NativeModules . PSPDFKitViewManager . getAllAnnotations (
292+ type ,
293+ findNodeHandle ( this . refs . pdfView )
294+ ) ;
295+ }
296+ } ;
297+
279298 /**
280299 * Applies the passed in document instant json.
281300 *
Original file line number Diff line number Diff line change @@ -43,6 +43,7 @@ NS_ASSUME_NONNULL_BEGIN
4343- (BOOL )addAnnotation : (id )jsonAnnotation error : (NSError *_Nullable *)error ;
4444- (BOOL )removeAnnotationWithUUID : (NSString *)annotationUUID ;
4545- (NSDictionary <NSString *, NSArray<NSDictionary *> *> *)getAllUnsavedAnnotationsWithError : (NSError *_Nullable *)error ;
46+ - (NSDictionary <NSString *, NSArray<NSDictionary *> *> *)getAllAnnotations : (PSPDFAnnotationType)type error : (NSError *_Nullable *)error ;
4647- (BOOL )addAnnotations : (NSString *)jsonAnnotations error : (NSError *_Nullable *)error ;
4748
4849// / Forms
Original file line number Diff line number Diff line change @@ -247,6 +247,15 @@ - (BOOL)removeAnnotationWithUUID:(NSString *)annotationUUID {
247247 return annotationsJSON;
248248}
249249
250+ - (NSDictionary <NSString *, NSArray<NSDictionary *> *> *)getAllAnnotations : (PSPDFAnnotationType)type error : (NSError *_Nullable *)error {
251+ PSPDFDocument *document = self.pdfController .document ;
252+ VALIDATE_DOCUMENT (document, nil )
253+
254+ NSArray <PSPDFAnnotation *> *annotations = [[document allAnnotationsOfType: type].allValues valueForKeyPath: @" @unionOfArrays.self" ];
255+ NSArray <NSDictionary *> *annotationsJSON = [RCTConvert instantJSONFromAnnotations: annotations error: error];
256+ return @{@" annotations" : annotationsJSON};
257+ }
258+
250259- (BOOL )addAnnotations : (id )jsonAnnotations error : (NSError *_Nullable *)error {
251260 NSData *data;
252261 if ([jsonAnnotations isKindOfClass: NSString .class]) {
Original file line number Diff line number Diff line change @@ -195,6 +195,19 @@ @implementation RCTPSPDFKitViewManager
195195 });
196196}
197197
198+ RCT_EXPORT_METHOD (getAllAnnotations:(NSString *)type reactTag:(nonnull NSNumber *)reactTag resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
199+ dispatch_async (dispatch_get_main_queue (), ^{
200+ RCTPSPDFKitView *component = (RCTPSPDFKitView *)[self .bridge.uiManager viewForReactTag: reactTag];
201+ NSError *error;
202+ NSDictionary *annotations = [component getAllAnnotations: [RCTConvert annotationTypeFromInstantJSONType: type] error: &error];
203+ if (annotations) {
204+ resolve (annotations);
205+ } else {
206+ reject (@" error" , @" Failed to get all annotations." , error);
207+ }
208+ });
209+ }
210+
198211RCT_EXPORT_METHOD (addAnnotations:(id )jsonAnnotations reactTag:(nonnull NSNumber *)reactTag resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
199212 dispatch_async (dispatch_get_main_queue (), ^{
200213 RCTPSPDFKitView *component = (RCTPSPDFKitView *)[self .bridge.uiManager viewForReactTag: reactTag];
Original file line number Diff line number Diff line change @@ -783,6 +783,26 @@ class ProgrammaticAnnotations extends Component {
783783 title = "getAllUnsavedAnnotations"
784784 />
785785 </ View >
786+ < View >
787+ < Button
788+ onPress = { async ( ) => {
789+ // Get all annotations annotations from the document.
790+ await this . refs . pdfView
791+ . getAllAnnotations ( )
792+ . then ( result => {
793+ if ( result ) {
794+ alert ( JSON . stringify ( result ) ) ;
795+ } else {
796+ alert ( "Failed to get all annotations." ) ;
797+ }
798+ } )
799+ . catch ( error => {
800+ alert ( JSON . stringify ( error ) ) ;
801+ } ) ;
802+ } }
803+ title = "getAllAnnotations"
804+ />
805+ </ View >
786806 </ View >
787807 </ View >
788808 ) ;
You can’t perform that action at this time.
0 commit comments