Build a basic PDF reader (PDFViewCtrl)
This guide shows how to build a basic PDF reader with FSPDFViewCtrl (display, zoom, page navigation) without UI Extensions toolbars or feature modules.
Prerequisites
Complete Integrate Foxit PDF SDK first (project setup, SDK integration, license initialization).
Step 1: Open and display a PDF with FSPDFViewCtrl
In ViewController, create FSPDFViewCtrl and call openDoc:password:completion:.
Objective-C
objc
#import <FoxitRDK/FSPDFViewControl.h>
#import <FoxitRDK/FSPDFObjC.h>
@interface ViewController ()
@property (nonatomic, strong) FSPDFViewCtrl *pdfViewCtrl;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Initialize FSPDFViewCtrl
self.pdfViewCtrl = [[FSPDFViewCtrl alloc] initWithFrame:self.view.bounds];
[self.view addSubview:self.pdfViewCtrl];
// Open PDF
NSString *path = [[NSBundle mainBundle] pathForResource:@"Sample" ofType:@"pdf"];
[self.pdfViewCtrl openDoc:path password:nil completion:^(FSErrorCode error) {
if (error != FSErrSuccess) {
NSLog(@"Failed to open document, error code: %d", (int)error);
}
}];
}
@endSwift
swift
import FoxitRDK
class ViewController: UIViewController {
var pdfViewCtrl: FSPDFViewCtrl!
override func viewDidLoad() {
super.viewDidLoad()
// Initialize FSPDFViewCtrl
pdfViewCtrl = FSPDFViewCtrl(frame: view.bounds)
view.addSubview(pdfViewCtrl)
// Open PDF
if let path = Bundle.main.path(forResource: "Sample", ofType: "pdf") {
pdfViewCtrl.openDoc(path, password: nil) { error in
if error != .errSuccess {
print("Failed to open document, error code: \(error.rawValue)")
}
}
}
}
}INFO
The sample loads from the app bundle. For Documents or network loading, see Open document.
Step 2: Listen for document events (optional)
Implement IDocEventListener to handle open, close, save, and related events:
objc
// Register document event listener
[self.pdfViewCtrl registerDocEventListener:self];
// IDocEventListener
- (void)onDocOpened:(FSPDFDoc *)document error:(int)error {
if (error == FSErrSuccess) {
NSLog(@"Document opened, page count: %d", [self.pdfViewCtrl getPageCount]);
}
}
- (void)onDocClosed:(FSPDFDoc *)document error:(int)error {
NSLog(@"Document closed");
}