Skip to content
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
16 changes: 16 additions & 0 deletions android/src/main/java/com/pspdfkit/react/ReactPdfViewManager.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.pspdfkit.react;

import android.app.Activity;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;

Expand Down Expand Up @@ -51,6 +52,7 @@ public class ReactPdfViewManager extends ViewGroupManager<PdfView> {
public static final int COMMAND_GET_FORM_FIELD_VALUE = 8;
public static final int COMMAND_SET_FORM_FIELD_VALUE = 9;
public static final int COMMAND_REMOVE_ANNOTATION = 10;
public static final int COMMAND_GET_ALL_ANNOTATIONS = 11;

private CompositeDisposable annotationDisposables = new CompositeDisposable();

Expand Down Expand Up @@ -100,6 +102,7 @@ public Map<String, Integer> getCommandsMap() {
commandMap.put("getFormFieldValue", COMMAND_GET_FORM_FIELD_VALUE);
commandMap.put("setFormFieldValue", COMMAND_SET_FORM_FIELD_VALUE);
commandMap.put("removeAnnotation", COMMAND_REMOVE_ANNOTATION);
commandMap.put("getAllAnnotations", COMMAND_GET_ALL_ANNOTATIONS);
return commandMap;
}

Expand Down Expand Up @@ -185,6 +188,19 @@ public void accept(List<Annotation> annotations) {
annotationDisposables.add(annotationDisposable);
}
break;
case COMMAND_GET_ALL_ANNOTATIONS:
if (args != null && args.size() == 2) {
final int requestId = args.getInt(0);
annotationDisposables.add(root.getAllAnnotations(args.getString(1))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(annotations -> {
root.getEventDispatcher().dispatchEvent(new PdfViewDataReturnedEvent(root.getId(), requestId, annotations));
}, throwable -> {
root.getEventDispatcher().dispatchEvent(new PdfViewDataReturnedEvent(root.getId(), requestId, throwable));
}));
}
break;
case COMMAND_ADD_ANNOTATION:
if (args != null && args.size() == 2) {
final int requestId = args.getInt(0);
Expand Down
17 changes: 11 additions & 6 deletions android/src/main/java/com/pspdfkit/views/PdfView.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

import android.content.Context;
import android.net.Uri;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentManager;

import android.util.AttributeSet;
import android.util.Pair;
import android.view.Choreographer;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentManager;

import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.uimanager.events.EventDispatcher;
import com.pspdfkit.annotations.Annotation;
Expand All @@ -30,7 +30,6 @@
import com.pspdfkit.forms.TextFormElement;
import com.pspdfkit.listeners.OnPreparePopupToolbarListener;
import com.pspdfkit.listeners.SimpleDocumentListener;
import com.pspdfkit.react.R;
import com.pspdfkit.react.events.PdfViewDataReturnedEvent;
import com.pspdfkit.react.events.PdfViewDocumentLoadFailedEvent;
import com.pspdfkit.react.events.PdfViewDocumentSaveFailedEvent;
Expand Down Expand Up @@ -449,11 +448,17 @@ public PdfDocument apply(PdfFragment pdfFragment) {
}).flatMap(new Function<PdfDocument, ObservableSource<Annotation>>() {
@Override
public ObservableSource<Annotation> apply(PdfDocument pdfDocument) {
return pdfDocument.getAnnotationProvider().getAllAnnotationsOfType(getTypeFromString(type), pageIndex, 1);
return pdfDocument.getAnnotationProvider().getAllAnnotationsOfTypeAsync(getTypeFromString(type), pageIndex, 1);
}
}).toList();
}

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

private EnumSet<AnnotationType> getTypeFromString(@Nullable String type) {
if (type == null) {
return EnumSet.allOf(AnnotationType.class);
Expand Down
34 changes: 34 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,40 @@ class PSPDFKitView extends React.Component {
}
};

/**
* Gets all annotations of the given type.
*
* @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.
*
* Returns a promise resolving an array with the following structure:
* {'annotations' : [instantJson]}
*/
getAllAnnotations = function(type) {
if (Platform.OS === "android") {
let requestId = this._nextRequestId++;
let requestMap = this._requestMap;

// We create a promise here that will be resolved once onDataReturned is called.
let promise = new Promise(function(resolve, reject) {
requestMap[requestId] = { resolve: resolve, reject: reject };
});

UIManager.dispatchViewManagerCommand(
findNodeHandle(this.refs.pdfView),
this._getViewManagerConfig("RCTPSPDFKitView").Commands
.getAllAnnotations,
[requestId, type]
);

return promise;
} else if (Platform.OS === "ios") {
return NativeModules.PSPDFKitViewManager.getAllAnnotations(
type,
findNodeHandle(this.refs.pdfView)
);
}
};

/**
* Applies the passed in document instant json.
*
Expand Down
1 change: 1 addition & 0 deletions ios/RCTPSPDFKit/RCTPSPDFKitView.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ NS_ASSUME_NONNULL_BEGIN
- (BOOL)addAnnotation:(id)jsonAnnotation error:(NSError *_Nullable *)error;
- (BOOL)removeAnnotationWithUUID:(NSString *)annotationUUID;
- (NSDictionary<NSString *, NSArray<NSDictionary *> *> *)getAllUnsavedAnnotationsWithError:(NSError *_Nullable *)error;
- (NSDictionary<NSString *, NSArray<NSDictionary *> *> *)getAllAnnotations:(PSPDFAnnotationType)type error:(NSError *_Nullable *)error;
- (BOOL)addAnnotations:(NSString *)jsonAnnotations error:(NSError *_Nullable *)error;

/// Forms
Expand Down
9 changes: 9 additions & 0 deletions ios/RCTPSPDFKit/RCTPSPDFKitView.m
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,15 @@ - (BOOL)removeAnnotationWithUUID:(NSString *)annotationUUID {
return annotationsJSON;
}

- (NSDictionary<NSString *, NSArray<NSDictionary *> *> *)getAllAnnotations:(PSPDFAnnotationType)type error:(NSError *_Nullable *)error {
PSPDFDocument *document = self.pdfController.document;
VALIDATE_DOCUMENT(document, nil)

NSArray<PSPDFAnnotation *> *annotations = [[document allAnnotationsOfType:type].allValues valueForKeyPath:@"@unionOfArrays.self"];
NSArray <NSDictionary *> *annotationsJSON = [RCTConvert instantJSONFromAnnotations:annotations error:error];
return @{@"annotations" : annotationsJSON};
}

- (BOOL)addAnnotations:(id)jsonAnnotations error:(NSError *_Nullable *)error {
NSData *data;
if ([jsonAnnotations isKindOfClass:NSString.class]) {
Expand Down
13 changes: 13 additions & 0 deletions ios/RCTPSPDFKit/RCTPSPDFKitViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,19 @@ @implementation RCTPSPDFKitViewManager
});
}

RCT_EXPORT_METHOD(getAllAnnotations:(NSString *)type 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;
NSDictionary *annotations = [component getAllAnnotations:[RCTConvert annotationTypeFromInstantJSONType:type] error:&error];
if (annotations) {
resolve(annotations);
} else {
reject(@"error", @"Failed to get all annotations.", error);
}
});
}

RCT_EXPORT_METHOD(addAnnotations:(id)jsonAnnotations reactTag:(nonnull NSNumber *)reactTag resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
dispatch_async(dispatch_get_main_queue(), ^{
RCTPSPDFKitView *component = (RCTPSPDFKitView *)[self.bridge.uiManager viewForReactTag:reactTag];
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.25.0",
"version": "1.25.1",
"description": "A React Native module for the PSPDFKit library.",
"keywords": [
"react native",
Expand Down
4 changes: 2 additions & 2 deletions samples/Catalog/Catalog.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -588,8 +588,8 @@ class PdfViewInstantJsonScreen extends Component<{}> {
<View>
<Button
onPress={() => {
// This gets all annotations on the first page.
this.refs.pdfView.getAnnotations(0, null).then(annotations => {
// This gets all annotations in the document.
this.refs.pdfView.getAllAnnotations().then(annotations => {
alert(JSON.stringify(annotations));
});
}}
Expand Down
20 changes: 20 additions & 0 deletions samples/Catalog/Catalog.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,26 @@ class ProgrammaticAnnotations extends Component {
title="getAllUnsavedAnnotations"
/>
</View>
<View>
<Button
onPress={async () => {
// Get all annotations annotations from the document.
await this.refs.pdfView
.getAllAnnotations()
.then(result => {
if (result) {
alert(JSON.stringify(result));
} else {
alert("Failed to get all annotations.");
}
})
.catch(error => {
alert(JSON.stringify(error));
});
}}
title="getAllAnnotations"
/>
</View>
</View>
</View>
);
Expand Down
2 changes: 1 addition & 1 deletion samples/Catalog/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Catalog",
"version": "1.25.0",
"version": "1.25.1",
"private": true,
"scripts": {
"start": "react-native start",
Expand Down