Make PDF Files Password Protected Using Java

Table of Contents

Introduction

This example is going to show you how to make pdf files password protected using Java programming language. Many applications require dynamic generation of PDF documents for reporting purpose, such as, generating statements for customers, readers buying online specific chapters of a book, etc. For example, you want to send bank statement in pdf format and this pdf file has to be password protected otherwise it may land in a wrong hand.

It is also a good idea to make the PDF file password protected when you want to send some sensitive data into the PDF file over the network, such as, over the email or you are allowing end users to download the PDF files. For example, you want to send OTP in pdf document in the email attachment and this pdf document should be password protected otherwise OTP may land in the wrong hand.

Related Posts:

PDF files can be useful for various reasons:

  • layout is uniform across different computer systems
  • tend to have a small file size in comparison to other file formats
  • can be protected by password
  • different levels of access can be set based on requirements
  • works uniformly across different operating systems
  • graphics or images, links, etc. can be easily integrated

I am goin to use itextpdf and Apache’s pdfbox libraries to make the pdf files password protected using Java language. You can use any one of them. Remember when you use itextpdf library, you also need to use bcprov-jdk15on (bouncycastle) library.

Prerequisites

Java 1.8+ (11 – 16), itextpdf 5.5.13.3, bcprov-jdk15on 1.70, pdfbox 3.0.0-RC1, Maven 3.8.2

Project Setup

You can create maven based project in your favorite IDE or tool. The following pom.xml file could be used for your project.

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.roytuts</groupId>
	<artifactId>java-password-protected-pdf-file</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>11</maven.compiler.source>
		<maven.compiler.target>11</maven.compiler.target>
	</properties>

	<dependencies>
		<dependency>
			<groupId>com.itextpdf</groupId>
			<artifactId>itextpdf</artifactId>
			<version>5.5.13.3</version>
		</dependency>

		<dependency>
			<groupId>org.bouncycastle</groupId>
			<artifactId>bcprov-jdk15on</artifactId>
			<version>1.70</version>
		</dependency>

		<dependency>
			<groupId>org.apache.pdfbox</groupId>
			<artifactId>pdfbox</artifactId>
			<version>3.0.0-RC1</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.1</version>
			</plugin>
		</plugins>
	</build>
</project>

Protect PDF using Password

I am going to show you how to use both itextpdf and Apache’s pdfbox libraries to encrypt the pdf files using password.

The following code example uses itextpdf library to make the pdf document password protected:

public static void protectPdf(String sourceFile, String destFile) throws IOException, DocumentException {
	PdfReader reader = new PdfReader(sourceFile);
	PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(destFile));
	stamper.setEncryption("roytuts".getBytes(), "roytuts".getBytes(), PdfWriter.ALLOW_PRINTING,
			PdfWriter.ENCRYPTION_AES_128 | PdfWriter.DO_NOT_ENCRYPT_METADATA);
	stamper.close();
	reader.close();
}

The important line in the above code snippets is setEncryption() method, where, the first argument is user password, the second argument is the owner password and both can be null. The third argument you can specify what operation for the pdf file can be performed, such as, printing, modifying, editing, etc. to the pdf document. The fourth argument is encryption type.

The following code snippet is used for protecting pdf document using Apache’s pdfbox library:

public static void protectPdfBox(String sourceFile, String destFile) throws IOException {
	PDDocument document = Loader.loadPDF(new File(sourceFile));
	AccessPermission ap = new AccessPermission();
	StandardProtectionPolicy spp = new StandardProtectionPolicy("roytuts", "roytuts", ap);
	spp.setEncryptionKeyLength(128);
	//spp.setPermissions(ap);
	document.protect(spp);

	FileOutputStream os = new FileOutputStream(destFile);
	document.save(os);
	document.close();
}

The constructor StandardProtectionPolicy() takes a number of arguments for password and permissions.

Testing Encrypted PDF Files

Once you execute any one of the above code snippets using main class or Junit test class, you will be asked to input password while trying to open the pdf files.

password protected pdf files

That’s how you can protect your pdf documents when the file has sensitive information. Note that you should use some hard password for your pdf files.

Source Code

Download

Leave a Reply

Your email address will not be published. Required fields are marked *