API referenceEncryption

pdfEncryptPDF

Two entry points to the same real Standard Security

C
int32_t __stdcall pdfEncryptPDFA(PPDF IPDF, const char* FileName, const char* OpenPwd, const char* OwnerPwd, TKeyLen KeyLen, TRestrictions Restrict);
BOOL32 __stdcall pdfOpenOutputFileEncryptedA(PPDF IPDF, const char* OutPDF, const char* OpenPwd, const char* OwnerPwd, TKeyLen KeyLen, TRestrictions Restrict);
Delphi
function pdfEncryptPDFA(const IPDF: PPDF; const FileName, OpenPwd, OwnerPwd: PAnsiChar; KeyLen: TKeyLen; Restrict: TRestrictions): Integer; stdcall;
function pdfOpenOutputFileEncryptedA(const IPDF: PPDF; const OutPDF: PAnsiChar; const OpenPwd, OwnerPwd: PAnsiChar; KeyLen: TKeyLen; Restrict: TRestrictions): LongBool; stdcall;

Purpose. Two entry points to the same real Standard Security Handler encryption: pdfEncryptPDF opens an output file, arms encryption, and immediately closes (a one-shot "encrypt and save" for a document with no further edits); pdfOpenOutputFileEncrypted arms encryption and opens the output file but leaves the document open for further edits before the caller closes it — the setup half of the normal open→edit→close workflow, with encryption pre-armed.

Description. Both call the same real SetEncryption(AKeyLen, UserPwd, OwnerPwd, Permissions), which is a genuine no-op when the document has a PDF/A conformance level set (FPdfAConf >= 0) — PDF/A-1 forbids encryption, and this guard is enforced unconditionally regardless of what the caller requests, silently returning False rather than raising an error. pdfEncryptPDF additionally opens+closes the output file itself as part of the call; pdfOpenOutputFileEncrypted only opens it, requiring the caller to make edits and call the normal close function afterward.

Parameters.

ParameterDescription
IPDFDocument handle.
FileName / OutPDFOutput file path.
OpenPwd, OwnerPwdThe user (open) and owner passwords.
KeyLenRC4-40/RC4-128/AES-128/AES-256 (per TKeyLen).
RestrictPermission-restriction bitmask.

Return value. pdfEncryptPDF: 1 on success, 0 if FileName is nil, the output file couldn't be opened, encryption is forbidden (PDF/A), or the close fails. pdfOpenOutputFileEncrypted: True/False for the same conditions (minus the close step).

C# (P/Invoke)

wrappers/dotnet/LumasPdf.cs
public static extern int pdfEncryptPDFA(IntPtr IPDF, [MarshalAs(UnmanagedType.LPStr)] string FileName, [MarshalAs(UnmanagedType.LPStr)] string OpenPwd, [MarshalAs(UnmanagedType.LPStr)] string OwnerPwd, TKeyLen KeyLen, int Restrict);
public static extern int pdfEncryptPDFW(IntPtr IPDF, [MarshalAs(UnmanagedType.LPWStr)] string FileName, [MarshalAs(UnmanagedType.LPStr)] string OpenPwd, [MarshalAs(UnmanagedType.LPStr)] string OwnerPwd, TKeyLen KeyLen, int Restrict);
Area
Encryption
Category

Encryption

Exported names

pdfEncryptPDFA pdfEncryptPDFW

String variants

The …A form takes UTF-8, …W takes UTF-16; a bare name aliases the ANSI form.

See working code

Worked examples — complete programs in ten languages.