1. Home
  2. Foxit PDF SDK for iOS
  3. How to Add Attachments to PDFs with Foxit PDF SDK for iOS

How to Add Attachments to PDFs with Foxit PDF SDK for iOS

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

Example:

How to embed a specified file to a PDF document

#import "ViewController.h"
#import 
...
NSString* filePath = @"/xxx/fileToBeEmbedded.xxx";
FSPDFNameTree* nameTree = [[FSPDFNameTree alloc] initWithDocument:self.fspdfdoc type:FSPDFNameTreeEmbeddedFiles];
FSAttachments* attachments = [[FSAttachments alloc] initWithDoc:self.fspdfdoc nametree:nameTree];
FSFileSpec* fileSpec = [[FSFileSpec alloc] initWithDocument:self.fspdfdoc];
[fileSpec setFileName:[filePath lastPathComponent]];
if(![fileSpec embed:filePath])
   return;
[attachments addEmbeddedFile:[filePath lastPathComponent] file_spec:fileSpec];
...

Example:

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

#import "ViewController.h"
#import 
...
// Extract the embedded attachment file.
int count = [attachments getCount];
for(int i=0; iif(key) {
       FSFileSpec* fileSpec = [attachments getEmbeddedFile:key];
       NSString* exportedFile = [@"/somewhere/" stringByAppendingString: [fileSpec getFileName]];
       if(fileSpec && ![fileSpec isEmpty]) {
           fileSpec exportToFile:exportedFile];
       }
   }
}
...
Updated on October 23, 2019

Was this article helpful?

Related Articles