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