Back to Blog

    Developer Blog

    How to Encrypt PDF files with Foxit PDF SDK for iOS

    Foxit PDF SDK for iOS

    Foxit PDF SDK provides a range of encryption and decryption functions to meet different levels of document security protection. Users can use regular password encryption and certificate-driven encryption, or use their own security handler for custom security implementation.

    Example:

    How to encrypt a PDF file with password

    #import "ViewController.h"
    #import 
    ...
    // Encrypt the source pdf document with specified owner password and user password, the encrypted PDF will be saved to the path specified by parameter savePath.
    - (BOOL) encryptPDF: (FSPDFDoc*) pdfDoc ownerPassword: (NSString*)ownerPassword userPassword: (NSString*)userPassword savedPath: (NSString*)savedPath
    {
        if(!pdfDoc || (!ownerPassword && !userPassword) || !savedPath)
            return NO;
        // The encryption setting data. Whether to encrypt meta data:YES, User permission: modify,assemble,fill form. Cipher algorithm:AES 128.
        FSStdEncryptData* encryptData = [[FSStdEncryptData alloc] initWithIs_encrypt_metadata:YES user_permissions: (FSPDFDocPermModify| FSPDFDocPermAssemble|FSPDFDocPermFillForm) cipher:FSSecurityHandlerCipherAES key_length:16];
        FSStdSecurityHandler * stdSecurity = [[FSStdSecurityHandler alloc] init];
        if(![stdSecurity initialize:encryptData user_password:userPassword owner_password:ownerPassword])
            return NO;
        [pdfDoc setSecurityHandler:stdSecurity];
        if(![pdfDoc saveAs:savedPath save_flags:FSPDFDocSaveFlagNormal])
            return NO;
        return YES;
    }