Document Security and Permissions ​
Foxit PDF SDK for Web provides document password, permission reading, permission setting, and encryption capabilities. Developers can check document password types, read user permissions, and set user passwords, owner passwords, and permission bits for exported PDFs.
With the full UI (UIExtension), the password entry flow when opening encrypted documents and password-protection UI capabilities are usually provided by built-in components; your application only needs to decide whether to enable or customize those entry points based on product requirements. When you need to implement password validation, permission reading, encryption settings, or export flows yourself, use the PDFDoc APIs described in this topic.
Related APIs include:
PDFUI.openPDFByFile()/PDFUI.openPDFByHttpRangeRequest(): In the full UI, use with the built-in password entry flow when opening encrypted documents.PDFViewer.openPDFByFile()/PDFViewer.openPDFByHttpRangeRequest(): When integrating only the base Viewer, pass the password in open options.PDFDoc.checkPassword(),PDFDoc.getPasswordType(),PDFDoc.hasOwnerPassword(): Check password type and owner password status.PDFDoc.getPermissions(),PDFDoc.getUserPermissions(): Read document permissions.PDFDoc.setPasswordAndPermission(): Set user password, owner password, permission bits, and encryption algorithm.PDFDoc.getFile(): Export the PDF after setting password and permissions.
Open an Encrypted Document ​
When opening an encrypted document, pass the password in open options.
javascript
await pdfViewer.openPDFByFile(file, {
password: '123456',
fileName: file.name
});If your application does not use the built-in password entry flow, collect the password yourself and call the open API again.
Check Password Type ​
PDFDoc.checkPassword(password) checks the password type; hasOwnerPassword() indicates whether the current document has an owner password.
javascript
const pdfDoc = pdfViewer.getCurrentPDFDoc();
const passwordType = await pdfDoc.checkPassword('123456');
const hasOwnerPassword = await pdfDoc.hasOwnerPassword();
console.log(passwordType, hasOwnerPassword);You can also use getPasswordType() to get the password type of the currently open document.
javascript
const passwordType = await pdfDoc.getPasswordType();Read Document Permissions ​
javascript
const permissions = await pdfDoc.getPermissions();
const userPermissions = await pdfDoc.getUserPermissions();
console.log(permissions, userPermissions);Permission information can control business UI—for example, disabling print, copy, annotation, form filling, or page organization entry points.
Set Password and Permissions ​
PDFDoc.setPasswordAndPermission(userPassword, ownerPassword, permission, cipher, isEncryptMetadata) sets password and permissions for the document.
javascript
const success = await pdfDoc.setPasswordAndPermission(
'user-password',
'owner-password',
0xfffffffc,
'aes256',
true
);
if (!success) {
throw new Error('Failed to set document security options');
}Common parameters:
| Parameter | Description |
|---|---|
userPassword | User password; opens the document with restricted permissions. |
ownerPassword | Owner password; grants full permissions. |
permission | User permission bits; controls print, modify, content extraction, annotation, forms, and related capabilities. |
cipher | Encryption algorithm; supports none, rc4, aes128, aes256. |
isEncryptMetadata | Whether to encrypt metadata. |
Save the Encrypted Document ​
After setting password and permissions, export the file through getFile() to save locally or upload to a server.
javascript
await pdfDoc.setPasswordAndPermission(
'user-password',
'owner-password',
0xfffffffc,
'aes256',
true
);
const file = await pdfDoc.getFile({
fileName: 'protected.pdf'
});Read-Only and Business Permission Control ​
Document permissions and business permissions usually need to be handled together:
- PDF document permissions are read through APIs such as
getPermissions()andgetUserPermissions(). - Annotation-level permissions can be controlled through the Authority manager.
- Business UI should disable corresponding entry points based on permissions to avoid operations the user is not allowed to perform.
Notes ​
- Password and permission changes affect the exported PDF; call
getFile()after changes to save results. - Owner passwords should be stored securely by your business system to avoid leakage.
- Permission bits are PDF standard capabilities; actual behavior is also affected by the viewer and business UI controls.
- Prefer
aes256as the encryption algorithm when possible.