React Native Requires Chilkat v11.7.0+
React Native
Restrict Printing, Copying and Editing of a PDF
See more PDF Protection Examples
Demonstrates a PDF that opens without a password but limits what a reader may do with it. The user password is left empty, the owner password protects the restrictions, and the permissions member of the JSON says what is denied. Chilkat honors these permissions itself: with copying denied, text extraction is refused unless the PDF is loaded with its owner password. An empty owner password is refused when anything is restricted, because anyone opening the file would then have owner rights.Chilkat React Native Downloads
import { JsonObject, Pdf, StringBuilder } 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 pdf = new Pdf();
try {
pdf.loadFile('qa_data/pdf/two_page_memo.pdf');
} catch {
console.log(pdf.lastErrorText);
return;
}
// No user password: the PDF opens without a prompt. The owner password is what protects the
// restrictions, so it must not be empty when anything is restricted.
pdf.userPassword = '';
pdf.ownerPassword = 'owner-secret';
const json = new JsonObject();
json.updateString('permissions.print', 'lowQuality');
json.updateBool('permissions.copy', false);
json.updateBool('permissions.modify', false);
json.updateBool('permissions.annotate', false);
// The permissions not mentioned (fillForms, assemble, accessibility) stay granted.
const jsonOut = new JsonObject();
try {
pdf.protection('encrypt', json, jsonOut);
} catch {
console.log(pdf.lastErrorText);
return;
}
try {
pdf.writeFile('qa_output/memo_restricted.pdf');
} catch {
console.log(pdf.lastErrorText);
return;
}
// Anyone can open the result, with the rights of a user.
const reader = new Pdf();
try {
reader.loadFile('qa_output/memo_restricted.pdf');
} catch {
console.log(reader.lastErrorText);
return;
}
const empty = new JsonObject();
const info = new JsonObject();
try {
reader.protection('getInfo', empty, info);
} catch {
console.log(reader.lastErrorText);
return;
}
console.log(`opened with: ${info.stringOf('loaded.openedWith')}`);
console.log(`print: ${info.stringOf('loaded.permissions.print')}`);
console.log(`copy: ${info.boolOf('loaded.permissions.copy')}`);
console.log(`modify: ${info.boolOf('loaded.permissions.modify')}`);
// Chilkat honors the restrictions. Copying is not permitted, so the text cannot be extracted.
const opts = new JsonObject();
const extractOut = new JsonObject();
const sb = new StringBuilder();
try {
reader.extractTextSb('text', opts, sb, extractOut);
} catch {
console.log('Text extraction was refused: the PDF does not permit copying.');
}
// The owner password lifts every restriction.
const owner = new Pdf();
owner.ownerPassword = 'owner-secret';
try {
owner.loadFile('qa_output/memo_restricted.pdf');
} catch {
console.log(owner.lastErrorText);
return;
}
const ownerInfo = new JsonObject();
try {
owner.protection('getInfo', empty, ownerInfo);
} catch {
console.log(owner.lastErrorText);
return;
}
console.log(`opened with: ${ownerInfo.stringOf('loaded.openedWith')}`);
try {
owner.extractTextSb('text', opts, sb, extractOut);
} catch {
console.log(owner.lastErrorText);
return;
}
console.log(`characters extracted with the owner password: ${sb.length}`);
// Sample output:
// opened with: emptyPassword
// print: lowQuality
// copy: False
// modify: False
// Text extraction was refused: the PDF does not permit copying.
// opened with: owner
// characters extracted with the owner password: 1048
}