Security
Foxit PDF SDK for Android provides document encryption and decryption through the Core SDK, including password encryption, certificate encryption, and custom security handlers for different protection levels.
Built-in password prompt in UI Extensions
If you use the full reader (UI Extensions), opening encrypted documents shows a password dialog (PasswordModule) automatically. No extra code is required.
Core Classes
| Class | Description |
|---|---|
StdSecurityHandler | Standard security handler for password encryption |
StdEncryptData | Password encryption parameters (algorithm, key length, permissions, etc.) |
CertificateSecurityHandler | Certificate security handler for certificate encryption |
Encryption Type Constants
Use PDFDoc.getEncryptionType() to get the current encryption type:
| Constant | Value | Description |
|---|---|---|
e_EncryptNone | 0 | Not encrypted |
e_EncryptPassword | 1 | Password encryption |
e_EncryptCertificate | 2 | Certificate encryption |
e_EncryptFoxitDRM | 3 | Foxit DRM encryption |
e_EncryptCustom | 4 | Custom encryption |
e_EncryptRMS | 5 | Microsoft RMS encryption |
Cipher Algorithm Constants
| Constant | Value | Description |
|---|---|---|
SecurityHandler.e_CipherRC4 | 1 | RC4 |
SecurityHandler.e_CipherAES | 2 | AES |
User Permission Constants
Set via StdEncryptData user_permissions; combine with bitwise OR:
| Constant | Value | Description |
|---|---|---|
PDFDoc.e_PermPrint | 0x0004 | Allow printing |
PDFDoc.e_PermModify | 0x0008 | Allow modifying the document |
PDFDoc.e_PermExtract | 0x0010 | Allow content extraction |
PDFDoc.e_PermAnnotForm | 0x0020 | Allow annotations and form filling |
PDFDoc.e_PermFillForm | 0x0100 | Form filling only |
PDFDoc.e_PermExtractAccess | 0x0200 | Allow accessibility extraction |
PDFDoc.e_PermAssemble | 0x0400 | Allow document assembly |
PDFDoc.e_PermPrintHigh | 0x0800 | Allow high-quality printing |
Example: Password Encryption
java
import com.foxit.sdk.pdf.PDFDoc;
import com.foxit.sdk.pdf.SecurityHandler;
import com.foxit.sdk.pdf.StdEncryptData;
import com.foxit.sdk.pdf.StdSecurityHandler;
PDFDoc doc = new PDFDoc("path/to/Sample.pdf");
doc.load(null);
// Encrypt metadata, permissions, AES-128
StdEncryptData encryptData = new StdEncryptData(
true,
PDFDoc.e_PermModify | PDFDoc.e_PermFillForm | PDFDoc.e_PermPrint,
SecurityHandler.e_CipherAES,
16
);
StdSecurityHandler handler = new StdSecurityHandler();
byte[] userPassword = "user123".getBytes();
byte[] ownerPassword = "owner456".getBytes();
if (handler.initialize(encryptData, userPassword, ownerPassword)) {
doc.setSecurityHandler(handler);
doc.saveAs("path/to/encrypted.pdf", PDFDoc.e_SaveFlagNormal);
}Example: Certificate Encryption
java
import com.foxit.sdk.pdf.CertificateSecurityHandler;
CertificateSecurityHandler certHandler = new CertificateSecurityHandler();
certHandler.initialize(envelopes, SecurityHandler.e_CipherAES, true);
doc.setSecurityHandler(certHandler);
doc.saveAs("path/to/cert_encrypted.pdf", PDFDoc.e_SaveFlagNormal);Remove Encryption
java
doc.removeSecurity();
doc.saveAs("path/to/decrypted.pdf", PDFDoc.e_SaveFlagNormal);API Reference
See the API Reference for full StdSecurityHandler, StdEncryptData, and CertificateSecurityHandler documentation.