How to attach files to PDFs on iOS

Nutrient iOS SDK has the ability to embed files using FileAnnotation and EmbeddedFile. Take a look at the file annotations with embedded files section of the programmatically creating annotations guide, along with AddFileAnnotationProgrammaticallyExample inside the Catalog app, both of which show how to programmatically create file annotations with embedded files.

Programmatically create a file annotation with an embedded file

Embedded files can be attached to a FileAnnotation object. You can create a file annotation with an embedded file using the URL of any file. Here’s how this looks in code:

// Create `Document`.
let document = Document(url: documentURL)
// Create the URL of the appearance stream that uses a PDF file.
let samplesURL = Bundle.main.resourceURL!.appendingPathComponent("Samples")
let embeddedFileURL = samplesURL.appendingPathComponent("Monthly Budget.xlsx")
// Create a new file annotation and set its properties.
let fileAnnotation = FileAnnotation()
fileAnnotation.pageIndex = 0
fileAnnotation.iconName = .graph
fileAnnotation.color = .blue
fileAnnotation.boundingBox = CGRect(x: 500, y: 250, width: 32, height: 32)
// Create an embedded file and add it to the file annotation.
let embeddedFile = EmbeddedFile(fileURL: embeddedFileURL, fileDescription: "Monthly Budget")
fileAnnotation.embeddedFile = embeddedFile
// Add the newly created annotation to the document.
document.add(annotations: [fileAnnotation])

To learn more about how to programmatically create file annotations with embedded files, check out AddFileAnnotationProgrammaticallyExample inside the Catalog app.