Dart Requires Chilkat v11.7.0+
Dart
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 Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
// 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
final pdf = CkPdf();
// Removing the encryption needs owner rights: load with the owner password.
pdf.ownerPassword = 'owner-secret';
try {
pdf.loadFile('qa_data/pdf/encrypted_user_password.pdf');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
final json = CkJsonObject();
final jsonOut = CkJsonObject();
try {
pdf.protection('getInfo', json, jsonOut);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
print('encrypted, as loaded: ${jsonOut.boolOf('loaded.encrypted')}');
print('algorithm: ${jsonOut.stringOf('loaded.algorithm')}');
try {
pdf.protection('removeEncryption', json, jsonOut);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
print('will be saved encrypted: ${jsonOut.boolOf('pending.encrypted')}');
try {
pdf.writeFile('qa_output/minutes_unprotected.pdf');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// The result is an ordinary PDF that opens without a password.
final reader = CkPdf();
final info = CkJsonObject();
try {
reader.preLoadCheck('qa_output/minutes_unprotected.pdf', info);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
print('encrypted: ${info.boolOf('encrypted')}');
try {
reader.loadFile('qa_output/minutes_unprotected.pdf');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
print('pages: ${reader.numPages}');
// Sample output:
// encrypted, as loaded: True
// algorithm: rc4-128
// will be saved encrypted: False
// encrypted: False
// pages: 1
}