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 how to integrate the static version of the PDFNet framework. It also shows how to add support for annotating, and opening encrypted documents. This document assumes this use of PDFNet V7.3 or greater. You should use the latest versions of the PDFNet Framework and the tools source code, available by request on our website. The tutorial is made 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

Part 1: Showing a PDFCreate a new project.

  1. Create a new projectOpen Xcode 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 ProjectPDFNet relies on C++, and projects that do not include any .cpp or .mm files must specifically instruct the linker to include the C++ standard library:
    • In Xcode, selection the project file in the left hand panel, then under the Build Phases tab, “Link binaries With Libraries section”, add libc++.tbd.
  3. Add the required PDFNet files, and a PDF Document.Add PDFNet.framework to the proejct:
    • Click on the project file, then making sure that it is the app under TARGETS that is selected, click the General tab, and under Linked Frameworks and Libraries, click the ‘+’ icon, and then click the “Add Other…” button, to add PDFNet.framework.

    Ensure that the framework can be located:

    • Under the Build Settings tab, in the Search Paths section, locate the Framework Search Paths entry. (You may need to ensure “All” entries are visible, not just the “Basic” ones.) Add the path to the location of PDFNet.framework. (If the path includes spaces, enclose the path in quotes.)

    Add the following supporting files to the project:

    • NSObjectInitWithCptr.{h,m} – Adds a needed category to NSObject.
    • ResourceFiles/pdfnet.res – Resources used by PDFNet
    • ResourceFiles/pdftron_layout_resources.plugin – Resources used by PDFNet
    • ResourceFiles/pdftron_smart_substitution.plugin – Resources used by PDFNet

    Make sure that pdfnet.res, pdftron_layout_resources.plugin and pdftron_smart_substitution.plugin will be copied into the app bundle:

    • Click on the project file, then making sure that it is the app under TARGETS that is selected, click the build phases tab, and under the Copy Bundle Resources section, use the ‘+’ button to add the three files.

    Lastly, add a PDF file:

    • In the TestFiles folder, locate “mech.pdf”, and add it 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.
    • Add the PDF file to the Copy Bundle Resources section (as was done for pdfnet.res, etc.).
  4. Add code to show a PDF.Change ViewController.m to include the following.
    #import <PDFNet/PDFNet.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]];
    
      // Makes the background light gray
      [self.view setBackgroundColor:[UIColor lightGrayColor]];
    
      // 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
  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.

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 that uses PDFNet’s API. 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. you must include the Tools library. As the pre-compiled version of the tools library expects the dynamic PDFNet framework, you must compile a version that does not rely on it.

  1. Open the Tools project, found in Lib/Tools/src/PDFViewCtrlTools/, and change the Scheme to Static - Release.
  2. Compile the project for the simulator or device (whichever you are using).
  3. Add the project’s output, libTools.a, plus tools-strings.bundle which is found at Lib/Tools/src/PDFViewCtrlTools/, to your project.
  4. Add Tool.h, PanTool.h and ToolManager.h files found in Lib/Tools/src/PDFViewCtrlTools/Tools/ to your project.
  5. In the target’s Build Phases, under “Link Binary With Libraries”, add libTools.a, AVKit.framework and AVFoundation.framework.
  6. In the target’s Build Phases, under “Copy Bundle Resources”, add tools-strings.bundle.
  7. Under the Build Settings tab, locate the Library Search Paths entry in the Search Paths section. (You may need to ensure “All” entries are visible, not just the “Basic” ones.) Add the path to the location of libTools.a. (If the path includes spaces, enclose the path in quotes.)
  8. Add #import “Tool.h” and #import “PanTool.h” and #import “ToolManager.h” at the top of ViewController.m
  9. 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.

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. 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