Back to Blog

    Developer Blog

    How to Add Attachments to your PDFs with Foxit PDF SDK (Objective-C)

    Foxit PDF SDK for Mac

    With Foxit PDF SDK, attachments refer to documents rather than file attachment annotations. This allows whole files to be embedded in a document, much like email attachments. PDF SDK provides application APIs to access functions such as loading attachments, getting attachments, inserting/removing attachments, and accessing properties of attachments.

    Example:

    How to export the embedded attachment file from a PDF and save it as a single file

    #include "FSPDFObjC.h"
    ...
    FSAttachments *attachments = [[FSAttachments alloc] initWithDoc:doc nametree:[[FSPDFNameTree alloc] init]];
    int count = [attachments getCount];
    for (int i = 0; i < count; i++) {
        NSString *key = [attachments getKey:i];
        FSFileSpec *file_spec = [attachments getEmbeddedFile:key];
        if (![file_spec isEmpty]) {
            NSString *name = [file_spec getFileName];
            if ([file_spec isEmbedded]) {
                NSString *exFilePath = [[NSString alloc] initWithFormat:@"%@%@", output_directory, name];
                bool bExportStatus = [file_spec exportToFile:exFilePath];
            }
        }
    }
    

    How to remove all the attachments of a PDF

    #include "FSPDFObjC.h"
    ...
    // Assuming FSPDFDoc doc has been loaded.
    FSAttachments *attachments = [[FSAttachments alloc] initWithDoc:doc nametree:[[FSPDFNameTree alloc] init]];
    [attachments removeAllEmbeddedFiles];      
    ...