Skip to content

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.

Mac

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

  1. Create a project directory:

    • Create a folder named test_oc as the project directory.
  2. Copy SDK files:

    • Mac x64: Copy the include and lib folders from the foxitpdfsdk_*_mac_oc SDK package into the test_oc directory.
    • Mac arm64: Copy the include and lib folders from the foxitpdfsdk_*_mac_arm64_oc SDK package into the test_oc directory.
  3. Create an Objective-C class file:

    • In the test_oc folder, create an Objective-C class file named test_oc.mm.
  4. Add header files:

    • Open the test_oc.mm file and add the following header declaration at the top of the file:
    Objc
    #include "FSPDFObjC.h"
  5. Initialize the SDK library:

  6. Load a PDF document:

    • Copy a PDF file named Sample.pdf into the test_oc folder.
    • 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];
  7. 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]
    objc
    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"];
  8. Create a shell script:

    • Create a shell script named RunTest.sh and add the path configuration for libfsdk_oc_mac64.dylib (x64) or libfsdk_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}
  9. Run the script:

    • Open a command-line terminal and navigate to the test_oc directory.
    • Run the ./RunTest.sh command.
    • If the script runs successfully, a testpage.jpg file is generated in the test_oc folder.

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"];
}