PDF document encryption is a feature used to protect file contents. Encrypted documents require a password to open and view. This feature effectively prevents unauthorized access, copying and modification of PDF files. Free Spire.PDF for Java Supports programmatic encryption and decryption of PDF documents, and does not require the installation of third-party software during the process.
Program Environment
Before proceeding, please import the jar into the Java program. Please refer to the following two import methods:
Method 1: If you are using maven, you can import the jar file into the application by adding the following code to the project's pom.xml file.
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.pdf.free</artifactId> <version>5.1.0</version> </dependency> </dependencies>
Method 2: If you are not using maven, you can download Free Spire.PDF for Java from this link, find Spire.PDF.jar in the lib folder and unzip it; then Create a new project in IDEA, click "File", "Project Structure", "Modules", "Dependencies", and then click under the green " " on the right The first option is "jar files or paths" (JARs or Directories). Find the decompressed Spire.PDF.jar file and click Confirm to import it into the project.
Encrypt PDF document
Create a PdfDocument instance.
Use the PdfDocument.loadFromFile() method to load the PDF sample document.
Set the opening password, permission password, encryption key size and permissions.
Use the PdfDocument.getSecurity().encrypt(java.lang.String openPassword, java.lang.String permissionPassword, java.util.EnumSet
Use the PdfDocument.saveToFile() method to save the resulting document.
import java.util.EnumSet; import com.spire.pdf.PdfDocument; import com.spire.pdf.security.PdfEncryptionKeySize; import com.spire.pdf.security.PdfPermissionsFlags; public class EncryptPDF { public static void main(String[] args) { //创建PdfDocument实例 PdfDocument pdf = new PdfDocument(); //加载PDF示例文档 pdf.loadFromFile("sample.pdf"); //加密文档 PdfEncryptionKeySize keySize = PdfEncryptionKeySize.Key_128_Bit; String openPassword = "123456"; String permissionPassword = "abcdef"; EnumSet flags = EnumSet.of(PdfPermissionsFlags.Print, PdfPermissionsFlags.Fill_Fields); pdf.getSecurity().encrypt(openPassword, permissionPassword, flags, keySize); //保存文档 pdf.saveToFile("Encrypt.pdf"); pdf.close(); } }
Decrypt PDF document
Create a PdfDocument instance.
Use the PdfDocument.loadFromFile(java.lang.String filename, java.lang.String password) method to load the encrypted PDF document.
By using PdfDocument.getSecurity().encrypt(java.lang.String openPassword, java.lang.String permissionPassword, java.util.EnumSet
Use the PdfDocument.saveToFile() method to save the result file.
import com.spire.pdf.PdfDocument; import com.spire.pdf.security.PdfEncryptionKeySize; import com.spire.pdf.security.PdfPermissionsFlags; public class DecryptPDF { public static void main(String[] args) throws Exception { //创建PdfDocument实例 PdfDocument pdf = new PdfDocument(); //加载加密文档 pdf.loadFromFile("Encrypt.pdf", "123456"); //解密文档 pdf.getSecurity().encrypt("", "", PdfPermissionsFlags.getDefaultPermissions(), PdfEncryptionKeySize.Key_256_Bit, "abcdef"); //保存文档 pdf.saveToFile("Decrypt.pdf"); pdf.close(); }
The above is the detailed content of Java implements encryption or decryption method of PDF documents. For more information, please follow other related articles on the PHP Chinese website!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Chinese version
Chinese version, very easy to use

Dreamweaver Mac version
Visual web development tools