Encryption and permissions
Open password-protected PDFs, read their permissions, encrypt with AES-256, remove passwords and stamp watermarks with NexPDF for Flutter.
License module
Encrypting, removing passwords, watermarks, headers, footers and Bates numbering need the security
module (Startup tier and above). Opening encrypted files and reading their security settings work
without a key.
Open an encrypted PDF
try {
final doc = sdk.open(bytes, password: 'secret');
} on NexPdfException catch (e) {
print(e.message); // "password required" or "incorrect password"
}The user (open) password or the owner password both work. RC4, AES-128 and AES-256 files are supported. The native reader asks the user for the password itself.
Read the security settings
The security functions have no Dart method. Call them
through dart:ffi on doc.handle:
import 'dart:convert';
import 'dart:ffi';
import 'package:ffi/ffi.dart';
import 'nexpdf_ffi.dart';
// char *nexpdf_security_json(const NexPdfDocument *doc);
final securityJson = nexpdfLib.lookupFunction<
Pointer<Utf8> Function(Pointer<Void>), Pointer<Utf8> Function(Pointer<Void>)>('nexpdf_security_json');
final info = jsonDecode(takeString(securityJson(doc.handle)) ?? '{}') as Map<String, Object?>;
print('${info['encrypted']} ${info['owner']} ${info['print']} ${info['copy']}');nexpdf_security_json reports encrypted, revision, owner (opened with owner access) and the
permissions print, modify, copy, annotate, fill_forms and assemble.
Encrypt with permissions
// int32_t nexpdf_encrypt_with_permissions(NexPdfDocument *doc, const char *user_pw,
// const char *owner_pw, uint32_t permissions);
final encrypt = nexpdfLib.lookupFunction<
Int32 Function(Pointer<Void>, Pointer<Utf8>, Pointer<Utf8>, Uint32),
int Function(Pointer<Void>, Pointer<Utf8>, Pointer<Utf8>, int)>('nexpdf_encrypt_with_permissions');
const printFlag = 1, fillFormsFlag = 16;
using((a) => nexpdfCall(() => encrypt(doc.handle, ''.toNativeUtf8(allocator: a),
'owner-password'.toNativeUtf8(allocator: a), printFlag | fillFormsFlag)));
final protected = doc.save();An empty user password lets anyone open the file while the owner password still protects the permissions.
| Flag | Value |
|---|---|
| 1 | |
| Modify | 2 |
| Copy text and images | 4 |
| Annotate | 8 |
| Fill forms | 16 |
| Assemble (insert, rotate, delete pages) | 32 |
Other security functions
| C function | Does |
|---|---|
nexpdf_encrypt_aes256(doc, user_pw, owner_pw) | AES-256 with every permission |
nexpdf_remove_password(doc, password) | Removes the encryption; pass the owner password or NULL if opened with it |
nexpdf_add_watermark(doc, text), nexpdf_remove_watermarks(doc) | A text watermark on every page |
nexpdf_add_header_footer(doc, header, footer, font_size) | Header and footer; {page} and {total} are replaced, font size 0 is the default |
nexpdf_add_bates(doc, prefix, start, digits) | Bates numbers such as ACME-000001 |