PlugPDF SDK iOS Developer Guide

GUIDE

PlugPDF VIEWER

  • How can I hide top bar and bottom bar?
  • How can I hide Page number indicator on PlugPDF Viewer?
  • How can I change Page number indicator position?
  • How can I detect that page was changed?
  • How can I get page slider value?
  • How to change page Preview size.
  • How can I go to the recent page when I close the PlugPDF Viewer?
  • How can I set Document title on PlugPDF Viewer?

ANNOTATION

  • How to get annotations information?
  • How to catch an annotation is added or removed?
  • Save and Remove annotations
  • Flatten annotations
  • Circle annotation
  • Square annotation
  • TextMarkup annotations ( Highlight, Underline, Strike Out)
  • Ink annotation
  • Note annotation
  • Eraser tool

DIGITAL SIGNATURE

Making an iOS application with the PlugPDF SDK

This tutorial walks you through the necessary steps to run a simple, fully functional mobile PDF reader on an iOS device. The goal is to get familiar with some basic PlugPDF concepts, as well as to build your first Objective-C application with Xcode.

Requirements

  1. An up-to-date version of both Xcode and iOS SDK, which can be downloaded from Apple’s developer site.
  2. The PlugPDF SDK supports XCode 5.0.2 onwards and iOS 5.1 onwards.

Steps required for making your first PDF iOS app

  1. Download the PlugPDF SDK
  2. Create a new Xcode project
  3. Add the PlugPDF framework
  4. Add some libraries
  5. Initialize the PlugPDF SDK
  6. Build the PDF viewer

1. Download the PlugPDF SDK

Download either a free trial license or an indie license as shown in the figure below.

Figure 1. Download PlugPDF now!

Figure 1. Download PlugPDF now!

You will then be redirected to a web page from which both PlugPDF for Android and iOS can be downloaded:

Figure 2. Thank you, your trial license key is on its way!

Figure 2. Thank you, your license key is on its way!

This is what the PlugPDF_iOS folder looks like.

Figure 3. PlugPDF_iOS folder structure

Figure 3. PlugPDF_iOS folder structure

Figure 4. PlugPDF_iOS folder content

Figure 4. PlugPDF_iOS folder content

On the other hand, you’ll instantly receive an email with your PlugPDF license key consisting of 56 alphanumeric characters like this example:

79F53DB9HHF9GH88EDB5297FH28FC6488AECC4EF496FB95A96CHFBF5

Please keep your license in a secure place since it will be required to initialize your PlugPDF applications, as we will show later in this paper.

2. Creating a new Xcode project

Let’s follow the steps illustrated in the figures below and create our first PDF application.

Figure 5. Open Xcode and choose File data-lazy-srcset= New > Project…” width=”300″ height=”287″ />

Figure 5. Open Xcode and choose File > New > Project…

Figure 6. Now select iOS data-lazy-srcset= Application > Single View Application ” width=”300″ height=”213″ />

Figure 6. Now select iOS > Application > Single View Application

Figure 7. Choose the options for your new project

Figure 7. Choose the options for your new project

Now Xcode will ask you to place your project under control version. By the way, this issue is not actually important for building your first PDF application, nevertheless let’s activate this default feature just to finish with the wizard.

Figure 8. Create Git repository

Figure 8. Select the Create Git repository checkbox, and finally click on the Create button

3. Adding the PlugPDF framework

At this point, we are adding both the PlugPDF.framework and the PlugPDF.bundle to the sample project. The former is the PDF library itself, while the latter contains the resources such as the fonts and images to be used.

Figure 9. Select your sample project from the Xcode window

Figure 9. Select your sample project from the Xcode window as shown here

Figure 10. Choose File data-lazy-srcset= Add Files to ” width=”300″ height=”194″ />

Figure 10. Choose File > Add Files to “sample”…

Figure 11. Select both PlugPDF files

Figure 11. Select both PlugPDF files as illustrated in this image and click on the Add button

If everything is ok your Xcode project navigator will display the PlugPDF.framework and the PlugPDF.bundle.

Figure 12. Xcode project navigator

Figure 12. Xcode project navigator

4. Adding libraries

We must now add the libstdc++.dylib,libc.tbd,and libz.dylib libraries as well as the AVFoundation framework. So, first choose Build Phase > Link Binary With Libraries and then press on the + button as shown in the figure below.

Figure 13. Click on the + button

Figure 13. Click on the + button

Use the search box when the dialog window is opened, and easily find these two dynamic libraries.

Figure 14. Adding the libstdc++.dylib and libz.dylib libraries

Figure 14. Adding the libstdc++.dylib and libz.dylib libraries

The last step is adding the AVFoundation.framework, after which the Linked Frameworks and Libraries will be properly set. Finally make sure that your project is set like this figure.

Figure 15. Linked Frameworks and Libraries

Figure 15. Linked Frameworks and Libraries

5. Initialize the PlugPDF SDK

The PlugPDF SDK is initialized from within the AppDelegate.m file by calling the PlugPDFInit function from the didFinishLaunchingWithOptions method.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    @try {
        PlugPDFInit((char *)"6AB4CF59HG4CFH34ED347F2EF48EGE596HA2639D4B588HH2DH5FFBF5");
    }
    @catch (NSException *exception) {
        NSLog(@"Exception %@", exception.description);
    }
    return YES;
}

Then we just have to call the PlugPDFUninit(); function from the applicationWillTerminate method.

- (void)applicationWillTerminate:(UIApplication *)application {
    PlugPDFUninit();
}

By the way, don’t forget to import the PlugPDF.h file.

#import "PlugPDF/PlugPDF.h"

In a nutshell, make sure that your AppDelegate.m file is as follows:

//
//  AppDelegate.m
//  sample
//
//  Created by ePapyrus on 23/12/15.
//  Copyright © 2015 plugpdf. All rights reserved.
//

#import "AppDelegate.h"
#import "PlugPDF/PlugPDF.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    @try {
        PlugPDFInit((char *)"6AB4CF59HG4CFH34ED347F2EF48EGE596HA2639D4B588HH2DH5FFBF5");
    }
    @catch (NSException *exception) {
        NSLog(@"Exception %@", exception.description);
    }
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    PlugPDFUninit();
}

@end

At this stage we can already run our first PDF application; if everything is ok Xcode will launch the iOS simulator.

6. Building the PDF viewer

In the previous section we have first created a Single View Application and then we’ve successfully initialized the PlugPDF SDK in the AppDelegate.m file, however we still haven’t seen any of the cool PDF features and classes that are at our disposal. With the PlugPDFDocumentViewController class, embedding the following fully functional PDF reader into your iOS app is really a snap.

Let’s look at this example:

Figure 16. The user is displayed the Gone With the Wind button

Figure 16. The user is displayed the Gone With the Wind button

Figure 17. The PDF ebook is opened once the button above is clicked

Figure 17. The PDF ebook is opened once the button above is clicked

Figure 18. The main tool bar is displayed when clicking on the top of the simulator’s screen

Figure 18. The main tool bar is displayed when clicking on the top of the simulator’s screen

This simple yet effective PDF reader comes with these tools:

  • Rotate
  • Page display mode
  • Brightness
  • Outline
  • Search
  • Edit

How can you embed this PDF reader into your app? Well, just open your AppDelegate.m file and create an UINavigationController object in the didFinishLaunchingWithOptions. This takes a couple of lines of code!

//
//  AppDelegate.m
//  sample
//
//  Created by ePapyrus on 23/12/15.
//  Copyright © 2015 plugpdf. All rights reserved.
//

#import "AppDelegate.h"
#import "PlugPDF/PlugPDF.h"
#import "ViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    @try {
        PlugPDFInit((char *)"6AB4CF59HG4CFH34ED347F2EF48EGE596HA2639D4B588HH2DH5FFBF5");
    }
    @catch (NSException *exception) {
        NSLog(@"Exception %@", exception.description);
    }
    
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController: [ViewController new]];
    
    [self.window setRootViewController: navigationController];
    
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    PlugPDFUninit();
}

@end

Then update your ViewController.m as follows:

//
//  ViewController.m
//  sample
//
//  Created by ePapyrus on 23/12/15.
//  Copyright © 2015 plugpdf. All rights reserved.
//

#import "ViewController.h"
#import "PlugPDF/DocumentViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *button = [[UIButton alloc] initWithFrame: CGRectMake(50, 80, 250, 50)];
    [button setBackgroundColor: [UIColor grayColor]];
    [button setTitle: @"Gone_With_the_Wind" forState: UIControlStateNormal];
    [button addTarget: self action: @selector(openPlugPDF:) forControlEvents: UIControlEventTouchUpInside];
    [self.view addSubview: button];
}

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

- (void)openPlugPDF: (id)sender {
    NSString *path = [[NSBundle mainBundle] pathForResource: @"Gone_With_the_Wind" ofType: @"pdf"];
    NSString *password = @"";
    NSString *title = [[path lastPathComponent] stringByDeletingPathExtension];
    
    PlugPDFDocumentViewController *viewController = nil;
    @try {
        viewController = [PlugPDFDocumentViewController initWithPath: path
                                                            password: password
                                                               title: title];
    }
    @catch (NSException *exception) {
        NSLog(@"Exception %@ %@", exception.name, exception.description);
    }
    
    if (viewController) {
        [self.navigationController pushViewController: viewController
                                             animated: YES];
        if ([[UIDevice currentDevice].systemVersion hasPrefix:@"7"] ||
            [[UIDevice currentDevice].systemVersion hasPrefix:@"8"]) {
            [viewController setAutomaticallyAdjustsScrollViewInsets: NO];
        }
    }
}

@end

To conclude, there’s one last important thing to notice:

NSString *path = [[NSBundle mainBundle] pathForResource: @"Gone_With_the_Wind" ofType: @"pdf"];

Indeed, our sample code is loading a PDF file called Gone_With_the_Wind.pdf but you can refer to any PDF file; in either case please add your PDF file to Xcode’s Copy Bundle Resources, otherwise you’ll get this error: Exception CorruptedPDF Thrown when a pass-in data or file is not PDF document or corrupted.

Figure 19. Copy Bundle Resources

Figure 19. Copy Bundle Resources

That’s it for now. We have already completed this tutorial for building an iOS PDF reader.