Skip to content

Commit

Permalink
Change the annotation processiong API to the native module instead of…
Browse files Browse the repository at this point in the history
… the PDF view
  • Loading branch information
Rad Azzouz committed Feb 18, 2020
1 parent 84ed211 commit b09439d
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 106 deletions.
19 changes: 0 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -503,25 +503,6 @@ class PSPDFKitView extends React.Component {
);
}
};
/**
* Process (embed, flatten, remove, print) annotations in a new document.
*
* @param change The annotation change. Can be 'embed', 'flatten', 'remove' or `print`. If `null` is passed, `embed` will be used.
* @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.
* @param processedDocumentPath The path of processed document. Needs to be in a writable location.
*
* @platform ios
*/
processAnnotations = function(change, type, processedDocumentPath) {
if (Platform.OS === "ios") {
return NativeModules.PSPDFKitViewManager.processAnnotations(
change,
type,
processedDocumentPath,
findNodeHandle(this.refs.pdfView)
);
}
};

_getViewManagerConfig = viewManagerName => {
const version = NativeModules.PlatformConstants.reactNativeVersion.minor;
Expand Down
26 changes: 26 additions & 0 deletions ios/RCTPSPDFKit/RCTPSPDFKitManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#import <React/RCTLog.h>
#import <React/RCTUtils.h>
#import <React/RCTConvert.h>
#import "RCTConvert+PSPDFAnnotation.h"
#import "RCTConvert+PSPDFAnnotationChange.h"

#define PROPERTY(property) NSStringFromSelector(@selector(property))

Expand Down Expand Up @@ -77,6 +79,30 @@ @implementation RCTPSPDFKitManager
}
}

#pragma mark - Annotations Processing

RCT_REMAP_METHOD(processAnnotations, processAnnotations:(nullable NSString *)change annotationType:(nullable NSString *)type sourceDocument:(PSPDFDocument *)sourceDocument processedDocumentPath:(nonnull NSString *)processedDocumentPath resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
NSError *error;
PSPDFAnnotationChange annotationChange = [RCTConvert PSPDFAnnotationChange:change];
PSPDFAnnotationType annotationType = [RCTConvert annotationTypeFromInstantJSONType:type];
NSURL *processedDocumentURL = [NSURL fileURLWithPath:processedDocumentPath];

// Create a processor configuration with the current document.
PSPDFProcessorConfiguration *configuration = [[PSPDFProcessorConfiguration alloc] initWithDocument:sourceDocument];

// Modify annotations.
[configuration modifyAnnotationsOfTypes:annotationType change:annotationChange];

// Create the PDF processor and write the processed file.
PSPDFProcessor *processor = [[PSPDFProcessor alloc] initWithConfiguration:configuration securityOptions:nil];
BOOL success = [processor writeToFileURL:processedDocumentURL error:&error];
if (success) {
resolve(@(success));
} else {
reject(@"error", @"Failed to process annotations.", error);
}
}

- (dispatch_queue_t)methodQueue {
return dispatch_get_main_queue();
}
Expand Down
2 changes: 0 additions & 2 deletions ios/RCTPSPDFKit/RCTPSPDFKitView.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ NS_ASSUME_NONNULL_BEGIN
- (NSArray <NSString *> *)getLeftBarButtonItemsForViewMode:(NSString *)viewMode;
- (NSArray <NSString *> *)getRightBarButtonItemsForViewMode:(NSString *)viewMode;

// Annotation Processing
- (BOOL)processAnnotations:(PSPDFAnnotationChange)annotationChange annotationType:(PSPDFAnnotationType)annotationType processedDocumentPath:(nonnull NSString *)processedDocumentPath error:(NSError *_Nullable *)error;
@end

NS_ASSUME_NONNULL_END
19 changes: 0 additions & 19 deletions ios/RCTPSPDFKit/RCTPSPDFKitView.m
Original file line number Diff line number Diff line change
Expand Up @@ -349,25 +349,6 @@ - (BOOL)setFormFieldValue:(NSString *)value fullyQualifiedName:(NSString *)fully
return success;
}

#pragma mark - Annotations Processing

- (BOOL)processAnnotations:(PSPDFAnnotationChange)annotationChange annotationType:(PSPDFAnnotationType)annotationType processedDocumentPath:(nonnull NSString *)processedDocumentPath error:(NSError *_Nullable *)error {
PSPDFDocument *document = self.pdfController.document;
VALIDATE_DOCUMENT(document, NO)

NSURL *processedDocumentURL = [NSURL fileURLWithPath:processedDocumentPath];

// Create a processor configuration with the current document.
PSPDFProcessorConfiguration *configuration = [[PSPDFProcessorConfiguration alloc] initWithDocument:document];

// Modify annotations.
[configuration modifyAnnotationsOfTypes:annotationType change:annotationChange];

// Create the PDF processor and write the processed file.
PSPDFProcessor *processor = [[PSPDFProcessor alloc] initWithConfiguration:configuration securityOptions:nil];
return [processor writeToFileURL:processedDocumentURL error:error];
}

#pragma mark - Notifications

- (void)annotationChangedNotification:(NSNotification *)notification {
Expand Down
16 changes: 0 additions & 16 deletions ios/RCTPSPDFKit/RCTPSPDFKitViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#import "RCTConvert+PSPDFDocument.h"
#import "RCTConvert+PSPDFAnnotationToolbarConfiguration.h"
#import "RCTConvert+PSPDFViewMode.h"
#import "RCTConvert+PSPDFAnnotationChange.h"
#import "RCTPSPDFKitView.h"
#import <React/RCTUIManager.h>

Expand Down Expand Up @@ -284,21 +283,6 @@ @implementation RCTPSPDFKitViewManager
});
}

RCT_EXPORT_METHOD(processAnnotations:(nullable NSString *)change annotationType:(nullable NSString *)type processedDocumentPath:(nonnull NSString *)processedDocumentPath reactTag:(nonnull NSNumber *)reactTag resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
dispatch_async(dispatch_get_main_queue(), ^{
RCTPSPDFKitView *component = (RCTPSPDFKitView *)[self.bridge.uiManager viewForReactTag:reactTag];
NSError *error;
PSPDFAnnotationChange annotationChange = [RCTConvert PSPDFAnnotationChange:change];
PSPDFAnnotationType annotationType = [RCTConvert annotationTypeFromInstantJSONType:type];
BOOL success = [component processAnnotations:annotationChange annotationType:annotationType processedDocumentPath:processedDocumentPath error:&error];
if (success) {
resolve(@(success));
} else {
reject(@"error", @"Failed to process annotations.", error);
}
});
}

- (UIView *)view {
return [[RCTPSPDFKitView alloc] init];
}
Expand Down
153 changes: 103 additions & 50 deletions samples/Catalog/Catalog.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -1031,11 +1031,12 @@ class ProgrammaticFormFilling extends Component {

class AnnotationProcessing extends Component {
render() {
const sourceDocumentPath = "PDFs/Annual Report.pdf";
return (
<View style={{ flex: 1 }}>
<PSPDFKitView
ref="pdfView"
document={"PDFs/Annual Report.pdf"}
document={sourceDocumentPath}
disableAutomaticSaving={true}
configuration={{
backgroundColor: processColor("lightgrey"),
Expand All @@ -1056,18 +1057,31 @@ class AnnotationProcessing extends Component {
onPress={async () => {
const processedDocumentPath =
RNFS.DocumentDirectoryPath + "/embedded.pdf";
await this.refs.pdfView
.processAnnotations("embed", null, processedDocumentPath)
.then(success => {
if (success) {
PSPDFKit.present(processedDocumentPath, {});
} else {
alert("Failed to embed annotations.");
}
})
.catch(error => {
alert(JSON.stringify(error));
});
// First, save all annotations in the current document.
await this.refs.pdfView.saveCurrentDocument().then(success => {
if (success) {
// Then, embed all the annotations
PSPDFKit.processAnnotations(
"embed",
null,
sourceDocumentPath,
processedDocumentPath
)
.then(success => {
if (success) {
// And finally, present the newly processed document with embedded annotations.
PSPDFKit.present(processedDocumentPath, {});
} else {
alert("Failed to embed annotations.");
}
})
.catch(error => {
alert(JSON.stringify(error));
});
} else {
alert("Failed to save current document.");
}
});
}}
title="Embed All Annotations"
/>
Expand All @@ -1077,18 +1091,31 @@ class AnnotationProcessing extends Component {
onPress={async () => {
const processedDocumentPath =
RNFS.DocumentDirectoryPath + "/flattened.pdf";
await this.refs.pdfView
.processAnnotations("flatten", null, processedDocumentPath)
.then(success => {
if (success) {
PSPDFKit.present(processedDocumentPath, {});
} else {
alert("Failed to flatten annotations.");
}
})
.catch(error => {
alert(JSON.stringify(error));
});
// First, save all annotations in the current document.
await this.refs.pdfView.saveCurrentDocument().then(success => {
if (success) {
// Then, flatten all the annotations
PSPDFKit.processAnnotations(
"flatten",
null,
sourceDocumentPath,
processedDocumentPath
)
.then(success => {
if (success) {
// And finally, present the newly processed document with flattened annotations.
PSPDFKit.present(processedDocumentPath, {});
} else {
alert("Failed to embed annotations.");
}
})
.catch(error => {
alert(JSON.stringify(error));
});
} else {
alert("Failed to save current document.");
}
});
}}
title="Flatten All Annotations"
/>
Expand All @@ -1098,18 +1125,31 @@ class AnnotationProcessing extends Component {
onPress={async () => {
const processedDocumentPath =
RNFS.DocumentDirectoryPath + "/removed.pdf";
await this.refs.pdfView
.processAnnotations("remove", null, processedDocumentPath)
.then(success => {
if (success) {
PSPDFKit.present(processedDocumentPath, {});
} else {
alert("Failed to remove annotations.");
}
})
.catch(error => {
alert(JSON.stringify(error));
});
// First, save all annotations in the current document.
await this.refs.pdfView.saveCurrentDocument().then(success => {
if (success) {
// Then, remove all the annotations
PSPDFKit.processAnnotations(
"remove",
null,
sourceDocumentPath,
processedDocumentPath
)
.then(success => {
if (success) {
// And finally, present the newly processed document with removed annotations.
PSPDFKit.present(processedDocumentPath, {});
} else {
alert("Failed to remove annotations.");
}
})
.catch(error => {
alert(JSON.stringify(error));
});
} else {
alert("Failed to save current document.");
}
});
}}
title="Remove All Annotations"
/>
Expand All @@ -1118,19 +1158,32 @@ class AnnotationProcessing extends Component {
<Button
onPress={async () => {
const processedDocumentPath =
RNFS.DocumentDirectoryPath + "/print.pdf";
await this.refs.pdfView
.processAnnotations("print", null, processedDocumentPath)
.then(success => {
if (success) {
PSPDFKit.present(processedDocumentPath, {});
} else {
alert("Failed to print annotations.");
}
})
.catch(error => {
alert(JSON.stringify(error));
});
RNFS.DocumentDirectoryPath + "/printed.pdf";
// First, save all annotations in the current document.
await this.refs.pdfView.saveCurrentDocument().then(success => {
if (success) {
// Then, print all the annotations
PSPDFKit.processAnnotations(
"print",
null,
sourceDocumentPath,
processedDocumentPath
)
.then(success => {
if (success) {
// And finally, present the newly processed document with printed annotations.
PSPDFKit.present(processedDocumentPath, {});
} else {
alert("Failed to print annotations.");
}
})
.catch(error => {
alert(JSON.stringify(error));
});
} else {
alert("Failed to save current document.");
}
});
}}
title="Print All Annotations"
/>
Expand Down

0 comments on commit b09439d

Please sign in to comment.