Getting Started with Cross-Platform PDF Processing Using Xamarin.Android 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.Android app using PDFNet SDK. In this tutorial, you will create a simple PDF viewing and annotating app. You will also learn about an Android Java Bindings Library Project that allows you to customize our Tools library.

Note the completed sample project described in Part 2-4 is available by request from here.

The tutorial is divided into 5 parts:

Part 1: Things you should know about the library distribution
Part 2: Showing a PDF
Part 3: Adding support for Annotations, Text Selection and Form Filling
Part 4: Create customized Tools.dll from the open source Tools library
Part 5: Next steps

Part 1: Things you should know about the library distribution

  • Full version vs. Standard version: The PDFNet for Xamarin.Android library is available in two different versions, the Standard version and the Full version. The Standard library offers the same viewing, annotation, and editing capabilities of the Full version, however it is much smaller. The main differences are:
    • The Full version has a built-in digital signature handler, which can be used by calling PDFDoc.AddStdSignatureHandler(); the Standard version does not have built-in signature handler. However, even with the Standard version, you will still be able to use the DigitalSignature tool because the pre-built Tools library includes Spongy Castle.
    • The Standard version does not support converting PDF pages to TIFF and PNG formats (i.e. PDFDraw will not work when using these formats.)
    • A number of features will only be available using the Full version due to their complexity. For example, universal document conversion (i.e. convert .docx, .doc, and .pptx to .pdf), reflow, and document preview cache generation. The detailed class list can be found in Release notes.
  • Best for app development vs. Best for app deployment: To provide a user-friendly framework for both development and deployment, PDFNet provides two options for using the PDFNetAndroid.dll. (To learn more about Android CPU architecture, please refer to this article.)
    • Best for app development: a single PDFNetAndroid.dll that contains arm64-v8a, armeabi-v7a, x86, and x86_64 architectures that can be used to create a single APK that works on all armeabi-v7a, arm64-v8a, x86 or x86_64 devices/emulators running Android 2.2 or greater. This .dll is located in Lib/Full and Lib/Standard.
    • Best for app deployment: five separate PDFNetAndroid.dlls for each architecture (arm64-v8a, armeabi, armeabi-v7a, x86, x86_64) that can be used to build five separate APKs. This allows an absolute minimum APK download size, and allows you to create APKs that work on armeabi, armeabi-v7a, arm64-v8a, x86 and x86_64 devices/emulators running Android 2.2 or greater. These .dlls are found in the /arm64-v8a, /armeabi, /armeabi-v7a, /x86 and /x86_64 folders respectively. (These five folders are themselves located in /Lib/Full and /Lib/Standard.)
  • Minumum Android API required: PDFNetAndroid.dll requires minimum API 11 (HONEYCOMB). Tools.dll requires minimum API 16 (JELLY_BEAN).

Part 2: Showing a PDF

  • Create a new app

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

  • Add required dependency, resource and a PDF file to the project

Add the PDFNetAndroid.dll to the References list. Add a sample file “sample.pdf” to the Resources/raw  folder of your application. Set the Build Action of the sample file to AndroidResource. Find pdfnet.res file in resource/android folder and add this file to the Resources/raw folder of your application. Set the Build Action of the res file to AndroidResource.

  • Add code to show a PDF

Change MainActivity.cs to the following.

using System;
using System.IO;
using System.Collections;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

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

using Android.Support.V4.App;

using com.xamarin.recipes.filepicker;
using pdftron.Common;

namespace PDFNetAndroidXamarinSample
{
[Activity(Label = "@string/app_name", MainLauncher = true, Icon = "@drawable/pdf_icon", HardwareAccelerated = true,
ConfigurationChanges = Android.Content.PM.ConfigChanges.ScreenSize | Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.KeyboardHidden,
WindowSoftInputMode = SoftInput.AdjustPan,
Theme = "@style/AppTheme")]
public class MainActivity : FragmentActivity
{
private pdftron.PDF.PDFViewCtrl mPdfViewCtrl;

protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);

// Set our view from the "main" layout resource
try
{
PDFNet.Initialize(this, Resource.Raw.pdfnet); // No license key, will produce water-marks
//PDFNet.Initialize(this, Resource.Raw.pdfnet, "your license key"); // Full version mode
// Disk caching should be disabled if write-external-storage is not permitted.
// To add the permission, add
// to the manifest file.
Console.WriteLine(PDFNet.GetVersion());
}
catch (PDFNetException e)
{
Console.WriteLine(e.GetMessage());
return;
}

SetContentView(Resource.Layout.Main);
mPdfViewCtrl = FindViewById(Resource.Id.pdfviewctrl);
mPdfViewCtrl.PagePresentationMode = PDFViewCtrl.PagePresentationModes.e_single_continuous;

// Load file from resource
Stream fis = this.Resources.OpenRawResource(Resource.Raw.sample);
PDFDoc docToOpen = new PDFDoc(fis);
mPdfViewCtrl.Doc = docToOpen;
}

protected override void OnPause()
{
base.OnPause();
if (mPdfViewCtrl != null)
{
mPdfViewCtrl.Pause();
}
}

protected override void OnResume()
{
base.OnResume();
if (mPdfViewCtrl != null)
{
mPdfViewCtrl.Resume();
}
}

protected override void OnDestroy()
{
base.OnDestroy();

if (mPdfViewCtrl != null)
{
mPdfViewCtrl.Destroy();
mPdfViewCtrl = null;
}
}

public override void OnLowMemory()
{
base.OnLowMemory();
if (mPdfViewCtrl != null)
{
mPdfViewCtrl.PurgeMemory();
}
}
}

}
  • Run the app

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

screenshot_1480980383

Part 3: 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 OnCreate method in MainActivity.cs
mToolManager = new ToolManager(mPdfViewCtrl);
mPdfViewCtrl.ToolManager = mToolManager;

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.

screenshot_1480980686

Part 4: 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 Java Tools project and then prepare the Android Archive file for building Tools.dll in Xamarin. This section demonstrates how to create Tools.dll from PDFViewCtrlTools.aar.

Please note that the following section will require some understanding on what an Android Archive 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 on Android Archive file. And please refer to this article for more information on Xamarin binding project.

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

In addition, Xamarin.Android is becoming more and more strict on binding rules, users will no longer be able to create customized Tools.dll with only the Tools project. PDFNetAndroid project will also be required. This is because PDFNetAndroid project uses PDFNet.jar for internal binding but this PDFNet.jar needs to match the PDFNet.jar inside the PDFViewCtrlTools.aar. PDFNetAndroid binding project is available by request from here.

  • In the requested PDFNetAndroid package, browse to /lib/src/PDFViewCtrlTools. Import the project to Android Studio and make the desired changes. Compile an Android Archive file PDFViewCtrlTools.aar.
  • Unzip PDFViewCtrlTools.aar and find PDFNet.jar file located in /libs folder of the unzipped PDFViewCtrlTools folder. This PDFNet.jar will be the EmbeddedJar used for PDFNetAndroid project.
  • In /lib/android/Native folder, add the PDFNet.jar file obtained from previous step. In the same directory, remove the PDFViewCtrlTools.aar file and replace it with the PDFViewCtrlTools.aar file obtained from first step.
  • Browse to /projectSrc/PDFNetAndroidXamarin. Open the PDFNetAndroidXamarin.sln provided in Xamarin Studio or Visual Studio. Check the Build Action for PDFNet.jar file, and make sure it is set to EmbeddedJar. Check the Build Action for PDFViewCtrlTools.aar file, and make sure it is set to LibraryProjectZip.
  • Clean and build PDFNetAndroid solution. A new PDFNetAndroid.dll and Tools.dll will be created in /lib/android/ folder. Use this new PDFNetAndroid.dll and Tools.dll in your application. Alternatively, you can also include the PDFNetAndroid project, FloatingActionButton project and PDFViewCtrlTools project in your application and make the binding project a project reference in your application project.

Part 5: Next steps

This concludes our introductory PDFNet for Xamarin.Android 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.

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.

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

Continue reading

Semantic Content Recognition in PDF

Semantic content recognition is the ability to identify components of a document by their “class” – that is if any particular content constitutes a title, subtitle, section, paragraph, word, figure, caption, table, etc. This is a problem, that despite decades of research, remains open. Available solutions are unreliable and are far, far behind the ability of a human being.

At the 2015 PDF Technical Conference, PDFTron’s CTO gave a presentation addressing the problem of semantic content recognition in PDF. The presentation gives an overview of the problem itself, why it has been such a hard problem to solve, and how the industry as a whole might organize itself to finally develop solutions that perform with the same accuracy as a person.

car-ad

pdf.js: Interesting Project, Incorrect Rendering

pdf.js is a well known project for rendering PDF documents directly in the browser. In that sense, it is similar to our recently announced PDFNetJS. While pdf.js is interesting project, and may be a reasonable choice in some very specific situations, it has a number of serious problems that make it unreliable for any situation where PDF rendering is important.

Continue reading

Introducing PDFNetJS: A Complete Browser-Side PDF Viewer and Editor

PDFNetJS

The WEB is taking over (obviously)

On desktop computers, web apps continue to replace activities that were previously fulfilled by Windows/Mac/Linux programs. The advantages are many: web apps are immediately available on every connected computer; the user doesn’t need to download and install something; they instantly update and they’re cross-platform. That they naturally lend themselves to a subscription model is yet another reason that companies are choosing to develop web apps in favor of a traditional desktop program.

However, web apps have historically had a number of shortcomings. An inability to deal with local files (without long uploads). Multimedia required securitychallenged plugins. And they couldn’t display PDF files. Continue reading

PDFTron at the PDF Technical Conference 2015

PDFTron is pleased to announce that we are a sponsor and presenter at the upcoming PDF Technical Conference 2015, held October 19-20 in San Jose, California. Aimed at software developers and technical product managers encountering PDF technologies in their work, the event will consist of educational and sponsored sessions presented by experts in the PDF field.

Continue reading