Java Requires Chilkat v11.7.0+
Chilkat2 Java
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 Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.Pdf;
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 encrypted_user_password.pdf
Pdf pdf = new Pdf();
// Removing the encryption needs owner rights: load with the owner password.
pdf.setOwnerPassword("owner-secret");
try {
pdf.loadFile("qa_data/pdf/encrypted_user_password.pdf");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
JsonObject json = new JsonObject();
JsonObject jsonOut = new JsonObject();
try {
pdf.protection("getInfo", json, jsonOut);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("encrypted, as loaded: " + jsonOut.boolOf("loaded.encrypted"));
System.out.println("algorithm: " + jsonOut.stringOf("loaded.algorithm"));
try {
pdf.protection("removeEncryption", json, jsonOut);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("will be saved encrypted: " + jsonOut.boolOf("pending.encrypted"));
try {
pdf.writeFile("qa_output/minutes_unprotected.pdf");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// The result is an ordinary PDF that opens without a password.
Pdf reader = new Pdf();
JsonObject info = new JsonObject();
try {
reader.preLoadCheck("qa_output/minutes_unprotected.pdf", info);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("encrypted: " + info.boolOf("encrypted"));
try {
reader.loadFile("qa_output/minutes_unprotected.pdf");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("pages: " + reader.getNumPages());
// Sample output:
// encrypted, as loaded: True
// algorithm: rc4-128
// will be saved encrypted: False
// encrypted: False
// pages: 1
}
}