Make PDF Files Password Protected Using Java

Table of Contents


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.


Java 1.8+ (11 – 16), itextpdf, 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=""







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,

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

	FileOutputStream os = new FileOutputStream(destFile);;

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


Leave a Reply

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