Creating Password-Protected PDFs on iOS

PSPDFKit’s Processor API can generate a password-protected document from another document. You can use Processor to create a new password-protected PDF document on disk based on a current Document. See CreatePasswordProtectedDocumentExample from the Catalog app for a complete example of how to create a password-protected document:

// By default, a newly initialized `Processor.Configuration` results in an exported document that is the same as the input.
guard let processorConfiguration = Processor.Configuration(document: originalDocument) else { return }

do {
    // Set the proper password and key length in `Document.SecurityOptions`.
    let documentSecurityOptions = try Document.SecurityOptions(ownerPassword: ownerPassword, userPassword: userPassword, keyLength: Document.SecurityOptionsKeyLengthAutomatic)
    let processor = Processor(configuration: processorConfiguration, securityOptions: documentSecurityOptions)
    try processor.write(toFileURL: outputFileURL)
} catch {
    // handle error
}

// Initialize the password-protected document.
let passwordProtectedDocument = Document(url: outputFileURL)
// By default, a newly initialized `PSPDFProcessorConfiguration` results in an exported document that is the same as the input.
PSPDFProcessorConfiguration *processorConfiguration = [[PSPDFProcessorConfiguration alloc] initWithDocument:originalDocument];

// Set the proper password and key length in `PSPDFDocumentSecurityOptions`.
PSPDFDocumentSecurityOptions *documentSecurityOptions = [[PSPDFDocumentSecurityOptions alloc] initWithOwnerPassword:ownerPassword userPassword:userPassword keyLength:PSPDFDocumentSecurityOptionsKeyLengthAutomatic error:&error];

PSPDFProcessor *processor = [[PSPDFProcessor alloc] initWithConfiguration:processorConfiguration securityOptions:documentSecurityOptions];
[processor writeToFileURL:outputFileURL error:NULL];

// Initialize the password-protected document.
PSPDFDocument *passwordProtectedDocument = [[PSPDFDocument alloc] initWithURL:outputFileURL];
PSPDFViewController *pdfController = [[PSPDFViewController alloc] initWithDocument:passwordProtectedDocument];