diff --git a/ILPDFKit/Model/PDFDocument.h b/ILPDFKit/Model/PDFDocument.h old mode 100644 new mode 100755 index c6b18f4..e0dbbba --- a/ILPDFKit/Model/PDFDocument.h +++ b/ILPDFKit/Model/PDFDocument.h @@ -131,6 +131,10 @@ -(UIImage*)imageFromPage:(NSUInteger)page width:(NSUInteger)width; +/** Sets the background color for the PDF view. + @return A string containing an json representation of the forms of the document and their values. Used for submitting the form. + */ +-(NSString*)formJSON; /** Sets the background color for the PDF view. @return A string containing an xml representation of the forms of the document and their values. Used for submitting the form. diff --git a/ILPDFKit/Model/PDFDocument.m b/ILPDFKit/Model/PDFDocument.m old mode 100644 new mode 100755 index 41dc8e8..a5c3841 --- a/ILPDFKit/Model/PDFDocument.m +++ b/ILPDFKit/Model/PDFDocument.m @@ -167,7 +167,10 @@ -(NSUInteger)numberOfPages } #pragma mark - PDF File Saving and Converting - +-(NSString*)formJSON +{ + return [self.forms formJSON]; +} -(NSString*)formXML { diff --git a/ILPDFKit/Model/PDFFormContainer.h b/ILPDFKit/Model/PDFFormContainer.h old mode 100644 new mode 100755 index 541a441..2c824c8 --- a/ILPDFKit/Model/PDFFormContainer.h +++ b/ILPDFKit/Model/PDFFormContainer.h @@ -101,6 +101,16 @@ */ -(void)setValue:(NSString*)val ForFormWithName:(NSString*)name; +/**--------------------------------------------------------------------------------------- + * @name JSON + * --------------------------------------------------------------------------------------- + */ + +/** Returns an JSON representation of the form values in the document. + @return The json string defining the value and hierarchical structure of all forms in the document. + */ +-(NSString*)formJSON; + /**--------------------------------------------------------------------------------------- * @name XML * --------------------------------------------------------------------------------------- diff --git a/ILPDFKit/Model/PDFFormContainer.m b/ILPDFKit/Model/PDFFormContainer.m old mode 100644 new mode 100755 index da5d9be..84b4c87 --- a/ILPDFKit/Model/PDFFormContainer.m +++ b/ILPDFKit/Model/PDFFormContainer.m @@ -239,6 +239,48 @@ -(void)setValue:(NSString*)val ForFormWithName:(NSString*)name } } +#pragma mark - formJSON + +-(NSString*)formJSON +{ + NSError *error; + NSData *jsonData = [NSJSONSerialization dataWithJSONObject:[self formSerializableDictionaryForFormsWithRootNode:_nameTree] + options:NSJSONWritingPrettyPrinted + error:&error]; + + if (! jsonData) { + NSLog(@"Got an error: %@", error); + return @"{}"; + } + else { + return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; + } +} + +-(NSDictionary*)formSerializableDictionaryForFormsWithRootNode:(NSDictionary*)node +{ + NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; + for(NSString* key in [node allKeys]) + { + id obj = [node objectForKey:key]; + if([obj isKindOfClass:[NSMutableArray class]]) + { + PDFForm* form = (PDFForm*)[obj lastObject]; + if([form.value length]){ + [dict setObject:form.value forKey:key]; + } + } + else + { + NSDictionary* val = [self formSerializableDictionaryForFormsWithRootNode:obj]; + if([val count]){ + [dict setObject:val forKey:key]; + } + } + } + return dict; +} + #pragma mark - formXML -(NSString*)formXML diff --git a/README.md b/README.md index 38ca611..d388d5a 100755 --- a/README.md +++ b/README.md @@ -85,9 +85,12 @@ for(PDFForm* form in _pdfViewController.document.forms){ ```objective-c [_pdfViewController.document saveFormsToDocumentData^(BOOL success) { - /* At this point, _pdfViewController.documentData represents the updated PDF. + /* At this point, _pdfViewController.document represents the updated PDF. You can do as you wish with it. Upload, save to disk etc. */ + NSString *filePath = [NSString stringWithFormat:@"%@updated.pdf",NSTemporaryDirectory()]; + NSData *data = [_pdfViewController.document flattenedData]; + [data writeToFile:filePath atomically:YES]; }]; ``` @@ -97,6 +100,12 @@ for(PDFForm* form in _pdfViewController.document.forms){ NSString* documentFormsXML = [_pdfViewController.document formsXML]; // Push to webservice ``` + +### Sending Form JSON Data +```objective-c +NSString* documentFormsJSON = [_pdfViewController.document formsJSON]; +// Push to webservice +```