Back to Blog

    Developer Blog

    How do I open a PDF document from a specified PDF file path using PDF SDK for iOS?

    Foxit PDF SDK for iOS

    Foxit PDF SDK for Android provides multiple interfaces to open a PDF document. You can open a PDF document from a specified PDF file path, or from a memory buffer. For from a specified PDF file path, there are two ways to do that.

    The first one is that just use the openDoc interface, which includes the operations of creating a PDF document object (initWithPath), loading the document content (load), and setting the PDF document object to view control (setDoc). Following is the sample code:

    Note: The openDoc interface is only available for opening a PDF document from a file path. If you want to customize to load a PDF document, you can implement it in the callback function (FSFileReadCallback), and then create a document object with a FireRead instance using initWithHandler. Next, also load the document content using load, and set the PDF document object to view control using setDoc.

    #import "ViewController.h"
    #import <foxitrdk /FSPDFViewControl.h>
    
    @interface ViewController : UIViewController
    @end
    @implementation ViewController
    { 
        FSPDFViewCtrl* pdfViewCtrl;
    }
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        // Get the path of a PDF.
        NSString* pdfPath = [[NSBundle mainBundle] pathForResource:@"Sample" ofType:@"pdf"];
      
        // Initilize a FSPDFViewCtrl object with the size of the entire screen.
        pdfViewCtrl = [[FSPDFViewCtrl alloc] initWithFrame: [self.view bounds]];
        
        // Open an unencrypted PDF document from a specified PDF file path.
        [pdfViewCtrl openDoc:pdfPath password:nil completion:nil];
        
        // Add the pdfView to the root view.
        [self.view addSubview:pdfViewCtrl];    
    }
    @end
    </foxitrdk>
    

    The second one is that use the initWithPath interface to create a PDF document object, use load interface to load the document content, and then use setDoc to set the PDF document object to view control. Following is the sample code:

    #import "ViewController.h"
    #import <foxitrdk /FSPDFViewControl.h>
    
    @interface ViewController : UIViewController
    @end
    @implementation ViewController
    {
    FSPDFViewCtrl* pdfViewCtrl;
    }
    - (void)viewDidLoad {
    [super viewDidLoad];
    
    // Get the path of a PDF.
    NSString* pdfPath = [[NSBundle mainBundle] pathForResource:@"Sample" ofType:@"pdf"];
    
    // Initialize a PDFDoc object with the path to the PDF file.
    FSPDFDoc* pdfdoc = [[FSPDFDoc alloc] initWithPath: pdfPath];
    
    // Load the unencrypted document content.
    if(FSErrSuccess != [pdfdoc load:nil]) {
    return;
    }
    
    // Initilize a FSPDFViewCtrl object with the size of the entire screen.
    pdfViewCtrl = [[FSPDFViewCtrl alloc] initWithFrame: [self.view bounds]];
    
    // Set the document to view control.
    [pdfViewCtrl setDoc:pdfdoc];
    
    // Add the pdfView to the root view.
    [self.view addSubview:pdfViewCtrl]; 
    }
    @end
    </foxitrdk>