Perl Requires Chilkat v11.7.0+
Chilkat2 Perl
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 Perl Downloads
use strict;
use warnings;
use Chilkat2;
# 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
my $pdf = Chilkat2::Pdf->new;
# Removing the encryption needs owner rights: load with the owner password.
$pdf->OwnerPassword('owner-secret');
my $success = $pdf->LoadFile('qa_data/pdf/encrypted_user_password.pdf');
if (!$success) {
print $pdf->LastErrorText, "\n";
exit;
}
my $json = Chilkat2::JsonObject->new;
my $jsonOut = Chilkat2::JsonObject->new;
$success = $pdf->Protection('getInfo', $json, $jsonOut);
if (!$success) {
print $pdf->LastErrorText, "\n";
exit;
}
print 'encrypted, as loaded: ', ($jsonOut->BoolOf('loaded.encrypted') ? 1 : 0), "\n";
print 'algorithm: ', $jsonOut->StringOf('loaded.algorithm'), "\n";
$success = $pdf->Protection('removeEncryption', $json, $jsonOut);
if (!$success) {
print $pdf->LastErrorText, "\n";
exit;
}
print 'will be saved encrypted: ', ($jsonOut->BoolOf('pending.encrypted') ? 1 : 0), "\n";
$success = $pdf->WriteFile('qa_output/minutes_unprotected.pdf');
if (!$success) {
print $pdf->LastErrorText, "\n";
exit;
}
# The result is an ordinary PDF that opens without a password.
my $reader = Chilkat2::Pdf->new;
my $info = Chilkat2::JsonObject->new;
$success = $reader->PreLoadCheck('qa_output/minutes_unprotected.pdf', $info);
if (!$success) {
print $reader->LastErrorText, "\n";
exit;
}
print 'encrypted: ', ($info->BoolOf('encrypted') ? 1 : 0), "\n";
$success = $reader->LoadFile('qa_output/minutes_unprotected.pdf');
if (!$success) {
print $reader->LastErrorText, "\n";
exit;
}
print 'pages: ', $reader->NumPages, "\n";
# Sample output:
# encrypted, as loaded: True
# algorithm: rc4-128
# will be saved encrypted: False
# encrypted: False
# pages: 1