Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

JSON Serializer added #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions ILPDFKit/Model/PDFDocument.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 4 additions & 1 deletion ILPDFKit/Model/PDFDocument.m
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,10 @@ -(NSUInteger)numberOfPages
}

#pragma mark - PDF File Saving and Converting

-(NSString*)formJSON
{
return [self.forms formJSON];
}

-(NSString*)formXML
{
Expand Down
10 changes: 10 additions & 0 deletions ILPDFKit/Model/PDFFormContainer.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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
* ---------------------------------------------------------------------------------------
Expand Down
42 changes: 42 additions & 0 deletions ILPDFKit/Model/PDFFormContainer.m
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}];
```

Expand All @@ -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
```



Expand Down