@@ -190,6 +190,10 @@ - (BOOL)addAnnotation:(id)jsonAnnotation {
190190 }
191191
192192 PSPDFDocument *document = self.pdfController .document ;
193+ if (![document isValid ]) {
194+ NSLog (@" Document is invalid." );
195+ return NO ;
196+ }
193197 PSPDFDocumentProvider *documentProvider = document.documentProviders .firstObject ;
194198
195199 BOOL success = NO ;
@@ -207,18 +211,22 @@ - (BOOL)addAnnotation:(id)jsonAnnotation {
207211
208212- (BOOL )removeAnnotationWithUUID : (NSString *)annotationUUID {
209213 PSPDFDocument *document = self.pdfController .document ;
210-
214+ if (![document isValid ]) {
215+ NSLog (@" Document is invalid." );
216+ return NO ;
217+ }
218+
211219 BOOL success = NO ;
212220
213221 NSArray <PSPDFAnnotation *> *allAnnotations = [[document allAnnotationsOfType: PSPDFAnnotationTypeAll].allValues valueForKeyPath: @" @unionOfArrays.self" ];
214- for (PSPDFAnnotation *annotation in allAnnotations) {
215- // Remove the annotation if the name matches.
216- if ([annotation.uuid isEqualToString: annotationUUID]) {
217- success = [document removeAnnotations: @[annotation] options: nil ];
218- break ;
219- }
222+ for (PSPDFAnnotation *annotation in allAnnotations) {
223+ // Remove the annotation if the name matches.
224+ if ([annotation.uuid isEqualToString: annotationUUID]) {
225+ success = [document removeAnnotations: @[annotation] options: nil ];
226+ break ;
220227 }
221-
228+ }
229+
222230 if (!success) {
223231 NSLog (@" Failed to remove annotation." );
224232 }
@@ -245,12 +253,18 @@ - (BOOL)addAnnotations:(id)jsonAnnotations {
245253
246254 PSPDFDataContainerProvider *dataContainerProvider = [[PSPDFDataContainerProvider alloc ] initWithData: data];
247255 PSPDFDocument *document = self.pdfController .document ;
256+ if (![document isValid ]) {
257+ NSLog (@" Document is invalid." );
258+ return NO ;
259+ }
260+
248261 PSPDFDocumentProvider *documentProvider = document.documentProviders .firstObject ;
249- BOOL success = [document applyInstantJSONFromDataProvider: dataContainerProvider toDocumentProvider: documentProvider error: NULL ];
262+ BOOL success = [document applyInstantJSONFromDataProvider: dataContainerProvider toDocumentProvider: documentProvider lenient: NO error: NULL ];
250263 if (!success) {
251264 NSLog (@" Failed to add annotations." );
252265 }
253266
267+ [self .pdfController reloadPageAtIndex: self .pdfController.pageIndex animated: NO ];
254268 return success;
255269}
256270
@@ -263,6 +277,11 @@ - (BOOL)addAnnotations:(id)jsonAnnotations {
263277 }
264278
265279 PSPDFDocument *document = self.pdfController .document ;
280+ if (![document isValid ]) {
281+ NSLog (@" Document is invalid." );
282+ return nil ;
283+ }
284+
266285 for (PSPDFFormElement *formElement in document.formParser .forms ) {
267286 if ([formElement.fullyQualifiedFieldName isEqualToString: fullyQualifiedName]) {
268287 id formFieldValue = formElement.value ;
@@ -280,6 +299,11 @@ - (void)setFormFieldValue:(NSString *)value fullyQualifiedName:(NSString *)fully
280299 }
281300
282301 PSPDFDocument *document = self.pdfController .document ;
302+ if (![document isValid ]) {
303+ NSLog (@" Document is invalid." );
304+ return ;
305+ }
306+
283307 for (PSPDFFormElement *formElement in document.formParser .forms ) {
284308 if ([formElement.fullyQualifiedFieldName isEqualToString: fullyQualifiedName]) {
285309 if ([formElement isKindOfClass: PSPDFButtonFormElement.class]) {
0 commit comments