Show Multiple Files as a Single PDF using Swift for iOS

Display a single document backed by multiple PDF data providers. Get additional resources by visiting our guide on PDF file coordination in iOS.


//
// Copyright © 2021-2025 PSPDFKit GmbH. All rights reserved.
//
// The Nutrient sample applications are licensed with a modified BSD license.
// Please see License for details. This notice may not be removed from this file.
//
import PSPDFKit
import PSPDFKitUI
class MultipleFilesExample: Example {
override init() {
super.init()
title = "Using Multiple Files"
contentDescription = "Display a single document backed by multiple PDF data providers."
category = .documentDataProvider
priority = 40
}
override func invoke(with delegate: ExampleRunnerDelegate?) -> UIViewController? {
let fileNames = ["Placeholder A.pdf", "Placeholder B.pdf", "Placeholder C.pdf", "Placeholder D.pdf"]
// Collection of multiple data providers that will be used to display a combined single document.
// See https://www.nutrient.io/guides/ios/features/data-providers to learn more about Data Providers.
let dataProviders = fileNames.map { fileName -> DataProviding in
let fileURL = AssetLoader.assetURL(for: AssetName(rawValue: fileName))
// Copy the files to a writable location so that the data providers can write to the backing
// file if needed.
let writableURL = fileURL.copyToDocumentDirectory()
// `FileDataProvider` can be used to read and write to the backing file at the URL.
// However that doesn't use file coordination. See use of `CoordinatedFileDataProvider` below.
// return FileDataProvider(fileURL: writableURL)
// Nutrient also allows you to load PDF from a Data object.
// Changes to the PDF document using this data provider will not be written to the disk.
// Data mapped to the memory (virtual) can also be used.
// let dataContents = try! Data(contentsOf: writableURL, options: .mappedIfSafe)
// return DataContainerProvider(data: dataContents)
// See AESCryptoDataProviderExample.swift if you wish to use encrypted data provider.
// We are using a `CoordinatedFileDataProvider` which is similar to `FileDataProvider`
// but also uses file coordination.
// See https://www.nutrient.io/guides/ios/features/file-coordination/ for more info on File Coordination.
return CoordinatedFileDataProvider(fileURL: writableURL)
}
// Use the above created data providers to create and load a document.
let document = Document(dataProviders: dataProviders)
document.title = "Using Multiple Files Example"
// Display the document backed by multiple PDF data providers.
let controller = PDFViewController(document: document)
return controller
}
}

This code sample is an example that illustrates how to use our SDK. Please adapt it to your specific use case.