Protect PDF via Java

Set document privileges for PDF with AES-128 encryption. Use Aspose.PDF for Java to modify PDF documents programmatically

How to Protect PDF File Using Using Java Tool

In order to protect PDF, we’ll use Aspose.PDF for Java API which is a feature-rich, powerful and easy to use conversion API for Java platform. You can download its latest version directly from Maven and install it within your Maven-based project by adding the following configurations to the pom.xml.

Repository

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java AP</name>
    <url>https://releases.aspose.com/java/repo/</url>
</repository>

Dependency

<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>version of aspose-pdf API</version>
</dependency>

Protect PDF via Java


You need Aspose.PDF for Java to try the code in your environment.

  1. Load the PDF with an instance of Document.
  2. Create an object of DocumentPrivilege & set options.
  3. Call Document.Encrypt method to protect PDF with AES-128.
  4. Save result in PDF format

Protect PDF - Java

This sample code shows how to protect PDF File


    // open PDF File
    Document pdfFile = new Document("sourceFile.pdf");
    // encrypt PDF
    pdfFile.encrypt("user", "owner", 0, CryptoAlgorithm.AESx256);
    // save updated PDF
    pdfFile.save("EncryptedFile.pdf");