Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API for annotation processing #350

Merged
merged 6 commits into from
Feb 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions android/src/main/java/com/pspdfkit/react/PSPDFKitModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,22 @@
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.pspdfkit.PSPDFKit;
import com.pspdfkit.annotations.Annotation;
import com.pspdfkit.annotations.AnnotationType;
import com.pspdfkit.document.PdfDocument;
import com.pspdfkit.document.PdfDocumentLoader;
import com.pspdfkit.document.image.CameraImagePickerFragment;
import com.pspdfkit.document.image.GalleryImagePickerFragment;
import com.pspdfkit.document.processor.PdfProcessor;
import com.pspdfkit.document.processor.PdfProcessorTask;
import com.pspdfkit.instant.ui.InstantPdfActivity;
import com.pspdfkit.listeners.SimpleDocumentListener;
import com.pspdfkit.react.helper.ConversionHelpers;
import com.pspdfkit.ui.PdfActivity;
import com.pspdfkit.ui.PdfFragment;

import java.io.File;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -178,6 +186,46 @@ public void setLicenseKey(@NonNull String licenseKey) {
PSPDFKit.initialize(getReactApplicationContext().getApplicationContext(), licenseKey);
}

@ReactMethod
public void processAnnotations(@NonNull final String processingMode,
@Nullable final String annotationType,
@NonNull final String sourceDocumentPath,
@NonNull final String targetDocumentPath,
@NonNull final Promise promise) {
PdfDocumentLoader.openDocumentAsync(getReactApplicationContext(), Uri.parse(sourceDocumentPath))
.flatMapCompletable(document -> {
PdfProcessorTask task = PdfProcessorTask.fromDocument(document);
final EnumSet<AnnotationType> types = ConversionHelpers.getAnnotationTypeFromString(annotationType);
final PdfProcessorTask.AnnotationProcessingMode mode = getProcessingModeFromString(processingMode);
for (AnnotationType type : types) {
task.changeAnnotationsOfType(type, mode);
}

return PdfProcessor.processDocumentAsync(task, new File(targetDocumentPath)).ignoreElements();
})
.subscribe(() -> {
promise.resolve(Boolean.TRUE);
}, throwable -> {
promise.reject(throwable);
});
}

private static PdfProcessorTask.AnnotationProcessingMode getProcessingModeFromString(@NonNull final String mode) {
if ("print".equalsIgnoreCase(mode)) {
return PdfProcessorTask.AnnotationProcessingMode.PRINT;
} else if ("remove".equalsIgnoreCase(mode)) {
// Called remove to match iOS.
return PdfProcessorTask.AnnotationProcessingMode.DELETE;
} else if ("flatten".equalsIgnoreCase(mode)) {
return PdfProcessorTask.AnnotationProcessingMode.FLATTEN;
} else if ("embed".equalsIgnoreCase(mode)) {
// Called embed to match iOS.
return PdfProcessorTask.AnnotationProcessingMode.KEEP;
} else {
return PdfProcessorTask.AnnotationProcessingMode.KEEP;
}
}

@NonNull
@Override
public Map<String, Object> getConstants() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.pspdfkit.react.helper;

import androidx.annotation.Nullable;

import com.pspdfkit.annotations.AnnotationType;

import java.util.EnumSet;

public class ConversionHelpers {

public static EnumSet<AnnotationType> getAnnotationTypeFromString(@Nullable final String type) {
if (type == null || "all".equalsIgnoreCase(type)) {
return EnumSet.allOf(AnnotationType.class);
}
if ("pspdfkit/ink".equalsIgnoreCase(type)) {
return EnumSet.of(AnnotationType.INK);
}
if ("pspdfkit/link".equalsIgnoreCase(type)) {
return EnumSet.of(AnnotationType.LINK);
}
if ("pspdfkit/markup/highlight".equalsIgnoreCase(type)) {
return EnumSet.of(AnnotationType.HIGHLIGHT);
}
if ("pspdfkit/markup/squiggly".equalsIgnoreCase(type)) {
return EnumSet.of(AnnotationType.SQUIGGLY);
}
if ("pspdfkit/markup/strikeout".equalsIgnoreCase(type)) {
return EnumSet.of(AnnotationType.STRIKEOUT);
}
if ("pspdfkit/markup/underline".equalsIgnoreCase(type)) {
return EnumSet.of(AnnotationType.UNDERLINE);
}
if ("pspdfkit/note".equalsIgnoreCase(type)) {
return EnumSet.of(AnnotationType.NOTE);
}
if ("pspdfkit/shape/ellipse".equalsIgnoreCase(type)) {
return EnumSet.of(AnnotationType.CIRCLE);
}
if ("pspdfkit/shape/line".equalsIgnoreCase(type)) {
return EnumSet.of(AnnotationType.LINE);
}
if ("pspdfkit/shape/polygon".equalsIgnoreCase(type)) {
return EnumSet.of(AnnotationType.POLYGON);
}
if ("pspdfkit/shape/polyline".equalsIgnoreCase(type)) {
return EnumSet.of(AnnotationType.POLYLINE);
}
if ("pspdfkit/shape/rectangle".equalsIgnoreCase(type)) {
return EnumSet.of(AnnotationType.SQUARE);
}
if ("pspdfkit/text".equalsIgnoreCase(type)) {
return EnumSet.of(AnnotationType.FREETEXT);
}
return EnumSet.noneOf(AnnotationType.class);
}
}
56 changes: 6 additions & 50 deletions android/src/main/java/com/pspdfkit/views/PdfView.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import com.facebook.react.common.MapBuilder;
import com.facebook.react.uimanager.events.EventDispatcher;
import com.pspdfkit.annotations.Annotation;
import com.pspdfkit.annotations.AnnotationType;
import com.pspdfkit.configuration.activity.PdfActivityConfiguration;
import com.pspdfkit.document.PdfDocument;
import com.pspdfkit.document.PdfDocumentLoader;
Expand Down Expand Up @@ -54,7 +53,6 @@
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
Expand All @@ -72,6 +70,8 @@
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subjects.BehaviorSubject;

import static com.pspdfkit.react.helper.ConversionHelpers.getAnnotationTypeFromString;

/**
* This view displays a {@link com.pspdfkit.ui.PdfFragment} and all associated toolbars.
*/
Expand Down Expand Up @@ -443,60 +443,16 @@ public Single<List<Annotation>> getAnnotations(final int pageIndex, @Nullable fi
return getCurrentPdfFragment()
.map(pdfFragment -> pdfFragment.getDocument())
.flatMap((Function<PdfDocument, ObservableSource<Annotation>>) pdfDocument ->
pdfDocument.getAnnotationProvider().getAllAnnotationsOfTypeAsync(getTypeFromString(type), pageIndex, 1)).toList();
pdfDocument.getAnnotationProvider().getAllAnnotationsOfTypeAsync(getAnnotationTypeFromString(type), pageIndex, 1)).toList();
}

public Single<List<Annotation>> getAllAnnotations(@Nullable final String type) {
return getCurrentPdfFragment().map(PdfFragment::getDocument)
.flatMap(pdfDocument -> pdfDocument.getAnnotationProvider().getAllAnnotationsOfTypeAsync(getTypeFromString(type)))
.flatMap(pdfDocument -> pdfDocument.getAnnotationProvider().getAllAnnotationsOfTypeAsync(getAnnotationTypeFromString(type)))
.toList();
}

private EnumSet<AnnotationType> getTypeFromString(@Nullable String type) {
if (type == null) {
return EnumSet.allOf(AnnotationType.class);
}
if ("pspdfkit/ink".equalsIgnoreCase(type)) {
return EnumSet.of(AnnotationType.INK);
}
if ("pspdfkit/link".equalsIgnoreCase(type)) {
return EnumSet.of(AnnotationType.LINK);
}
if ("pspdfkit/markup/highlight".equalsIgnoreCase(type)) {
return EnumSet.of(AnnotationType.HIGHLIGHT);
}
if ("pspdfkit/markup/squiggly".equalsIgnoreCase(type)) {
return EnumSet.of(AnnotationType.SQUIGGLY);
}
if ("pspdfkit/markup/strikeout".equalsIgnoreCase(type)) {
return EnumSet.of(AnnotationType.STRIKEOUT);
}
if ("pspdfkit/markup/underline".equalsIgnoreCase(type)) {
return EnumSet.of(AnnotationType.UNDERLINE);
}
if ("pspdfkit/note".equalsIgnoreCase(type)) {
return EnumSet.of(AnnotationType.NOTE);
}
if ("pspdfkit/shape/ellipse".equalsIgnoreCase(type)) {
return EnumSet.of(AnnotationType.CIRCLE);
}
if ("pspdfkit/shape/line".equalsIgnoreCase(type)) {
return EnumSet.of(AnnotationType.LINE);
}
if ("pspdfkit/shape/polygon".equalsIgnoreCase(type)) {
return EnumSet.of(AnnotationType.POLYGON);
}
if ("pspdfkit/shape/polyline".equalsIgnoreCase(type)) {
return EnumSet.of(AnnotationType.POLYLINE);
}
if ("pspdfkit/shape/rectangle".equalsIgnoreCase(type)) {
return EnumSet.of(AnnotationType.SQUARE);
}
if ("pspdfkit/text".equalsIgnoreCase(type)) {
return EnumSet.of(AnnotationType.FREETEXT);
}
return EnumSet.noneOf(AnnotationType.class);
}


public Disposable addAnnotation(final int requestId, ReadableMap annotation) {
return getCurrentPdfFragment().map(PdfFragment::getDocument).subscribeOn(Schedulers.io())
Expand Down Expand Up @@ -524,7 +480,7 @@ public Disposable removeAnnotation(final int requestId, ReadableMap annotation)
return Observable.empty();
}

return pdfDocument.getAnnotationProvider().getAllAnnotationsOfTypeAsync(getTypeFromString(type), pageIndex, 1)
return pdfDocument.getAnnotationProvider().getAllAnnotationsOfTypeAsync(getAnnotationTypeFromString(type), pageIndex, 1)
.filter(annotationToFilter -> name.equals(annotationToFilter.getName()))
.map(filteredAnnotation -> new Pair<>(filteredAnnotation, pdfDocument));
})
Expand Down
6 changes: 6 additions & 0 deletions ios/RCTPSPDFKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
657278111D86AEC600A5E1A8 /* RCTConvert+PSPDFDocument.m in Sources */ = {isa = PBXBuildFile; fileRef = 657278101D86AEC600A5E1A8 /* RCTConvert+PSPDFDocument.m */; };
84545BA4210A5CCF00FBB0A7 /* RCTConvert+PSPDFAnnotation.m in Sources */ = {isa = PBXBuildFile; fileRef = 84545BA2210A5CCF00FBB0A7 /* RCTConvert+PSPDFAnnotation.m */; };
84694AA822AFC7510077FD01 /* RCTConvert+UIBarButtonItem.m in Sources */ = {isa = PBXBuildFile; fileRef = 84694AA722AFC7510077FD01 /* RCTConvert+UIBarButtonItem.m */; };
84B9870023FC208600C6711A /* RCTConvert+PSPDFAnnotationChange.m in Sources */ = {isa = PBXBuildFile; fileRef = 84B986FF23FC208600C6711A /* RCTConvert+PSPDFAnnotationChange.m */; };
84BC2EAD229EE9FF00A386C6 /* RCTConvert+PSPDFViewMode.m in Sources */ = {isa = PBXBuildFile; fileRef = 84BC2EAC229EE9FF00A386C6 /* RCTConvert+PSPDFViewMode.m */; };
B783BA3421C3F55300FD981A /* RCTConvert+PSPDFAnnotationToolbarConfiguration.m in Sources */ = {isa = PBXBuildFile; fileRef = B783BA3321C3F55300FD981A /* RCTConvert+PSPDFAnnotationToolbarConfiguration.m */; };
F84F8B192032D54F00153D9E /* RCTPSPDFKitView.m in Sources */ = {isa = PBXBuildFile; fileRef = F84F8B182032D54F00153D9E /* RCTPSPDFKitView.m */; };
Expand Down Expand Up @@ -42,6 +43,8 @@
84545BA3210A5CCF00FBB0A7 /* RCTConvert+PSPDFAnnotation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "RCTConvert+PSPDFAnnotation.h"; sourceTree = "<group>"; };
84694AA622AFC7510077FD01 /* RCTConvert+UIBarButtonItem.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "RCTConvert+UIBarButtonItem.h"; sourceTree = "<group>"; };
84694AA722AFC7510077FD01 /* RCTConvert+UIBarButtonItem.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "RCTConvert+UIBarButtonItem.m"; sourceTree = "<group>"; };
84B986FE23FC208600C6711A /* RCTConvert+PSPDFAnnotationChange.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "RCTConvert+PSPDFAnnotationChange.h"; sourceTree = "<group>"; };
84B986FF23FC208600C6711A /* RCTConvert+PSPDFAnnotationChange.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "RCTConvert+PSPDFAnnotationChange.m"; sourceTree = "<group>"; };
84BC2EAB229EE9FF00A386C6 /* RCTConvert+PSPDFViewMode.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "RCTConvert+PSPDFViewMode.h"; sourceTree = "<group>"; };
84BC2EAC229EE9FF00A386C6 /* RCTConvert+PSPDFViewMode.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "RCTConvert+PSPDFViewMode.m"; sourceTree = "<group>"; };
B783BA3221C3F55300FD981A /* RCTConvert+PSPDFAnnotationToolbarConfiguration.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "RCTConvert+PSPDFAnnotationToolbarConfiguration.h"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -92,6 +95,8 @@
84BC2EAC229EE9FF00A386C6 /* RCTConvert+PSPDFViewMode.m */,
84694AA622AFC7510077FD01 /* RCTConvert+UIBarButtonItem.h */,
84694AA722AFC7510077FD01 /* RCTConvert+UIBarButtonItem.m */,
84B986FE23FC208600C6711A /* RCTConvert+PSPDFAnnotationChange.h */,
84B986FF23FC208600C6711A /* RCTConvert+PSPDFAnnotationChange.m */,
);
path = Converters;
sourceTree = "<group>";
Expand Down Expand Up @@ -178,6 +183,7 @@
84545BA4210A5CCF00FBB0A7 /* RCTConvert+PSPDFAnnotation.m in Sources */,
6540D1841D89D22E00B8F94F /* RCTPSPDFKitManager.m in Sources */,
84694AA822AFC7510077FD01 /* RCTConvert+UIBarButtonItem.m in Sources */,
84B9870023FC208600C6711A /* RCTConvert+PSPDFAnnotationChange.m in Sources */,
B783BA3421C3F55300FD981A /* RCTConvert+PSPDFAnnotationToolbarConfiguration.m in Sources */,
F84F8B192032D54F00153D9E /* RCTPSPDFKitView.m in Sources */,
6572780C1D86AE7300A5E1A8 /* RCTConvert+PSPDFConfiguration.m in Sources */,
Expand Down
18 changes: 18 additions & 0 deletions ios/RCTPSPDFKit/Converters/RCTConvert+PSPDFAnnotationChange.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// Copyright © 2020 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 <React/RCTConvert.h>
@import PSPDFKit;
@import PSPDFKitUI;

@interface RCTConvert (PSPDFAnnotationChange)

+ (PSPDFAnnotationChange)PSPDFAnnotationChange:(NSString *)annotationChange;

@end
28 changes: 28 additions & 0 deletions ios/RCTPSPDFKit/Converters/RCTConvert+PSPDFAnnotationChange.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// Copyright © 2020 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 "RCTConvert+PSPDFAnnotationChange.h"

@implementation RCTConvert (PSPDFAnnotationChange)

+ (PSPDFAnnotationChange)PSPDFAnnotationChange:(NSString *)annotationChange {
if ([annotationChange isEqualToString:@"flatten"]) {
return PSPDFAnnotationChangeFlatten;
} else if ([annotationChange isEqualToString:@"remove"]) {
return PSPDFAnnotationChangeRemove;
} else if ([annotationChange isEqualToString:@"embed"]) {
return PSPDFAnnotationChangeEmbed;
} else if ([annotationChange isEqualToString:@"print"]) {
return PSPDFAnnotationChangePrint;
} else {
return PSPDFAnnotationChangeEmbed;
}
}

@end
24 changes: 24 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,28 @@ @implementation RCTPSPDFKitManager
}
}

#pragma mark - Annotation Processing

RCT_REMAP_METHOD(processAnnotations, processAnnotations:(PSPDFAnnotationChange)annotationChange annotationType:(PSPDFAnnotationType)annotationType sourceDocument:(PSPDFDocument *)sourceDocument processedDocumentPath:(nonnull NSString *)processedDocumentPath resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
NSError *error;
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
10 changes: 5 additions & 5 deletions ios/RCTPSPDFKit/RCTPSPDFKitViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,11 @@ @implementation RCTPSPDFKitViewManager
dispatch_async(dispatch_get_main_queue(), ^{
RCTPSPDFKitView *component = (RCTPSPDFKitView *)[self.bridge.uiManager viewForReactTag:reactTag];
BOOL success = [component setFormFieldValue:value fullyQualifiedName:fullyQualifiedName];
if (success) {
resolve(@(success));
} else {
reject(@"error", @"Failed to set form field value.", nil);
}
if (success) {
resolve(@(success));
} else {
reject(@"error", @"Failed to set form field value.", nil);
}
});
}

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-pspdfkit",
"version": "1.27.3",
"version": "1.27.4",
"description": "A React Native module for the PSPDFKit library.",
"keywords": [
"react native",
Expand Down
Loading