Delphi DLL Requires Chilkat v11.7.0+
Delphi DLL
Remove the Password and Encryption from a PDF
See more PDF Protection Examples
Demonstrates the removeEncryption operation, which saves the PDF with no encryption at all: no password, no restrictions. It needs owner rights, so the PDF is loaded with its owner password. As with every Protection operation, the change is applied when the file is written.Chilkat Delphi DLL Downloads
var
success: Boolean;
pdf: HCkPdf;
json: HCkJsonObject;
jsonOut: HCkJsonObject;
reader: HCkPdf;
info: HCkJsonObject;
begin
success := False;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// The PDF used in this example is available at encrypted_user_password.pdf
pdf := CkPdf_Create();
// Removing the encryption needs owner rights: load with the owner password.
CkPdf_putOwnerPassword(pdf,'owner-secret');
success := CkPdf_LoadFile(pdf,'qa_data/pdf/encrypted_user_password.pdf');
if (success = False) then
begin
Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
Exit;
end;
json := CkJsonObject_Create();
jsonOut := CkJsonObject_Create();
success := CkPdf_Protection(pdf,'getInfo',json,jsonOut);
if (success = False) then
begin
Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
Exit;
end;
Memo1.Lines.Add('encrypted, as loaded: ' + IntToStr(Ord(CkJsonObject_BoolOf(jsonOut,'loaded.encrypted'))));
Memo1.Lines.Add('algorithm: ' + CkJsonObject__stringOf(jsonOut,'loaded.algorithm'));
success := CkPdf_Protection(pdf,'removeEncryption',json,jsonOut);
if (success = False) then
begin
Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
Exit;
end;
Memo1.Lines.Add('will be saved encrypted: ' + IntToStr(Ord(CkJsonObject_BoolOf(jsonOut,'pending.encrypted'))));
success := CkPdf_WriteFile(pdf,'qa_output/minutes_unprotected.pdf');
if (success = False) then
begin
Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
Exit;
end;
// The result is an ordinary PDF that opens without a password.
reader := CkPdf_Create();
info := CkJsonObject_Create();
success := CkPdf_PreLoadCheck(reader,'qa_output/minutes_unprotected.pdf',info);
if (success = False) then
begin
Memo1.Lines.Add(CkPdf__lastErrorText(reader));
Exit;
end;
Memo1.Lines.Add('encrypted: ' + IntToStr(Ord(CkJsonObject_BoolOf(info,'encrypted'))));
success := CkPdf_LoadFile(reader,'qa_output/minutes_unprotected.pdf');
if (success = False) then
begin
Memo1.Lines.Add(CkPdf__lastErrorText(reader));
Exit;
end;
Memo1.Lines.Add('pages: ' + IntToStr(CkPdf_getNumPages(reader)));
// Sample output:
// encrypted, as loaded: True
// algorithm: rc4-128
// will be saved encrypted: False
// encrypted: False
// pages: 1
CkPdf_Dispose(pdf);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);
CkPdf_Dispose(reader);
CkJsonObject_Dispose(info);