JavaScript Requires Chilkat v11.7.0+
JavaScript
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.
Note
This example is intended for running within a Chilkat.Js embedded JavaScript engine. All Chilkat JavaScript examples require Chilkat
v11.4.0 or greater.
var success = false;
// 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
var pdf = new CkPdf();
success = pdf.LoadFile("qa_data/pdf/two_page_memo.pdf");
if (success == false) {
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";
var json = new CkJsonObject();
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.
var jsonOut = new CkJsonObject();
success = pdf.Protection("encrypt",json,jsonOut);
if (success == false) {
console.log(pdf.LastErrorText);
return;
}
success = pdf.WriteFile("qa_output/memo_restricted.pdf");
if (success == false) {
console.log(pdf.LastErrorText);
return;
}
// Anyone can open the result, with the rights of a user.
var reader = new CkPdf();
success = reader.LoadFile("qa_output/memo_restricted.pdf");
if (success == false) {
console.log(reader.LastErrorText);
return;
}
var empty = new CkJsonObject();
var info = new CkJsonObject();
success = reader.Protection("getInfo",empty,info);
if (success == false) {
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.
var opts = new CkJsonObject();
var extractOut = new CkJsonObject();
var sb = new CkStringBuilder();
success = reader.ExtractTextSb("text",opts,sb,extractOut);
if (success == false) {
console.log("Text extraction was refused: the PDF does not permit copying.");
}
// The owner password lifts every restriction.
var owner = new CkPdf();
owner.OwnerPassword = "owner-secret";
success = owner.LoadFile("qa_output/memo_restricted.pdf");
if (success == false) {
console.log(owner.LastErrorText);
return;
}
var ownerInfo = new CkJsonObject();
success = owner.Protection("getInfo",empty,ownerInfo);
if (success == false) {
console.log(owner.LastErrorText);
return;
}
console.log("opened with: " + ownerInfo.StringOf("loaded.openedWith"));
success = owner.ExtractTextSb("text",opts,sb,extractOut);
if (success == false) {
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