Skip to content

Commit cd6116c

Browse files
committed
Added iOS implementation for applyInstantJson and exportInstantJson. Added the apply instant Json example to iOS.
1 parent 66c4528 commit cd6116c

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

example/lib/main.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,17 @@ class _MyAppState extends State<MyApp> {
299299
Text(_formExampleSub, style: subhead)
300300
])),
301301
),
302+
Divider(),
303+
GestureDetector(
304+
onTap: importInstantJsonExample,
305+
child: Container(
306+
padding: padding,
307+
child: Column(crossAxisAlignment: crossAxisAlignment, children: [
308+
309+
Text(_importInstantJsonExample, style: title),
310+
Text(_importInstantJsonExampleSub, style: subhead)
311+
])),
312+
),
302313
Divider()
303314
];
304315
return CupertinoApp(

ios/Classes/PspdfkitPlugin.m

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,39 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
3737
} else if ([@"getFormFieldValue" isEqualToString:call.method]) {
3838
NSString *fullyQualifiedName = call.arguments[@"fullyQualifiedName"];
3939
result([self getFormFieldValueForFieldWithFullyQualifiedName:fullyQualifiedName]);
40+
} else if ([@"applyInstantJson" isEqualToString:call.method]) {
41+
NSString *annotationsJson = call.arguments[@"annotationsJson"];
42+
if (annotationsJson == nil || annotationsJson.length <= 0) {
43+
result([FlutterError errorWithCode:@"" message:@"annotationsJson may not be nil or empty." details:nil]);
44+
return;
45+
}
46+
PSPDFDocument *document = self.pdfViewController.document;
47+
if (!document || !document.isValid) {
48+
result([FlutterError errorWithCode:@"" message:@"PDF document not found or is invalid." details:nil]);
49+
return;
50+
}
51+
PSPDFDataContainerProvider *jsonContainer = [[PSPDFDataContainerProvider alloc] initWithData:[annotationsJson dataUsingEncoding:NSUTF8StringEncoding]];
52+
NSError *error;
53+
BOOL success = [document applyInstantJSONFromDataProvider:jsonContainer toDocumentProvider:document.documentProviders.firstObject lenient:NO error:&error];
54+
if (!success) {
55+
result([FlutterError errorWithCode:@"" message:@"Error while importing document Instant JSON." details:nil]);
56+
} else {
57+
result(@(YES));
58+
}
59+
} else if ([@"exportInstantJson" isEqualToString:call.method]) {
60+
PSPDFDocument *document = self.pdfViewController.document;
61+
if (!document || !document.isValid) {
62+
result([FlutterError errorWithCode:@"" message:@"PDF document not found or is invalid." details:nil]);
63+
return;
64+
}
65+
NSError *error;
66+
NSData *data = [document generateInstantJSONFromDocumentProvider:document.documentProviders.firstObject error:&error];
67+
NSString *annotationsJson = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
68+
if (annotationsJson == nil || annotationsJson.length <= 0 || error) {
69+
result([FlutterError errorWithCode:@"" message:@"Error while exporting document Instant JSON." details:nil]);
70+
} else {
71+
result(annotationsJson);
72+
}
4073
} else if ([@"present" isEqualToString:call.method]) {
4174
NSString *documentPath = call.arguments[@"document"];
4275

0 commit comments

Comments
 (0)