Skip to content

Attachments ​

Foxit PDF SDK for iOS provides document-level embedded file support via Core SDK: add, read, extract, and remove attachments.

UI Extensions attachment panel

With the full reader (UI Extensions), attachments are available in the side panel—no extra code required.

Core classes ​

ClassDescription
FSAttachmentsDocument attachment collection
FSFileSpecFile specification for an embedded file

Example: add, enumerate, and extract attachments ​

objc
#import <FoxitRDK/FSPDFObjC.h>

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

// Get attachment collection
FSAttachments *attachments = [[FSAttachments alloc] initWithDoc:doc];

// Add attachment
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];

// Enumerate attachments
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);

    // Export to disk
    [spec exportToFile:[@"path/to/export/" stringByAppendingString:fileName]];
}

// Remove attachment
[attachments removeEmbeddedFile:@"attachment.txt"];

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

API reference

See the API reference for FSAttachments and FSFileSpec.