Skip to content

Instantly share code, notes, and snippets.

Code to Protect PDF with Password in Java. For more information, please follow link: https://kb.conholdate.com/total/java/how-to-protect-pdf-with-password-in-java/
import com.aspose.pdf.CryptoAlgorithm;
import com.aspose.pdf.Document;
import com.aspose.pdf.License;
public class ProtectPDFWithPasswordInJava {
public static void main(String[] args) throws Exception { // main method to protect PDF with password in Java
// Instantiate the license to avoid trial version limitations and watermark in the protected PDF
License asposePdfLicenseProtect = new License();
asposePdfLicenseProtect.setLicense("Aspose.pdf.lic");
// Open document
Document documentToProtect = new Document("Input.pdf");
// Encrypt PDF
documentToProtect.encrypt("user", "owner", com.aspose.pdf.facades.DocumentPrivilege.getForbidAll(), CryptoAlgorithm.RC4x128 , false);
// Save updated PDF
documentToProtect.save("Password PDF.pdf");
System.out.println("Done");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment