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

This Getting Started document is for users of the PDFNet version 6.7 and greater. For users of earlier version of PDFNet, please refer to Getting Started (2014).

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 learn about 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-3 is available by request from here.

The tutorial is divided into 4 parts:

  • Part 1: Showing a PDF
  • Part 2: Adding support for Annotations, Text Selection and Form Filling
  • Part 3: Create customized Tools.dll from the open source Tools library
  • Part 4: Next steps

Part 1: Showing a PDF

  • Create a new app

Open Xamarin Studio or Visual Studio and create a new Xamarin.iOS project. If this is your first Xamarin.iOS project, check out the getting started with Xamarin.iOS guide here: https://developer.xamarin.com/guides/ios/getting_started/hello,_iOS/

  • Add required dependency and a PDF file to the project

Add the PDFNetiOS.dll to the References list. Add a sample file “sample.pdf” to the Resources list. Set the Build Action of the sample file to BundleResource.

  • Add code to show a PDF

Change ViewController.cs to the following.

using System;
using System.IO;
using CoreGraphics;
using Foundation;
using UIKit;
using CoreAnimation;
using ObjCRuntime;
using System.Collections.Generic;

using pdftron;
using pdftron.PDF;
using pdftron.PDF.Tools;
using pdftron.PDF.Controls;

namespace PDFNetiOSXamarinSample
{
public partial class PDFNetiOSXamarinSampleViewController : UIViewController
{

private PDFViewCtrl mPdfViewCtrl;
public PDFNetiOSXamarinSampleViewController () : base ("PDFNetiOSXamarinSampleViewController", null)
{
}

public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// Do any additional setup after loading the view, typically from a nib.

try
{
// Initilize PDFNet (in demo mode - pages will be watermarked)
PDFNet.Initialize();

Console.WriteLine("version:" + PDFNet.GetVersion());
}
catch (pdftron.Common.PDFNetException e)
{
Console.WriteLine(e.GetMessage());
return;
}

View.Frame = UIScreen.MainScreen.Bounds;

// Create a new PDFViewCtrl that is the size of the entire screen
CGRect viewRect = new CGRect(0, 0, View.Frame.Size.Width, View.Frame.Size.Height);
mPdfViewCtrl = new PDFViewCtrl(viewRect);
mPdfViewCtrl.PagePresentationMode = PDFViewCtrl.PagePresentationModes.e_single_continuous;
mPdfViewCtrl.TranslatesAutoresizingMaskIntoConstraints = false;
mPdfViewCtrl.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;

View.AddSubview(mPdfViewCtrl);

// Get the path to document in the app bundle.
string docPath = "sample.pdf";
// Initialize a new PDFDoc with the path to the file
PDFDoc docToOpen = new PDFDoc(docPath);

// Set initial document from disk
mPdfViewCtrl.Doc = docToOpen;
}

public override void DidReceiveMemoryWarning ()
{
base.DidReceiveMemoryWarning ();
// Release any cached data, images, etc that aren't in use.

mPdfViewCtrl.PurgeMemory();
}
}
}
  • 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.

simulator-screen-shot-dec-2-2016-4-13-33-pm

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 Tools.dll. 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:

  • Add the dependency

Add the Tools.dll to the References list.

  • Add the following lines as the last lines of the ViewDidLoad method in ViewController.cs
ToolManager toolManager = new ToolManager(mPdfViewCtrl);
mPdfViewCtrl.ToolManager = toolManager;
toolManager.ChangeTool(typeof(pdftronprivate.PanTool));

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 annotations. 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 below screenshot.

simulator-screen-shot-dec-2-2016-4-39-35-pm

Part 3: Create customized Tools.dll from the open source Tools library

In the previous section you have seen how to use PDFViewCtrl with the tools add-on. However, if you wish to customize the Tools behavior, you will need to do so using the open source Objective-C Tools project and then prepare the dynamic framework for building Tools.dll in Xamarin. This section demonstrates how to create Tools.dll from Tools.framework.

Please note that the following section will require some understanding on what a dynamic framework is and how to build one. As well as what a Xamarin binding project is and how to build one. Please refer to this article for more information.

You will need the PDFNetiOS package to proceed. It is available by request from here.

  • In the requested PDFNetiOS package, browse to /Lib/Tools/src/PDFViewCtrlTools. Open Tools.xcodeproj in Xcode and make the desired changes. Compile a fat dynamic framework Tools.framework.
  • Browse to /lib/ios/Native/Tools/binding. Open the provided binding project PDFViewCtrlTools.csproj in Xamarin Studio or Visual Studio. Remove the existing Tools reference under Native References. Click Add Native Reference and select the Tools.framework from the previous step.
  • Clean and build PDFViewCtrlTools project. A new Tools.dll will be created in /lib/ios/ folder. Use this new Tools.dll in your application. Alternatively, you can also include the PDFViewCtrlTools binding project in your application and make the binding project a project reference in your application project.

Part 4: Next steps

This concludes our introductory PDFNet for Xamarin.iOS Tutorial. The completed tutorial project is available by request from here. For more help, please see the sample code, and other tutorials. You can also browse our public forum for more information about PDFNet. For details related to technical support, please refer to PDFTron support page.

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