Getting Started with the iOS Static Framework

NOTE: This is the getting started document for static framework of PDFNet for iOS. We strongly recommend using the dynamic framework.

Introduction

This tutorial shows the minimum steps needed to open a PDF using PDFNet. It also shows how to add support for annotations, and opening encrypted documents. This document assumes this use of PDFNet V 6.5.3 or greater. Note that 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”, which implements the features contained in this sample plus more.)

Part 1: Showing a PDFCreate a new project.

  1. Open Xcode 7 GM 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”.) The project’s default settings need to be adjusted so that the project compiles properly with PDFNet’s internal C++ code. To make these changes, click on the PTTest project in the navigation pane. Click on the Build Settings tab. The required changes are:

    • Under Apple LLVM [version] – Language: Ensure that the C++ standard library is libc++. This is the default for new Xcode projects.
    • In Build Options, set Enable Bitcode to No. (A bitcode version of the library can be made available to licensed customers.)
    • Again in the project’s settings, but under the Build Phases tab, expand the Link Binary With Libraries section. In addition to the standard frameworks CoreGraphics, UIKit and Foundation, add CoreText, QuartzCore, AVFoundation and MediaPlayer.
  3. Add the required PDFNet files, and a PDF Document.

    After downloading the SDK, you will need to add the following files to the project. They are all found in the Lib/ folder:

    • PDFNet.framework – The framework that includes the PDFNet static library and associated header files.
    • NSObjectInitWithCptr.{h,m} – Adds a needed category to NSObject.
    • pdfnet.res
    • pdftron_layout_resources.plugin
    • pdftron_smart_substitution.plugin

    Now, make sure that pdfnet.res will be copied into the app bundle. In Xcode, click on the project file, then making sure that it is the app under TARGETS is selected, click the build phases tab, and under the Copy Bundle Resources section, use the ‘+’ button to add pdfnet.res. Next, you will need to add a PDF file. For the purposes of this tutorial, we will use “mech.pdf”, which is included in the TestFiles folder. Add it now by dragging to XCode. You may use any PDF you wish, 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 two additional #import statements, and the code in the viewDidLoad selector.

#import "ViewController.h"
#import <PDFNet/PDFNetOBJC.h>
#import <PDFNet/PDFViewCtrl.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] initWithFrame:[self.view bounds]];

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

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

}

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

@end
  • 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.

// set the background of the view to light gray
[self.view setBackgroundColor:[UIColor lightGrayColor]];

// set the background of PDFViewCtrl to transparent (alpha is zero.)
[pdfViewCtrl SetBackgroundColor:255 g:255 b:255 a:0];

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 using PDFNet’s Objective-C API, and are included as a  project that builds the static library libTools.a. 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 libTools.a and tools-strings.bundle to your project, both found in the Lib/ folder.
  2. Add the image file “PopupIcon.png” found in Lib/src/PDFViewCtrlTools/Tools/ to your project
  3. Add Tool.h, PanTool.h and ToolManager.h files found in Lib/src/PDFViewCtrlTools/Tools/
  4. Add #import “Tool.h” and #import “PanTool.h” and #import “ToolManager.h” at the top of ViewController.m
  5. 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, all 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.

6 thoughts on “Getting Started with the iOS Static Framework

  1. Anna Fortuna

    Hi,

    I encountered this error when I added the code to open the PDF.

    ‘_TRN_ActionCreateGoto’ in /Users/Dhash/PDFNet_iOS/Lib/libPDFNet.a(all-obfuscated.o) contains undefined reference for architecture i386

    What could possibly be the cause of this error?

    Reply
  2. xodo Post author

    Hello Anna,

    This error can occur if the standard C++ library is not linked in. Please make sure your project has at least one .mm or .cpp file, and that you are linking with libc++, as described in Part 1 Step 2.

    Reply
  3. xboehm

    After i called [PDFNet Initialize:@””]
    I getting this error message:

    Undefined symbols for architecture armv7:
    “std::__1::basic_streambuf<char, std::__1::char_traits >::seekoff(long long, std::__1::ios_base::seekdir, unsigned int)”, referenced from:
    l35038 in PDFNet(all-obfuscated.o)
    “std::__1::basic_streambuf<wchar_t, std::__1::char_traits >::showmanyc()”, referenced from:
    l34971 in PDFNet(all-obfuscated.o)
    “std::__1::basic_streambuf<wchar_t, std::__1::char_traits >::xsgetn(wchar_t*, int)”, referenced from:
    l34971 in PDFNet(all-obfuscated.o)
    “std::__1::basic_streambuf<wchar_t, std::__1::char_traits >::pbackfail(int)”, referenced from:
    l34971 in PDFNet(all-obfuscated.o)
    “std::__1::basic_streambuf<wchar_t, std::__1::char_traits >::xsputn(wchar_t const*, int)”, referenced from:
    l34971 in PDFNet(all-obfuscated.o)
    “std::__1::basic_streambuf<wchar_t, std::__1::char_traits >::overflow(int)”, referenced from:
    l34971 in PDFNet(all-obfuscated.o)
    “std::__1::basic_streambuf<char, std::__1::char_traits >::pbackfail(int)”, referenced from:
    l34883 in PDFNet(all-obfuscated.o)
    l35038 in PDFNet(all-obfuscated.o)
    “std::__1::basic_streambuf<char, std::__1::char_traits >::overflow(int)”, referenced from:
    l34883 in PDFNet(all-obfuscated.o)
    “std::__1::basic_streambuf<char, std::__1::char_traits >::imbue(std::__1::locale const&)”, referenced from:……………………………….. and so on ……….

    can someone help me, i think it is a settings problem but i can’t find it

    Reply
    1. xodo Post author

      Is it possible you missed on the steps in Part 1 point 2? The problem is that the project is set to use the GNU implementation of the standard library (stdc++), and PDFNet is expecting the LLVM implementation of the standard library (libc++). To fix this problem, please be sure of two things:
      In your project’s settings, that the standard library is set to libc++.
      If you are deploying for iOS 7+, then add at least one .mm or .cpp to your project.

      For more help please see our support forum here: https://groups.google.com/forum/?fromgroups#!forum/pdfnet-sdk

      Reply

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