How to Show a PDF with Your iOS App Using Foxit Mobile PDF SDK
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:
Contents
Step 1: Create a New Project
File -> New -> Project…, then select iOS -> Application -> Single View Application
Then select a directory to save your project. You should now see the following:
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:
Select SDK-libs- FoxitRDK.framework, then click Add
Now Foxit Mobile PDF library has been imported, as seen below:
Now go to General->[your project name]->Embedded Binaries, and click the “+” to add FoxitRDK.framework
Here is how it looks like after adding FoxitRDK.framework
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.
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!
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