search
HomeJavajavaTutorialJava implements encryption or decryption method of PDF documents

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 permissions, PdfEncryptionKeySize keySize) method to encrypt PDF files.

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();

    }

}

Java implements encryption or decryption method of PDF documents

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 permissions, PdfEncryptionKeySize keySize, java.lang.String originalPermissionPassword) Method to decrypt PDF files.

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();
    }

Java implements encryption or decryption method of PDF documents

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!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

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

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

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

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

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

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

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]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

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

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools