@@ -30,10 +30,22 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
3030 } else if ([@" setLicenseKey" isEqualToString: call.method]) {
3131 NSString *licenseKey = call.arguments [@" licenseKey" ];
3232 [PSPDFKit setLicenseKey: licenseKey];
33+ } else if ([@" setFormFieldValue" isEqualToString: call.method]) {
34+ NSString *value = call.arguments [@" value" ];
35+ NSString *fullyQualifiedName = call.arguments [@" fullyQualifiedName" ];
36+ result ([self setFormFieldValue: value forFieldWithFullyQualifiedName: fullyQualifiedName]);
37+ } else if ([@" getFormFieldValue" isEqualToString: call.method]) {
38+ NSString *fullyQualifiedName = call.arguments [@" fullyQualifiedName" ];
39+ result ([self getFormFieldValueForFieldWithFullyQualifiedName: fullyQualifiedName]);
3340 } else if ([@" present" isEqualToString: call.method]) {
3441 NSString *documentPath = call.arguments [@" document" ];
35- NSAssert (documentPath != nil , @" Document path may not be nil." );
36- NSAssert (documentPath.length != 0 , @" Document path may not be empty." );
42+
43+ if (documentPath == nil || documentPath.length <= 0 ) {
44+ FlutterError *error = [FlutterError errorWithCode: @" " message: @" Document path may not be nil or empty." details: nil ];
45+ result (error);
46+ return ;
47+ }
48+
3749 NSDictionary *configurationDictionary = call.arguments [@" configuration" ];
3850
3951 PSPDFDocument *document = [self document: documentPath];
@@ -52,6 +64,7 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
5264 UINavigationController *navigationController = [[UINavigationController alloc ] initWithRootViewController: self .pdfViewController];
5365 UIViewController *presentingViewController = [UIApplication sharedApplication ].delegate .window .rootViewController ;
5466 [presentingViewController presentViewController: navigationController animated: YES completion: nil ];
67+ result (@(YES ));
5568 } else {
5669 result (FlutterMethodNotImplemented);
5770 }
@@ -153,6 +166,78 @@ - (void)setRightBarButtonItems:(nullable NSArray <NSString *> *)items {
153166 [self .pdfViewController.navigationItem setRightBarButtonItems: [rightItems copy ] animated: NO ];
154167}
155168
169+ #pragma mark - Forms
170+
171+ - (id )setFormFieldValue : (NSString *)value forFieldWithFullyQualifiedName : (NSString *)fullyQualifiedName {
172+ PSPDFDocument *document = self.pdfViewController .document ;
173+
174+ if (!document || !document.isValid ) {
175+ FlutterError *error = [FlutterError errorWithCode: @" " message: @" PDF document not found or is invalid." details: nil ];
176+ return error;
177+ }
178+
179+ if (fullyQualifiedName == nil || fullyQualifiedName.length == 0 ) {
180+ FlutterError *error = [FlutterError errorWithCode: @" " message: @" Fully qualified name may not be nil or empty." details: nil ];
181+ return error;
182+ }
183+
184+ BOOL success = NO ;
185+ for (PSPDFFormElement *formElement in document.formParser .forms ) {
186+ if ([formElement.fullyQualifiedFieldName isEqualToString: fullyQualifiedName]) {
187+ if ([formElement isKindOfClass: PSPDFButtonFormElement.class]) {
188+ if ([value isEqualToString: @" selected" ]) {
189+ [(PSPDFButtonFormElement *)formElement select ];
190+ success = YES ;
191+ } else if ([value isEqualToString: @" deselected" ]) {
192+ [(PSPDFButtonFormElement *)formElement deselect ];
193+ success = YES ;
194+ }
195+ } else if ([formElement isKindOfClass: PSPDFChoiceFormElement.class]) {
196+ ((PSPDFChoiceFormElement *)formElement).selectedIndices = [NSIndexSet indexSetWithIndex: value.integerValue];
197+ success = YES ;
198+ } else if ([formElement isKindOfClass: PSPDFTextFieldFormElement.class]) {
199+ formElement.contents = value;
200+ success = YES ;
201+ } else if ([formElement isKindOfClass: PSPDFSignatureFormElement.class]) {
202+ FlutterError *error = [FlutterError errorWithCode: @" " message: @" Signature form elements are not supported." details: nil ];
203+ return error;
204+ } else {
205+ return @(NO );
206+ }
207+ break ;
208+ }
209+ }
210+
211+ if (!success) {
212+ FlutterError *error = [FlutterError errorWithCode: @" " message: [NSString stringWithFormat: @" Error while searching for a form element with name %@ ." , fullyQualifiedName] details: nil ];
213+ return error;
214+ }
215+
216+ return @(YES );
217+ }
218+
219+ - (id )getFormFieldValueForFieldWithFullyQualifiedName : (NSString *)fullyQualifiedName {
220+ if (fullyQualifiedName == nil || fullyQualifiedName.length == 0 ) {
221+ FlutterError *error = [FlutterError errorWithCode: @" " message: @" Fully qualified name may not be nil or empty." details: nil ];
222+ return error;
223+ }
224+
225+ PSPDFDocument *document = self.pdfViewController .document ;
226+ id formFieldValue = nil ;
227+ for (PSPDFFormElement *formElement in document.formParser .forms ) {
228+ if ([formElement.fullyQualifiedFieldName isEqualToString: fullyQualifiedName]) {
229+ formFieldValue = formElement.value ;
230+ break ;
231+ }
232+ }
233+
234+ if (formFieldValue == nil ) {
235+ FlutterError *error = [FlutterError errorWithCode: @" " message: [NSString stringWithFormat: @" Error while searching for a form element with name %@ ." , fullyQualifiedName] details: nil ];
236+ return error;
237+ }
238+
239+ return formFieldValue;
240+ }
156241
157242# pragma mark - Helpers
158243
@@ -196,7 +281,6 @@ - (PSPDFThumbnailBarMode)thumbnailBarMode:(NSDictionary *)dictionary {
196281 return thumbnailBarMode;
197282}
198283
199-
200284- (PSPDFAppearanceMode)appearanceMode : (NSDictionary *)dictionary {
201285 if ((id )dictionary == NSNull .null || !dictionary || dictionary.count == 0 ) {
202286 return PSPDFAppearanceModeDefault;
0 commit comments