Foxit PDF SDK for Mac

How to load & save documents using Foxit PDF SDK (Objective-C)

Foxit PDF SDK provides a series of options on how to handle documents. A PDF document object can be constructed with an existing PDF file from a file path, memory buffer, a custom implemented ReaderCallback object and an input file stream. The methods PDFDoc.Load or PDFDoc.StartLoad are used to load document content. Document objects inside a PDF are used for document level operations such as opening and closing files, getting pages, annotation, metadata and more. This article will describe how to perform these and other operations using Foxit PDF SDK.

Example:

How to create a PDF document from scratch
#include "FSPDFObjC.h"
...
FSPDFDoc* doc = [[FSPDFDoc alloc] init];

Note: It creates a new PDF document without any pages.

How to load an existing PDF document from file path
#include "FSPDFObjC.h"
...
NSString* pdfpath = [[NSBundle mainBundle] pathForResource:@"Sample" ofType:@"pdf"];
FSPDFDoc* doc = [[FSPDFDoc alloc] initWithPath:pdfpath];
FSErrorCode errorCode = [doc load:nil];
if (errorCode != FSErrSuccess) {
    return -1;
}
How to load an existing PDF document from a memory buffer
#include "FSPDFObjC.h"
...
NSData* file_data = [NSData dataWithContentsOfFile:pdf_path];
FSPDFDoc* doc = [[FSPDFDoc alloc] initWithBuffer:file_data];
FSErrorCode errorCode = [doc load:nil];
if (errorCode != FSErrSuccess) {
    return -1;
}
How to load an existing PDF document from a file read callback object
#include "FSPDFObjC.h"
...
@interface FSFileRead : NSObject
- (id)initWithSourceFilePath: (NSString *)path offset: (long long)offset;
@end
@implementation FSFileRead {
    FileReader *imp;
}
- (id)initWithSourceFilePath: (NSString *)path offset: (long long)offset {
    if (self = [super init]) {
        imp = new FileReader(offset);
        if (!imp->LoadFile([path UTF8String])) {
            return nil;
        }
    }
    return self;
}
- (void)dealloc {
    delete imp;
}
- (unsigned long long)getSize {
    return imp->GetSize();
}
- (NSData *)readBlock: (unsigned long long)offset size: (unsigned long long)size {
    void *buffer = malloc(size);
    if (!buffer) {
        NSLog(@"failed to malloc buffer of size %llu", size);
        return nil;
    }
    if (imp->ReadBlock(buffer, offset, (size_t)size)) {
        return [NSData dataWithBytesNoCopy:buffer length:size];
    } else {
        free(buffer);
        return nil;
    }
}
@end
FSFileRead *file_reader = [[FSFileRead alloc] initWithSourceFilePath:file_name offset:offset];
FSPDFDoc *doc_real = [[FSPDFDoc alloc] initWithFile_read:file_reader is_async:NO];
FSErrorCode code = [doc_real load:nil];
if (code != FSErrSuccess) {
    return -1;
}
How to load PDF document and get the first page of the PDF document
#include "FSPDFObjC.h"
...
NSString* pdfpath = [[NSBundle mainBundle] pathForResource:@"Sample" ofType:@"pdf"];
FSPDFDoc* doc = [[FSPDFDoc alloc] initWithPath:pdfpath];
FSErrorCode errorCode = [doc load:nil];
if (errorCode != FSErrSuccess) {
    return -1;
}
// Get the first page of the document.
FSPDFPage* page = [doc getPage:0];
// Parse page.
[page startParse:FSPDFPageParsePageNormal pause:nil is_reparse:NO];
How to save a PDF to a file
#include "FSPDFObjC.h"
...
NSString* pdfpath = [[NSBundle mainBundle] pathForResource:@"Sample" ofType:@"pdf"];
FSPDFDoc* doc = [[FSPDFDoc alloc] initWithPath:pdfpath];
FSErrorCode errorCode = [doc load:nil];
if (errorCode != FSErrSuccess) {
return -1;
}
[doc saveAs:output_file save_flags:FSPDFDocSaveFlagNoOriginal];

Updated on April 1, 2019

Was this article helpful?
Thanks for your feedback. If you have a comment on how to improve the article, you can write it here: