Foxit PDF SDK Objective-C Library
This section describes how to run samples and create a basic project using the Foxit PDF SDK Objective-C library. The project demonstrates rendering the first page of a PDF document to a bitmap and saving it as a JPG image.
Prerequisites
Development environment
- Xcode
- Foxit PDF SDK Objective-C library
System support
We provide detailed system support information for macOS, including operating system versions and compiler requirements. Select your platform to view the details.
Run samples
Run samples from the command line
See Samples for an overview of the sample projects, dependency information for specific samples, and instructions for running samples from the command line.
Quick start
Create a project directory:
- Create a folder named
test_ocas the project directory.
- Create a folder named
Copy SDK files:
- Mac x64: Copy the
includeandlibfolders from thefoxitpdfsdk_*_mac_ocSDK package into thetest_ocdirectory. - Mac arm64: Copy the
includeandlibfolders from thefoxitpdfsdk_*_mac_arm64_ocSDK package into thetest_ocdirectory.
- Mac x64: Copy the
Create an Objective-C class file:
- In the
test_ocfolder, create an Objective-C class file namedtest_oc.mm.
- In the
Add header files:
- Open the
test_oc.mmfile and add the following header declaration at the top of the file:
Objc#include "FSPDFObjC.h"- Open the
Initialize the SDK library:
- Before calling any API, initialize the Foxit PDF SDK library with a license key. See Initialize the SDK Library
Load a PDF document:
- Copy a PDF file named
Sample.pdfinto thetest_ocfolder. - Use the SDK to load and parse the first page of the PDF document.
[loadpdf.mm]
objc// Load a PDF document, and parse the first page of the document. NSString* pdfpath = [[NSBundle mainBundle] pathForResource:@"Sample" ofType:@"pdf"]; FSPDFDoc* doc = [[FSPDFDoc alloc] initWithPath:pdfpath]; FSErrorCode errorCode = [doc load:nil]; if (errorCode != FSErrSuccess) { return -1; } FSPDFPage* page = [doc getPage:0]; [page startParse:FSPDFPageParsePageNormal pause:nil is_reparse:NO];- Copy a PDF file named
Render and save as JPG:
- Use the SDK to render the PDF page to a bitmap and save it as a JPG image.
[SaveAsJPG.mm]
objcint width = (int)[page getWidth]; int height = (int)[page getHeight]; FSMatrix2D* matrix = [page getDisplayMatrix:0 top:0 width:width height:height rotate:page.rotation]; // Prepare a bitmap for rendering. FSBitmap* bitmap = [[FSBitmap alloc] initWithWidth:width height:height format:FSBitmapDIBArgb buffer:nil pitch:0]; [bitmap fillRect:0xFFFFFFFF rect:nil]; // Render page. FSRenderer* render = [[FSRenderer alloc] initWithBitmap:bitmap is_rgb_order:NO]; [render startRender:page matrix:matrix pause:nil]; // Add the bitmap to image and save the image. FSImage* image = [FSImage new]; [image addFrame:bitmap]; [image saveAs:@"testpage.jpg"];Create a shell script:
- Create a shell script named
RunTest.shand add the path configuration forlibfsdk_oc_mac64.dylib(x64) orlibfsdk_oc_macarm.dylib(arm64). - Shell script example:
[RunTest.sh]
bash// MacOS X64 #!/bin/bash export TEST_NAME=test_oc clang -framework Foundation -I include -L lib -lfsdk_oc_mac64 -Xlinker -rpath -Xlinker lib ${TEST_NAME}.mm -o ${TEST_NAME} ./${TEST_NAME} // MacOS arm64 #!/bin/bash export TEST_NAME=test_oc clang -framework Foundation -I include -L lib -lfsdk_oc_macarm -Xlinker -rpath -Xlinker lib ${TEST_NAME}.mm -o ${TEST_NAME} ./${TEST_NAME}- Create a shell script named
Run the script:
- Open a command-line terminal and navigate to the
test_ocdirectory. - Run the
./RunTest.shcommand. - If the script runs successfully, a
testpage.jpgfile is generated in thetest_ocfolder.
- Open a command-line terminal and navigate to the
Complete code example:
[test_oc.mm]
objc
#include "FSPDFObjC.h"
int main(int argc, const char * argv[]) {
<!-- The value of "sn" can be got from "gsdk_sn.txt"(the string after "SN="). -->
<!-- The value of "key" can be got from "gsdk_key.txt"(the string after "Sign="). -->
NSString* sn = @" ";
NSString* key = @" ";
<!-- Initialize library. -->
FSErrorCode code = [FSLibrary initialize:sn key:key];
if (code != FSErrSuccess) {
return -1;
}
<!-- Load a PDF document, and parse the first page of the document. -->
NSString* pdfpath = [[NSBundle mainBundle] pathForResource:@"Sample" ofType:@"pdf"];
FSPDFDoc* doc = [[FSPDFDoc alloc] initWithPath:pdfpath];
FSErrorCode errorCode = [doc load:nil];
if (errorCode != FSErrSuccess) {
return -1;
}
FSPDFPage* page = [doc getPage:0];
[page startParse:FSPDFPageParsePageNormal pause:nil is_reparse:NO];
int width = (int)[page getWidth];
int height = (int)[page getHeight];
FSMatrix2D* matrix = [page getDisplayMatrix:0 top:0 width:width height:height rotate:page.rotation];
<!-- Prepare a bitmap for rendering. -->
FSBitmap* bitmap = [[FSBitmap alloc] initWithWidth:width height:height format:FSBitmapDIBArgb buffer:nil pitch:0];
[bitmap fillRect:0xFFFFFFFF rect:nil];
<!-- Render page. -->
FSRenderer* render = [[FSRenderer alloc] initWithBitmap:bitmap is_rgb_order:NO];
[render startRender:page matrix:matrix pause:nil];
<!-- Add the bitmap to image and save the image. -->
FSImage* image = [FSImage new];
[image addFrame:bitmap];
[image saveAs:@"testpage.jpg"];
}