React Native Requires Chilkat v11.7.0+
React Native
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 React Native Downloads
import { JsonObject, Pdf } from '@chilkat/react-native'
function chilkatExample() {
// 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
const pdf = new Pdf();
// 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');
} catch {
console.log(pdf.lastErrorText);
return;
}
const json = new JsonObject();
const jsonOut = new JsonObject();
try {
pdf.protection('getInfo', json, jsonOut);
} catch {
console.log(pdf.lastErrorText);
return;
}
console.log(`encrypted, as loaded: ${jsonOut.boolOf('loaded.encrypted')}`);
console.log(`algorithm: ${jsonOut.stringOf('loaded.algorithm')}`);
try {
pdf.protection('removeEncryption', json, jsonOut);
} catch {
console.log(pdf.lastErrorText);
return;
}
console.log(`will be saved encrypted: ${jsonOut.boolOf('pending.encrypted')}`);
try {
pdf.writeFile('qa_output/minutes_unprotected.pdf');
} catch {
console.log(pdf.lastErrorText);
return;
}
// The result is an ordinary PDF that opens without a password.
const reader = new Pdf();
const info = new JsonObject();
try {
reader.preLoadCheck('qa_output/minutes_unprotected.pdf', info);
} catch {
console.log(reader.lastErrorText);
return;
}
console.log(`encrypted: ${info.boolOf('encrypted')}`);
try {
reader.loadFile('qa_output/minutes_unprotected.pdf');
} catch {
console.log(reader.lastErrorText);
return;
}
console.log(`pages: ${reader.numPages}`);
// Sample output:
// encrypted, as loaded: True
// algorithm: rc4-128
// will be saved encrypted: False
// encrypted: False
// pages: 1
}