From 73af623daca1482fc2aa52e480f0d0fec0ae11fd Mon Sep 17 00:00:00 2001 From: Jamie Dassoulas <20866295+JamieDass@users.noreply.github.com> Date: Tue, 5 Mar 2024 15:31:46 -0700 Subject: [PATCH] [iOS] Match Android implementation of `importAnnotations` (#682) * Add replace parameter to importAnnotations function on iOS * Update documentation * Updating package version --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- API.md | 2 +- ios/RNTPTDocumentView.h | 2 +- ios/RNTPTDocumentView.m | 24 ++++++++++++++++++------ ios/RNTPTDocumentViewManager.m | 2 +- package.json | 2 +- 5 files changed, 22 insertions(+), 10 deletions(-) diff --git a/API.md b/API.md index 9a9c6ace..8218823f 100644 --- a/API.md +++ b/API.md @@ -2580,7 +2580,7 @@ Parameters: Name | Type | Description --- | --- | --- xfdf | string | annotation string in XFDF format for import -replace | boolean | whether to replace existing form and annotation data with those imported from the XFDF string (Android only) +replace | boolean | whether to replace existing form and annotation data with those imported from the XFDF string Returns a Promise. Promise Parameters: diff --git a/ios/RNTPTDocumentView.h b/ios/RNTPTDocumentView.h index f8275a0f..438383dc 100644 --- a/ios/RNTPTDocumentView.h +++ b/ios/RNTPTDocumentView.h @@ -572,7 +572,7 @@ static NSString * const PTSignaturesManager_signatureDirectory = @"PTSignaturesM - (nullable NSString *)exportAnnotationsWithOptions:(NSDictionary *)options; -- (nullable NSArray *)importAnnotations:(NSString *)xfdfString; +- (nullable NSArray *)importAnnotations:(NSString *)xfdfString replace:(BOOL)replace; - (void)flattenAnnotations:(BOOL)formsOnly; diff --git a/ios/RNTPTDocumentView.m b/ios/RNTPTDocumentView.m index dd41ba55..ba632d79 100644 --- a/ios/RNTPTDocumentView.m +++ b/ios/RNTPTDocumentView.m @@ -1353,7 +1353,7 @@ - (NSString *)exportAnnotationsWithOptions:(NSDictionary *)options return nil; } -- (nullable NSArray *)importAnnotations:(NSString *)xfdfString +- (nullable NSArray *)importAnnotations:(NSString *)xfdfString replace:(BOOL)replace { PTDocumentBaseViewController *documentViewController = self.currentDocumentViewController; PTPDFViewCtrl *pdfViewCtrl = documentViewController.pdfViewCtrl; @@ -1369,12 +1369,24 @@ - (NSString *)exportAnnotationsWithOptions:(NSDictionary *)options return nil; } - PTAnnotationManager * const annotationManager = documentViewController.toolManager.annotationManager; + if (self.collaborationManager != nil) { + [self.collaborationManager importAnnotationsWithXFDFString:xfdfString]; + return [self getAnnotationFromXFDF:xfdfString]; + } - const BOOL updateSuccess = [annotationManager updateAnnotationsWithXFDFString:xfdfString - error:&error]; - if (!updateSuccess || error) { - @throw [NSException exceptionWithName:NSGenericException reason:error.localizedFailureReason userInfo:error.userInfo]; + [pdfViewCtrl DocLock:YES withBlock:^(PTPDFDoc * _Nullable doc) { + PTFDFDoc *fdfDoc = [PTFDFDoc CreateFromXFDF:xfdfString]; + if (replace) { + [doc FDFUpdate:fdfDoc]; + } else { + [doc FDFMerge:fdfDoc]; + } + [pdfViewCtrl Update:YES]; + } error:&error]; + + if (error) { + NSLog(@"Error: There was an error while trying to import annotations. %@", error.localizedDescription); + return nil; } return [self getAnnotationFromXFDF:xfdfString]; diff --git a/ios/RNTPTDocumentViewManager.m b/ios/RNTPTDocumentViewManager.m index 523700ce..29b9b705 100644 --- a/ios/RNTPTDocumentViewManager.m +++ b/ios/RNTPTDocumentViewManager.m @@ -1130,7 +1130,7 @@ - (void)importAnnotationCommandForDocumentViewTag:(NSNumber *)tag xfdfCommand:(N { RNTPTDocumentView *documentView = self.documentViews[tag]; if (documentView) { - return [documentView importAnnotations:xfdfString]; + return [documentView importAnnotations:xfdfString replace:replace]; } else { @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"Unable to find DocumentView for tag" userInfo:nil]; } diff --git a/package.json b/package.json index 03bf256b..1352dc63 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "react-native-pdftron", "title": "React Native Pdftron", - "version": "3.0.3-36", + "version": "3.0.3-37", "description": "React Native Pdftron", "main": "./lib/index.js", "typings": "index.ts",