Created
January 22, 2024 05:34
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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