Skip to content

附件

福昕 PDF SDK iOS 版通过 Core SDK 提供了 PDF 文档级附件(Embedded Files)的添加、读取、提取和删除能力。

UI Extensions 内置附件面板

如果您使用的是完整阅读器(UI Extensions),附件功能已内置在侧边面板中,用户可直接查看和管理文档中的附件文件,无需额外编码。

核心类

说明
FSAttachments文档级附件集合,管理所有嵌入文件
FSFileSpec文件规格对象,表示一个嵌入文件的路径、名称、数据等属性

示例:添加、遍历和提取附件

objc
#import <FoxitRDK/FSPDFObjC.h>

FSPDFDoc *doc = [[FSPDFDoc alloc] initWithPath:@"path/to/Sample.pdf"];
[doc load:nil];

// 获取附件集合
FSAttachments *attachments = [[FSAttachments alloc] initWithDoc:doc];

// 添加附件
NSString *filePath = @"path/to/attachment.txt";
FSFileSpec *fileSpec = [[FSFileSpec alloc] initWithPDFDoc:doc];
[fileSpec setFileName:@"attachment.txt"];
[fileSpec embed:filePath];
[attachments addEmbeddedFile:@"attachment.txt" file_spec:fileSpec];

// 遍历所有附件
int count = [attachments getCount];
for (int i = 0; i < count; i++) {
    NSString *key = [attachments getKey:i];
    FSFileSpec *spec = [attachments getEmbeddedFile:key];
    NSString *fileName = [spec getFileName];
    NSLog(@"附件: %@", fileName);

    // 提取附件到本地
    [spec exportToFile:[@"path/to/export/" stringByAppendingString:fileName]];
}

// 删除附件
[attachments removeEmbeddedFile:@"attachment.txt"];

[doc saveAs:@"path/to/output.pdf" saveFlags:FSPDFDocSaveFlagNormal];

API 参考

FSAttachmentsFSFileSpec 的完整接口说明请参阅 API 手册