Java Requires Chilkat v11.7.0+
Chilkat2 Java
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 Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.Pdf;
import com.chilkatsoft.chilkat2.StringBuilder;
public class ChilkatExample {
public static void main(String[] args) {
// 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
Pdf pdf = new Pdf();
try {
pdf.loadFile("qa_data/pdf/two_page_memo.pdf");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
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.setUserPassword("");
pdf.setOwnerPassword("owner-secret");
JsonObject 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.
JsonObject jsonOut = new JsonObject();
try {
pdf.protection("encrypt", json, jsonOut);
pdf.writeFile("qa_output/memo_restricted.pdf");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Anyone can open the result, with the rights of a user.
Pdf reader = new Pdf();
try {
reader.loadFile("qa_output/memo_restricted.pdf");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
JsonObject empty = new JsonObject();
JsonObject info = new JsonObject();
try {
reader.protection("getInfo", empty, info);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("opened with: " + info.stringOf("loaded.openedWith"));
System.out.println("print: " + info.stringOf("loaded.permissions.print"));
System.out.println("copy: " + info.boolOf("loaded.permissions.copy"));
System.out.println("modify: " + info.boolOf("loaded.permissions.modify"));
// Chilkat honors the restrictions. Copying is not permitted, so the text cannot be extracted.
JsonObject opts = new JsonObject();
JsonObject extractOut = new JsonObject();
StringBuilder sb = new StringBuilder();
try {
reader.extractTextSb("text", opts, sb, extractOut);
} catch (ChilkatException e) {
System.out.println("Text extraction was refused: the PDF does not permit copying.");
}
// The owner password lifts every restriction.
Pdf owner = new Pdf();
owner.setOwnerPassword("owner-secret");
try {
owner.loadFile("qa_output/memo_restricted.pdf");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
JsonObject ownerInfo = new JsonObject();
try {
owner.protection("getInfo", empty, ownerInfo);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("opened with: " + ownerInfo.stringOf("loaded.openedWith"));
try {
owner.extractTextSb("text", opts, sb, extractOut);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("characters extracted with the owner password: " + sb.getLength());
// 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
}
}