Attach Files to PDFs on iOS
PSPDFKit for iOS 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.
See also: How to Embed Files Using File Annotations on our blog.
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])
// Create `PSPDFDocument`. PSPDFDocument *document = [[PSPDFDocument alloc] initWithURL:documentURL]; // Create the URL of the appearance stream that uses a PDF file. NSURL *samplesURL = [NSBundle.mainBundle.resourceURL URLByAppendingPathComponent:@"Samples"]; NSURL *embeddedFileURL = [samplesURL URLByAppendingPathComponent:@"Monthly Budget.xlsx"]; // Create a new file annotation and set its properties. PSPDFFileAnnotation *fileAnnotation = [[PSPDFFileAnnotation alloc] init]; fileAnnotation.pageIndex = 0; fileAnnotation.iconName = PSPDFFileIconNameGraph; fileAnnotation.color = UIColor.blueColor; fileAnnotation.boundingBox = CGRectMake(500.0, 250.0, 32.0, 32.0); // Create an embedded file and add it to the file annotation. PSPDFEmbeddedFile *embeddedFile = [[PSPDFEmbeddedFile alloc] initWithFileURL:embeddedFileURL fileDescription:@"Monthly Budget"]; fileAnnotation.embeddedFile = embeddedFile; [document addAnnotations:@[fileAnnotation] options:nil];
To learn more about how to programmatically create file annotations with embedded files, check out AddFileAnnotationProgrammaticallyExample
inside the Catalog app.