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

Converted to storyboard, added uiactionviewcontroller to handle all docu... #7

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
2 changes: 1 addition & 1 deletion ILPDFKit/Controller/PDFViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
/** Sets the background color for the PDF view.
@param color The new color.
*/
-(void)setBackColor:(UIColor*)color;
-(void)setBackColor:(UIColor*)color animated:(BOOL)animated;



Expand Down
89 changes: 75 additions & 14 deletions ILPDFKit/Controller/PDFViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
#import "PDF.h"

@interface PDFViewController()
{
UIColor *backColor;
UITableViewCell *loading;
}

-(void)loadPDFView;
-(CGRect)currentFrame:(UIInterfaceOrientation)o;
-(CGPoint)getMargins;
Expand Down Expand Up @@ -42,6 +47,7 @@ -(void)viewDidLoad
self.view.backgroundColor = [UIColor whiteColor];
self.view.opaque = YES;
[self loadPDFView];
[self showLoading];
}

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
Expand Down Expand Up @@ -74,7 +80,6 @@ -(id)initWithData:(NSData*)data
self = [super init];
if(self != nil)
{

_document = [[PDFDocument alloc] initWithData:data];
}
return self;
Expand Down Expand Up @@ -103,6 +108,27 @@ -(id)initWithPath:(NSString*)path

#pragma mark - Configuration

-(void)showLoading
{
loading = [[UITableViewCell alloc] init];
loading.backgroundColor = [UIColor clearColor];
loading.center = CGPointMake(self.view.frame.size.width/2+95, self.view.frame.size.height/2-44);
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];

CGSize size = CGSizeMake(15, spinner.frame.size.height);
UIGraphicsBeginImageContext(size);
UIImage* spacer = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
loading.imageView.image = spacer;
[loading.imageView addSubview:spinner];

[spinner startAnimating];

loading.textLabel.text = @"Loading…";
loading.textLabel.textColor = [UIColor darkGrayColor];
[self.view addSubview:loading];
}

-(void)reload
{
[_document refresh];
Expand All @@ -111,9 +137,19 @@ -(void)reload
[self loadPDFView];
}

-(void)setBackColor:(UIColor*)color
-(void)setBackColor:(UIColor*)color animated:(BOOL)animated
{
_pdfView.pdfView.backgroundColor = color;
backColor = color;

if(!backColor)
backColor = [UIColor colorWithRed:0.74f green:0.74f blue:0.76f alpha:1.f];

if(animated)
[UIView animateWithDuration:0.3f animations:^{
_pdfView.pdfView.backgroundColor = backColor;
}];
else
_pdfView.pdfView.backgroundColor = backColor;
}

#pragma mark - Hidden
Expand All @@ -132,26 +168,51 @@ -(CGRect)currentFrame:(UIInterfaceOrientation)o

-(void)loadPDFView
{
id pass = (_document.documentPath?_document.documentPath:_document.documentData);
CGRect frm = [self currentFrame:self.interfaceOrientation];
self.view.frame = CGRectMake(0,self.view.frame.origin.y,frm.size.width,frm.size.height-self.view.frame.origin.y);
CGPoint margins = [self getMargins];
NSArray* additionViews = [_document.forms createWidgetAnnotationViewsForSuperviewWithWidth:frm.size.width Margin:margins.x HMargin:margins.y];
_pdfView = [[PDFView alloc] initWithFrame:self.view.bounds DataOrPath:pass AdditionViews:additionViews];
[self.view addSubview:_pdfView];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0), ^{
id pass = (_document.documentPath?_document.documentPath:_document.documentData);
CGRect frm = [self currentFrame:self.interfaceOrientation];
self.view.frame = CGRectMake(0,self.view.frame.origin.y,frm.size.width,frm.size.height-self.view.frame.origin.y);
CGPoint margins = [self getMargins];
NSArray* additionViews = [_document.forms createWidgetAnnotationViewsForSuperviewWithWidth:frm.size.width Margin:margins.x HMargin:margins.y];

dispatch_async(dispatch_get_main_queue(), ^(void){
_pdfView = [[PDFView alloc] initWithFrame:self.view.bounds DataOrPath:pass AdditionViews:additionViews];
[self.view addSubview:_pdfView];
[self setBackColor:backColor animated:FALSE];
[loading removeFromSuperview];
});
});
}

-(CGPoint)getMargins
{
if(iPad)
{
if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation))return CGPointMake(PDFPortraitPadWMargin,PDFPortraitPadHMargin);
else return CGPointMake(PDFLandscapePadWMargin,PDFLandscapePadHMargin);
if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) {
if(SYSTEM_VERSION_LESS_THAN(@"7.0"))
return CGPointMake(PDFPortraitPadWMargin,PDFPortraitPadWMargin);

return CGPointMake(PDFPortraitPadWMargin,PDFPortraitPadHMargin);
} else {
if(SYSTEM_VERSION_LESS_THAN(@"7.0"))
return CGPointMake(PDFLandscapePadWMargin,PDFLandscapePadWMargin);

return CGPointMake(PDFLandscapePadWMargin,PDFLandscapePadHMargin);
}
}
else
{
if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation))return CGPointMake(PDFPortraitPhoneWMargin,PDFPortraitPhoneHMargin);
else return CGPointMake(PDFLandscapePhoneWMargin,PDFLandscapePhoneHMargin);
if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) {
if(SYSTEM_VERSION_LESS_THAN(@"7.0"))
return CGPointMake(PDFPortraitPhoneWMargin,PDFPortraitPhoneWMargin);

return CGPointMake(PDFPortraitPhoneWMargin,PDFPortraitPhoneHMargin);
} else {
if(SYSTEM_VERSION_LESS_THAN(@"7.0"))
return CGPointMake(PDFLandscapePhoneWMargin,PDFLandscapePhoneWMargin);

return CGPointMake(PDFLandscapePhoneWMargin,PDFLandscapePhoneHMargin);
}
}
}

Expand Down
28 changes: 28 additions & 0 deletions ILPDFKit/ILPDFKit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// ILPDFKit.h
// ILPDFKit
//
// Created by Brock Haymond on 3/21/14.
// Copyright (c) 2014 Interact. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ILPDFKit : UINavigationController

- (id)initWithPDF:(id)file;
- (void)setTitle:(NSString *)title;
- (void)setValue:(NSString*)value forFormWithName:(NSString*)name;
- (void)setReadOnly:(BOOL)value;

/**---------------------------------------------------------------------------------------
* @name Debugging
* ---------------------------------------------------------------------------------------
*/

/** Sets the acroforms to display form names when entered.
@param value Set to TRUE to log the form name.
*/
-(void)setDebugForms:(BOOL)value;

@end
184 changes: 184 additions & 0 deletions ILPDFKit/ILPDFKit.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
//
// ILPDFKit.m
// ILPDFKit
//
// Created by Brock Haymond on 3/21/14.
// Copyright (c) 2014 Interact. All rights reserved.
//

#import "ILPDFKit.h"

#import "PDF.h"

BOOL debugForms = FALSE;
BOOL readOnly = FALSE;

@interface ILPDFKit ()
{
id _file;
BOOL hasStatusBar;
PDFViewController* _pdfViewController;
UIPopoverController *activityPopover;
}

//@property (strong, nonatomic) UIPopoverController *activityPopover;

@end

@implementation ILPDFKit

- (id)initWithPDF:(id)file
{
self = [super init];
if (self)
{
_file = file;
if([file isKindOfClass:[NSData class]])
{
_pdfViewController = [[PDFViewController alloc] initWithData:file];
}
else if([[NSFileManager defaultManager] fileExistsAtPath:file])
{
_pdfViewController = [[PDFViewController alloc] initWithPath:file];
}
else if([[NSFileManager defaultManager] fileExistsAtPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:file]])
{
_pdfViewController = [[PDFViewController alloc] initWithResource:file];
}
else {
file = nil;
_pdfViewController = (PDFViewController*)[[UIViewController alloc] init];
_pdfViewController.view.backgroundColor = [UIColor colorWithRed:0.74f green:0.74f blue:0.76f alpha:1.f];
}

self.navigationBar.translucent = FALSE;
self.view.backgroundColor = [UIColor whiteColor];

[self setViewControllers:@[_pdfViewController]];

UIBarButtonItem* doneBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done:)];
[_pdfViewController.navigationItem setLeftBarButtonItems:@[doneBarButtonItem]];

if(!file) {
dispatch_async(dispatch_get_main_queue(),^{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Notice" message:@"File not found." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
});
return self;
}

if([_file isKindOfClass:[NSString class]])
_pdfViewController.title = [_file lastPathComponent];

UIBarButtonItem* actionBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(openActivitySheet:)];
[_pdfViewController.navigationItem setRightBarButtonItems:@[actionBarButtonItem]];

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backgroundTap)];
[self.view addGestureRecognizer:tap];

if(![UIApplication sharedApplication].statusBarHidden)
hasStatusBar = TRUE;
}

return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (void)backgroundTap
{
//Check to see if the keyboard is being closed, if so don't move in or out of fullscreen
if(_pdfViewController.pdfView->keyboardClosed) {
_pdfViewController.pdfView->keyboardClosed = FALSE;
return;
}

//Fade background in and out for fullscreen
if(!self.navigationBarHidden)
[_pdfViewController setBackColor:[UIColor blackColor] animated:TRUE];
else
[_pdfViewController setBackColor:nil animated:TRUE];

//Hide the status bar only if present
if(hasStatusBar)
[[UIApplication sharedApplication] setStatusBarHidden:!self.navigationBarHidden withAnimation:UIStatusBarAnimationFade];

[self setNavigationBarHidden:!self.navigationBarHidden animated:TRUE];
}

- (IBAction)done:(id)sender
{
[self dismissViewControllerAnimated:TRUE completion:^{}];
}

- (IBAction)openActivitySheet:(UIBarButtonItem*)sender
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0), ^{
//Get the PDF title.
NSString *title = @"Attachment-1.pdf";
if([_file isKindOfClass:[NSString class]])
title = [_file lastPathComponent];

//Write the PDF to a temp file.
NSString *path = [NSTemporaryDirectory() stringByAppendingString:title];
[[_pdfViewController.document flattenedData] writeToFile:path atomically:TRUE];

//Create an activity view controller with the PDF as its activity item.
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[[NSURL fileURLWithPath:path]] applicationActivities:nil];

dispatch_async(dispatch_get_main_queue(), ^(void){
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
//iPhone, present activity view controller as is.
[_pdfViewController presentViewController:activityViewController animated:YES completion:nil];
}
else
{
//iPad, present the view controller inside a popover.
if (![activityPopover isPopoverVisible]) {
activityPopover = [[UIPopoverController alloc] initWithContentViewController:activityViewController];
if(!self.navigationBarHidden)
[activityPopover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
else
{
//Dismiss if the button is tapped while popover is visible.
[activityPopover dismissPopoverAnimated:YES];
}
}
});
});
}

- (void)setTitle:(NSString *)title
{
_pdfViewController.title = title;
}

- (void)setValue:(NSString*)value forFormWithName:(NSString*)name
{
[_pdfViewController.document.forms setValue:value ForFormWithName:name];
}

- (void)setReadOnly:(BOOL)v
{
NSLog(@"%s %d", __FUNCTION__, v);
readOnly = v;
}

- (void)setDebugForms:(BOOL)v
{
NSLog(@"%s %d", __FUNCTION__, v);
debugForms = v;
}

@end
3 changes: 3 additions & 0 deletions ILPDFKit/Model/PDFForm.m
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,9 @@ -(PDFWidgetAnnotationView*)createWidgetAnnotationViewForSuperviewWithWidth:(CGFl

-(void)widgetAnnotationEntered:(PDFWidgetAnnotationView *)sender
{
if(debugForms)
NSLog(@"%s %@", __FUNCTION__, self.name);

[[_actions objectForKey:@"E"] execute];
[[_actions objectForKey:@"A"] execute];
}
Expand Down
4 changes: 3 additions & 1 deletion ILPDFKit/Model/PDFFormContainer.m
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ -(id)initWithParentDocument:(PDFDocument*)parent
[self enumerateFields:field PageMap:pmap];
}

[self loadJS];
dispatch_async(dispatch_get_main_queue(), ^(void){
[self loadJS];
});
}
return self;
}
Expand Down
Loading