Skip to content

Commit 641cbd6

Browse files
author
RadAzzouz
committed
Add support for programmatically removing annotations
1 parent f35e03e commit 641cbd6

File tree

5 files changed

+103
-1
lines changed

5 files changed

+103
-1
lines changed

index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,27 @@ class PSPDFKitView extends React.Component {
192192
}
193193
};
194194

195+
/**
196+
* Removes an existing annotation from the current document.
197+
*
198+
* @param annotation InstantJson of the annotation to remove.
199+
*/
200+
removeAnnotation = function(annotationName) {
201+
if (Platform.OS === "android") {
202+
// TODO: Uncomment once the Android implementaion is ready.
203+
// UIManager.dispatchViewManagerCommand(
204+
// findNodeHandle(this.refs.pdfView),
205+
// UIManager.RCTPSPDFKitView.Commands.removeAnnotation,
206+
// [annotation]
207+
// );
208+
} else if (Platform.OS === "ios") {
209+
NativeModules.PSPDFKitViewManager.removeAnnotation(
210+
annotation,
211+
findNodeHandle(this.refs.pdfView)
212+
);
213+
}
214+
};
215+
195216
/**
196217
* Gets all unsaved changes to annotations.
197218
*

ios/RCTPSPDFKit/RCTPSPDFKitView.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737

3838
/// Anotations
3939
- (NSDictionary<NSString *, NSArray<NSDictionary *> *> *)getAnnotations:(PSPDFPageIndex)pageIndex type:(PSPDFAnnotationType)type;
40-
- (void)addAnnotation:(NSString *)jsonAnnotation;
40+
- (void)addAnnotation:(id)jsonAnnotation;
41+
- (void)removeAnnotation:(id)jsonAnnotation;
4142
- (NSDictionary<NSString *, NSArray<NSDictionary *> *> *)getAllUnsavedAnnotations;
4243
- (void)addAnnotations:(NSString *)jsonAnnotations;
4344

ios/RCTPSPDFKit/RCTPSPDFKitView.m

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,37 @@ - (void)addAnnotation:(id)jsonAnnotation {
203203
}
204204
}
205205

206+
- (void)removeAnnotation:(id)jsonAnnotation {
207+
NSData *data;
208+
if ([jsonAnnotation isKindOfClass:NSString.class]) {
209+
data = [jsonAnnotation dataUsingEncoding:NSUTF8StringEncoding];
210+
} else if ([jsonAnnotation isKindOfClass:NSDictionary.class]) {
211+
data = [NSJSONSerialization dataWithJSONObject:jsonAnnotation options:0 error:nil];
212+
} else {
213+
NSLog(@"Invalid JSON Annotation.");
214+
return;
215+
}
216+
217+
PSPDFDocument *document = self.pdfController.document;
218+
PSPDFDocumentProvider *documentProvider = document.documentProviders.firstObject;
219+
220+
BOOL success = NO;
221+
if (data) {
222+
PSPDFAnnotation *annotationToRemove = [PSPDFAnnotation annotationFromInstantJSON:data documentProvider:documentProvider error:NULL];
223+
for (PSPDFAnnotation *annotation in [document annotationsForPageAtIndex:annotationToRemove.pageIndex type:annotationToRemove.type]) {
224+
// Remove the annotation if the name matches.
225+
if ([annotation.name isEqualToString:annotationToRemove.name]) {
226+
success = [document removeAnnotations:@[annotation] options:nil];
227+
break;
228+
}
229+
}
230+
}
231+
232+
if (!success) {
233+
NSLog(@"Failed to remove annotation.");
234+
}
235+
}
236+
206237
- (NSDictionary<NSString *, NSArray<NSDictionary *> *> *)getAllUnsavedAnnotations {
207238
PSPDFDocumentProvider *documentProvider = self.pdfController.document.documentProviders.firstObject;
208239
NSData *data = [self.pdfController.document generateInstantJSONFromDocumentProvider:documentProvider error:NULL];

ios/RCTPSPDFKit/RCTPSPDFKitViewManager.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ @implementation RCTPSPDFKitViewManager
116116
});
117117
}
118118

119+
RCT_EXPORT_METHOD(removeAnnotation:(id)jsonAnnotation reactTag:(nonnull NSNumber *)reactTag) {
120+
dispatch_async(dispatch_get_main_queue(), ^{
121+
RCTPSPDFKitView *component = (RCTPSPDFKitView *)[self.bridge.uiManager viewForReactTag:reactTag];
122+
[component removeAnnotation:jsonAnnotation];
123+
});
124+
}
125+
119126
RCT_EXPORT_METHOD(getAllUnsavedAnnotations:(nonnull NSNumber *)reactTag resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
120127
dispatch_async(dispatch_get_main_queue(), ^{
121128
RCTPSPDFKitView *component = (RCTPSPDFKitView *)[self.bridge.uiManager viewForReactTag:reactTag];

samples/Catalog/Catalog.ios.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,7 @@ class ProgrammaticAnnotations extends Component {
593593
},
594594
opacity: 1,
595595
pageIndex: 0,
596+
name: "A167811E-6D10-4546-A147-B7AD775FE8AC",
596597
strokeColor: "#AA47BE",
597598
type: "pspdfkit/ink",
598599
v: 1
@@ -602,6 +603,46 @@ class ProgrammaticAnnotations extends Component {
602603
title="addAnnotation"
603604
/>
604605
</View>
606+
<View>
607+
<Button
608+
onPress={() => {
609+
// Programmatically remove an existing ink annotation.
610+
const annotationJSON = {
611+
bbox: [
612+
89.586334228515625,
613+
98.5791015625,
614+
143.12948608398438,
615+
207.1583251953125
616+
],
617+
isDrawnNaturally: false,
618+
lineWidth: 5,
619+
lines: {
620+
intensities: [[0.5, 0.5, 0.5], [0.5, 0.5, 0.5]],
621+
points: [
622+
[
623+
[92.086334228515625, 101.07916259765625],
624+
[92.086334228515625, 202.15826416015625],
625+
[138.12950134277344, 303.2374267578125]
626+
],
627+
[
628+
[184.17266845703125, 101.07916259765625],
629+
[184.17266845703125, 202.15826416015625],
630+
[230.2158203125, 303.2374267578125]
631+
]
632+
]
633+
},
634+
opacity: 1,
635+
pageIndex: 0,
636+
name: "A167811E-6D10-4546-A147-B7AD775FE8AC",
637+
strokeColor: "#AA47BE",
638+
type: "pspdfkit/ink",
639+
v: 1
640+
};
641+
this.refs.pdfView.removeAnnotation(annotationJSON);
642+
}}
643+
title="removeAnnotation"
644+
/>
645+
</View>
605646
<View>
606647
<Button
607648
onPress={() => {
@@ -635,6 +676,7 @@ class ProgrammaticAnnotations extends Component {
635676
},
636677
isDrawnNaturally: false,
637678
strokeColor: "#AA47BE",
679+
name: "A167811E-6D10-4546-A147-B7AD775FE8AC",
638680
updatedAt: "2018-07-30T15:34:48Z",
639681
pageIndex: 0,
640682
opacity: 1,

0 commit comments

Comments
 (0)