1. Home
  2. Foxit PDF SDK for Android
  3. How to Encrypt PDF files with Foxit PDF SDK for Android

How to Encrypt PDF files with Foxit PDF SDK for Android

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 com.foxit.sdk.PDFException;
import com.foxit.sdk.PDFViewCtrl;
import com.foxit.sdk.common.Constants;
import com.foxit.sdk.pdf.PDFDoc;
import com.foxit.sdk.pdf.SecurityHandler;
import com.foxit.sdk.pdf.StdEncryptData;
import com.foxit.sdk.pdf.StdSecurityHandler;
...
// 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.
public boolean encryPDF(PDFDoc pdfDoc, byte[] ownerPassword, byte[] userPassword, String savePth) {
 if (pdfDoc == null || (ownerPassword == null && userPassword == null) || savePth == null)
 return false;
 // The encryption setting data. Whether to encrypt meta data:true, User permission: modify,assemble,fill form. Cipher algorithm:AES 128.
 StdEncryptData encryptData = new StdEncryptData(true,
 PDFDoc.e_PermModify | PDFDoc.e_PermAssemble | PDFDoc.e_PermFillForm,
 SecurityHandler.e_CipherAES, 16);
 StdSecurityHandler securityHandler = new StdSecurityHandler();
 try {
 if (!securityHandler.initialize(encryptData, userPassword, ownerPassword)) return false;
 pdfDoc.setSecurityHandler(securityHandler);
 if (!pdfDoc.saveAs(savePth, PDFDoc.e_SaveFlagNormal)) return false;
 return true;
 } catch (PDFException e) {
 e.printStackTrace();
 }
 return false;
}
Updated on October 23, 2019

Was this article helpful?

Related Articles