Tag Archives: iOS

Getting Started with Cross-Platform PDF Processing Using Xamarin.iOS and PDFNet SDK

Introduction

This tutorial shows the minimum steps needed to add a PDF viewing and annotating component to a Xamarin.iOS app using PDFNet SDK. In this tutorial, you will create a simple PDF viewing and annotating app. You will also create an iOS Objective-C Bindings Library Project that allows you to customize our Tools library.

Note that the completed sample project described in Part 1 and Part 2 can be found in the samples folder located inside the PDFNet component package. The completed sample project described in Part 1-3 is available by request from here.

The tutorial is divided into 5 parts:

To complete this tutorial, you will need to install the latest version of XCode, which is required because its compilers are used by Xamarin. If you are using an out of date version of XCode you may encounter linker errors. You will also need to install and setup Xamarin.iOS as described in this article.

Continue reading

Streaming a PDF From the Web to a Mobile or Desktop App

Wouldn’t it be nice to be able to view a remote PDF the same way as one can view an online video? By this we mean you can see the beginning of the content almost immediately, and if you move to the middle of the content, it is prioritized and loaded very quickly, before other parts.

Unfortunately, with remotely stored PDFs, this is not how things usually work. What typically happens is that the entire file must be downloaded before it can be opened and viewed. This is the case for two reasons:

  1. PDF documents are not typically linearized (or in Adobe lingo “fast web view”ed). This means that the contents of page twenty, for example, can actually be located in many different places within the file, with no way of being able to quickly determine where the different pieces are. Without this information, the entire document must be downloaded before page two can be displayed.
  2. Even if a document is linearized, most viewers are not equipped to show partial content. They are designed to work on complete documents and will reject partial documents as corrupted.

There is, however, a better way.

If you’re building a website or HTML5 app, see this post for how to display a PDF in HTML. If you’re building a native program for desktops (Windows, Mac, Linux) or mobile (iOS, Android, WinRT, Windows Phone 8), then read on…

Continue reading

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.