Getting Started with PDFNet for iOS

This Getting Started document is for users of the PDFNet dynamic framework, version 6.7 and greater. For users of the static library, or earlier version of PDFNet, please refer to getting started with the static framework.

Introduction

This short tutorial will guide you through creating an app that can show and annotate a PDF. A completed project can be found on our GitHub repository. You should use the latest versions of the PDFNet Framework and the tools source code, available by request on our website. The tutorial is divided into four parts:

  • Part 1: Showing a PDF.
  • Part 2: Adding support for text selection, annotation creation and editing, link following and form filling.
  • Part 3: Adding support for encrypted PDF documents.
  • Part 4: Next Steps

(Note that PDFNet for iOS includes the sample project “PDFViewCtrl” and “Complete Reader”, which implement the features contained in this sample plus more.)

Part 1: Showing a PDF

1. Create a new app

Open Xcode 8 or greater and create a new iOS Project, choosing “Single View Application” from the list of available templates. After clicking “next”, name the project PTTest. Save the new project at the location of your choosing.

2. Prepare the project

The first thing is to change the name of AppDelegate.m to AppDelegate.mm. This changes the file to an Objective-C++ file, and will ensure that the C++ standard library that is required by PDFNet is included at link time. (The project should have at least one .mm or .cpp file; which file is not important. Alternatively, open the project settings, select Build Phases, and under “Link Binary With Libraries”, add “libc++.tbd”.)

Secondly, in the project’s Build Options, set Enable Bitcode to No. (A bitcode version of the library can be made available to licensed customers.)

Lastly, in your target’s Build Phases panel, add a new “New Run Script Phase”, and add the following script:


Bash "$BUILT_PRODUCTS_DIR/$FRAMEWORKS_FOLDER_PATH/PDFNet.framework/strip-framework.sh

This will ensure the simulator slices are striped from the framework before being submitted to the app store.

3. Add the framework and a PDF to the project

Add the framework: In your project’s settings general tab, scroll to the “Embedded Binaries” section, click the ‘+’, and select PDFNet.framework from your filesystem.

Add a PDF: For the purposes of this tutorial, we will use “mech.pdf”, which is included in the TestFiles folder. Add it by dragging it to your project’s other files in Xcode. You can use any PDF you want, just replace “mech” with the name of your PDF in the code snippets that follow.

4. Add code to show a PDF

Change ViewController.m to the following. The changes are the additional #import statement, and the code in the viewDidLoad selector.

#import "ViewController.h"
#import <PDFNet/PDFNet.h>
#import <Tools/Tools.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
 [super viewDidLoad];
 // Do any additional setup after loading the view, typically from a nib.

 // Initilize PDFNet (in demo mode - pages will be watermarked)
 [PTPDFNet Initialize:@""];

 // Get the path to document in the app bundle.
 NSString* fullPath = [[NSBundle mainBundle] pathForResource:@"mech" ofType:@"pdf"];

 // Initialize a new PDFDoc with the path to the file
 PTPDFDoc* docToOpen = [[PTPDFDoc alloc] initWithFilepath:fullPath];

 // Create a new PDFViewCtrl that is the size of the entire screen
 PTPDFViewCtrl* pdfViewCtrl = [[PTPDFViewCtrl alloc] init];

 // Set the document to display
 [pdfViewCtrl SetDoc:docToOpen];

 // Add the PDFViewCtrl to the root view
 [self.view addSubview:pdfViewCtrl];

 // set size of PDFViewCtrl
 [pdfViewCtrl setTranslatesAutoresizingMaskIntoConstraints:NO];

 [NSLayoutConstraint activateConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[pdfViewCtrl]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(pdfViewCtrl)]];

 [NSLayoutConstraint activateConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[pdfViewCtrl]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(pdfViewCtrl)]];

}

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

@end

5. Run the app

You can now run the app. If you run in the simulator, you will see the following. Note that the PDF can be scrolled and zoomed.

iPad-Simulator

When zooming, you might notice that the gray area behind the pages does not match the white background of the root view. You can fix this by adding the following code at the end of the viewDidLoad selector.

	// Makes the background light gray
	[self.view setBackgroundColor:[UIColor lightGrayColor]];

	// sets the non-page content of the PDFViewCtrl to transparent
	[pdfViewCtrl SetBackgroundColor:255 g:0 b:0 a:128];

Part 2: Adding support for Annotations, Text Selection and Form Filling

PDFNet comes with built-in support for text selection, interactive annotation creation and editing, form filling and link following. These features have been implemented in an open source project using the PDFNet API, and are included as a  project that builds the dynamic library Tools.framework. Because the source is provided, implementers have complete flexibility and control to customize how users interact with the PDF so that it can fit their requirements exactly. To add support for annotations, text selection, etc:

  1. Add the framework: In your project’s settings general tab, scroll to the “Embedded Binaries” section, click the ‘+’, and select Tools.framework from your filesystem.
  2. Add #import <Tools/Tools.h> at the top of ViewController.m
  3. Add the following lines as the last lines of the viewDidLoad selector in ViewController.m
	// creates a new tool manager using the designated initializer
	ToolManager* toolManager = [[ToolManager alloc] initWithPDFViewCtrl:pdfViewCtrl];

	// registers the tool manager to receive events
	[pdfViewCtrl setToolDelegate:toolManager];

	// sets the initial tool
	[toolManager changeTool:[PanTool class]];

You are now ready to run the project again. Now, when you run the project, you can select text, follow links and create and edit annotation. To create a new annotation, long press on an area of the document to trigger a popup with annotation types to create. This example behavior is shown in the blow screenshot.

annotations

Part 3: Opening encrypted documents.

PDFNet supports opening encrypted PDF documents. To open an encrypted document, you need to do is initialize a PDFDoc’s security handler with the correct password. Add the following code snippet after creating the PDFDoc in order to display an encrypted PDF.

if( [docToOpen InitStdSecurityHandlerWithPassword:@"password-string" password_sz:0] == NO )
{
  NSLog("Password is incorrect");
  return;
}

Of course a “real” app would require that the password be obtained from the user, which is implemented in the sample viewer that is included with the PDFNet for iOS download.

Part 4: Next Steps

This concludes our introductory PDFNet for iOS Tutorial. The completed tutorial project can be downloaded from GitHub. For more help, please see the online documentation, sample code, and other tutorials.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s