Adding the ability to view PDF files to your iOS app is incredibly easy when using Foxit Mobile PDF SDK. This tutorial will show you how with just 10 lines of code and 3 easy steps.

A few quick notes before we begin:

  • If you don’t have Foxit Mobile PDF SDK, you can get a free trial here.
  • This tutorial is created using Xcode version 8.1, and will work with any Xcode version coming after 7.0.
  • This will teach you how to implement basic PDF viewing functionality.
  • We will provide full sample code at the end of the tutorial.

Let’s begin:

Step 1: Create a New Project

File -> New -> Project…, then select  iOS -> Application -> Single View Application

 

image2016-12-13 15-48-37

image2016-12-13 15-49-22

 

Then select a directory to save your project. You should now see the following:

 

image2016-12-13 15-55-18

 

Step 2: Import Foxit Mobile PDF SDK

Right click the project, and select Add Files to [“project name here”] as shown in the screenshot below:

 

image2016-12-13 15-57-20

 

Select SDK-libs- FoxitRDK.framework, then click Add

 

image2016-12-13 16-3-25

 

Now Foxit Mobile PDF library has been imported, as seen below:

 

image2016-12-13 16-3-58

 

Now go to General->[your project name]->Embedded Binaries, and click the “+” to add FoxitRDK.framework

 

image2016-12-13 16-6-24

 

Here is how it looks like after adding FoxitRDK.framework

 

image2016-12-13 16-7-9

 

Now we will add a PDF file (this tutorial uses a pdf called Samples.pdf, but you can use any file you’d like)

Select your project, then select the PDF file, then click the “added” button. If you see the below screen, you’re done importing Foxit Mobile PDF SDK.

 

image2016-12-13 16-10-48

Step 3: Write a Few Lines of Code

In order to display the PDF file you’ve imported you need to write a few lines of code in ViewController.mm

First, import the SDK header files:

#import <FoxitRDK/FSPDFObjC.h>
#import <FoxitRDK/FSPDFViewControl.h>

Then Initiate Foxit Mobile PDF SDK:

NSString* sn = @"***";
NSString* unlock = @"***";
[FSLibrary init:sn key:unlock];

Now you’ll want to load the document:

//load doc
NSString* docPath= [[NSBundle mainBundle] pathForResource:@"getting_started_ios" ofType:@"pdf"];
FSPDFDoc* doc = [FSPDFDoc createFromFilePath:docPath];
[doc load:nil];

After that’s added, you’ll need to show the PDF with Viewer Control:

FSPDFViewCtrl* myTestViewCtrl;
myTestViewCtrl = [[FSPDFViewCtrl alloc] initWithFrame:[self.view bounds]];
[myTestViewCtrl setDoc:doc];
[self.view addSubview:myTestViewCtrl];

Now, you’ll be able to see your PDF file when running the demo!

 

image2016-12-13 16-24-46

 

Here is the entire code for “ViewController.mm”:

#import "ViewController.h"
#import <FoxitRDK/FSPDFObjC.h>
#import <FoxitRDK/FSPDFViewControl.h>
#import "UIExtensionsManager.h"

@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.
NSString* sn = @"****";
NSString* unlock = @"****";
enum FS_ERRORCODE eRet = [FSLibrary init:sn key:unlock];
if (e_errSuccess != eRet) {
return;
}

//load doc
NSString* docPath= [[NSBundle mainBundle] pathForResource:@"Sample" ofType:@"pdf"];
FSPDFDoc* doc = [FSPDFDoc createFromFilePath:docPath];
if (e_errSuccess!=[doc load:nil]) {
return;
}

//init PDFViewerCtrl
FSPDFViewCtrl* myTestViewCtrl;
myTestViewCtrl = [[FSPDFViewCtrl alloc] initWithFrame:[self.view bounds]];
[myTestViewCtrl setDoc:doc];
[self.view addSubview:myTestViewCtrl];
}

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

@end