Skip to content

福昕 PDF SDK Objective-C 库

本节主要介绍如何使用福昕 PDF SDK Objective-C 库运行示例及创建一个基础的工程。该工程演示如何将 PDF 文档的首页渲染成 bitmap,并将其另存为 JPG 图片。

先决条件

开发环境

  • Xcode
  • 福昕 PDF SDK Objective-C 库

系统支持

我们提供 MacOS 平台的详细系统支持信息,包括操作系统版本、编译器要求等。请选择您的操作系统平台,查看详细信息。 MacOS 平台

运行示例

使用命令行运行示例

请参阅 示例,其中包含了示例工程简介,特定示例的依赖信息,以及如何通过命令行运行示例。

快速创建工程

  1. 创建项目目录:

    • 新建一个名为 test_oc 的文件夹作为项目目录。
  2. 复制 SDK 文件:

    • Mac x64:foxitpdfsdk_*_mac_oc SDK 包文件夹下的 includelib 文件夹复制到 test_oc 目录下。
    • Mac arm64:foxitpdfsdk_*_mac_arm64_oc SDK 包文件夹下的 includelib 文件夹复制到 test_oc 目录下。
  3. 创建 Objective-C 类文件:

    • test_oc 文件夹中,创建一个名为 test_oc.mm 的 Objective-C 类文件。
  4. 添加头文件:

    • 打开 test_oc.mm 文件,并在文件开头添加以下头文件声明:
    Objc
    #include "FSPDFObjC.h"
  5. 初始化 SDK 库:

    • 在调用任何 API 之前,必须使用许可证密钥初始化 Foxit PDF SDK 库。参考初始化SDK库
  6. 加载 PDF 文档:

    • 将一个名为 Sample.pdf 的 PDF 文件复制到 test_oc 文件夹中。
    • 使用 SDK 加载并解析该 PDF 文档的首页。
    [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. 渲染并保存为 JPG:

    • 使用 SDK 将 PDF 页面渲染成位图,并将其保存为 JPG 图片。
    [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. 创建 Shell 脚本:

    • 创建一个名为 RunTest.sh 的 Shell 脚本,并添加 libfsdk_oc_mac64.dylib (x64) 或 libfsdk_oc_macarm.dylib (arm64) 的路径配置。
    • Shell 脚本示例:
    [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. 运行脚本:

    • 打开命令行终端,导航到 test_oc 目录。
    • 运行 ./RunTest.sh 命令。
    • 如果脚本成功运行,将在 test_oc 文件夹下生成 testpage.jpg 文件。

完整代码示例:

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