React Native Requires Chilkat v11.7.0+
React Native
Choose the PDF Encryption Algorithm: AES-256, AES-128 or RC4
See more PDF Protection Examples
Demonstrates the algorithmChoice option of the encrypt operation. aes-256 is the default and the one to use unless an older reader must open the file. aes-128 is for readers that predate AES-256. rc4-128 and rc4-40 are broken ciphers kept only for very old readers. Each algorithm needs a minimum PDF version, and the file header is raised to it when the PDF is saved, never lowered. A PDF saved with RC4 is written with a classic cross-reference table so that those old readers can parse it.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 two_page_memo.pdf
const json = new JsonObject();
const jsonOut = new JsonObject();
const info = new JsonObject();
// AES-256, the default. Saved as PDF 1.7 with Adobe extension level 8.
const pdf1 = new Pdf();
try {
pdf1.loadFile('qa_data/pdf/two_page_memo.pdf');
} catch {
console.log(pdf1.lastErrorText);
return;
}
pdf1.userPassword = 'user-secret';
pdf1.ownerPassword = 'owner-secret';
json.updateString('algorithmChoice', 'aes-256');
try {
pdf1.protection('encrypt', json, jsonOut);
} catch {
console.log(pdf1.lastErrorText);
return;
}
console.log(`aes-256 will be saved as PDF ${jsonOut.stringOf('pending.pdfVersion')}, Adobe extension level ${jsonOut.intOf('pending.adobeExtensionLevel')}`);
try {
pdf1.writeFile('qa_output/memo_aes256.pdf');
} catch {
console.log(pdf1.lastErrorText);
return;
}
try {
pdf1.preLoadCheck('qa_output/memo_aes256.pdf', info);
} catch {
console.log(pdf1.lastErrorText);
return;
}
console.log(` saved file: ${info.stringOf('algorithm')}, PDF ${info.stringOf('pdfVersion')}`);
// AES-128, for readers that predate AES-256. Saved as PDF 1.6.
const pdf2 = new Pdf();
try {
pdf2.loadFile('qa_data/pdf/two_page_memo.pdf');
} catch {
console.log(pdf2.lastErrorText);
return;
}
pdf2.userPassword = 'user-secret';
pdf2.ownerPassword = 'owner-secret';
json.updateString('algorithmChoice', 'aes-128');
try {
pdf2.protection('encrypt', json, jsonOut);
} catch {
console.log(pdf2.lastErrorText);
return;
}
console.log(`aes-128 will be saved as PDF ${jsonOut.stringOf('pending.pdfVersion')}`);
try {
pdf2.writeFile('qa_output/memo_aes128.pdf');
} catch {
console.log(pdf2.lastErrorText);
return;
}
try {
pdf2.preLoadCheck('qa_output/memo_aes128.pdf', info);
} catch {
console.log(pdf2.lastErrorText);
return;
}
console.log(` saved file: ${info.stringOf('algorithm')}, PDF ${info.stringOf('pdfVersion')}`);
// RC4-128, a broken cipher, only for readers too old for AES. Saved as PDF 1.4.
const pdf3 = new Pdf();
try {
pdf3.loadFile('qa_data/pdf/two_page_memo.pdf');
} catch {
console.log(pdf3.lastErrorText);
return;
}
pdf3.userPassword = 'user-secret';
pdf3.ownerPassword = 'owner-secret';
json.updateString('algorithmChoice', 'rc4-128');
try {
pdf3.protection('encrypt', json, jsonOut);
} catch {
console.log(pdf3.lastErrorText);
return;
}
console.log(`rc4-128 will be saved as PDF ${jsonOut.stringOf('pending.pdfVersion')}`);
try {
pdf3.writeFile('qa_output/memo_rc4_128.pdf');
} catch {
console.log(pdf3.lastErrorText);
return;
}
try {
pdf3.preLoadCheck('qa_output/memo_rc4_128.pdf', info);
} catch {
console.log(pdf3.lastErrorText);
return;
}
console.log(` saved file: ${info.stringOf('algorithm')}, PDF ${info.stringOf('pdfVersion')}`);
// Sample output:
// aes-256 will be saved as PDF 1.7, Adobe extension level 8
// saved file: aes-256, PDF 1.7
// aes-128 will be saved as PDF 1.6
// saved file: aes-128, PDF 1.6
// rc4-128 will be saved as PDF 1.4
// saved file: rc4-128, PDF 1.4
}