Home  >  iText

Add Password and Encrypt-Decrypt PDF using iText in Java

By Arvind Rai, February 07, 2015
In this page we will learn how to use iText API to add password in PDF and how to encrypt and decrypt existing PDF to write and read data respectively. PdfWriter.setEncryption() method is used to add password to new PDF. While creating password protected PDF, we need to add user password, owner password, permission and encryption type. To encrypt existing PDF with password, iText provides PdfStamper.setEncryption which accepts same arguments as PdfWriter.setEncryption.

PdfWriter.setEncryption(byte[] userPassword, byte[] ownerPassword, int permissions, int encryptionType)

To add password to a new PDF, we need to use PdfWriter.setEncryption() method. This method accepts arguments

User password as bytes
Owner Password bytes
Permission for PDF
Encrption Type

We can set different type of permissions to PDF

PdfWriter.ALLOW_PRINTING
PdfWriter.ALLOW_COPY

There are different encryption type as given below.

ENCRYPTION_AES_128
STANDARD_ENCRYPTION_128
STANDARD_ENCRYPTION_40
ENCRYPTION_AES_256


PdfStamper.setEncryption(byte[] userPassword, byte[] ownerPassword, int permissions, int encryptionType)

PdfStamper API is used when we need to protect existing PDF. While instantiating PdfStamper, it accepts source file as PdfReader and destination file as OutputStream. PdfStamper can add some extra content in PDF while writing to destination file. PdfStamper.setEncryption uses the same arguments as PdfWriter.setEncryption while setting the encryption to existing PDF.

Add Password to PDF

This program will show how to create a PDF file with password.
CreatePDFwithPassword.java
package com.concretepage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
public class CreatePDFwithPassword {
   public static void main(String[] args) throws DocumentException, MalformedURLException, IOException {
	Document document = new Document();
	PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("PDFwithPasswordOne.pdf"));
	writer.setEncryption("concretepage".getBytes(), "cp123".getBytes(), PdfWriter.ALLOW_COPY, PdfWriter.STANDARD_ENCRYPTION_40);
        writer.createXmpMetadata();
        document.open();
        document.add(new Paragraph("This is create PDF with Password demo."));
	document.close();
	System.out.println("Done");
   }
} 
Here in this program we have added user password as "concretepage" and owner password as "cp123". Find the output.
Add Password and Encrypt-Decrypt PDF using iText in Java
To open this PDF we need to use password as "concretepage".

Decrypt Password Protected PDF

In this example we will read a PDF which is password protected. While reading such PDF, we need owner password.
ReadPasswordProtectedPDF.java
package com.concretepage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
public class ReadPasswordProtectedPDF {
	public static void main(String[] args) throws FileNotFoundException, DocumentException, IOException {
	   PdfReader reader = new PdfReader("PDFwithPasswordOne.pdf", "cp123".getBytes());
	   PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("NoPasswordPDF.pdf"));
	   stamper.close();
	   reader.close();
	   System.out.println("Done");
	}
} 
In the example, after reading password protected PDF, we are writing content to new unprotected PDF.

Encrypt Unprotected PDF with Password

Here we will read an unprotected PDF and write data to another password protected PDF.
ReadPDFtoAddPassword.java
package com.concretepage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfWriter;
public class ReadPDFtoAddPassword {
	public static void main(String[] args) throws FileNotFoundException, DocumentException, IOException {
	      PdfReader reader = new PdfReader("NoPasswordPDF.pdf");
	      PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("PDFwithPasswordTwo.pdf"));
	      stamper.setEncryption("pwd123".getBytes(), "cp123".getBytes(),PdfWriter.ALLOW_COPY, PdfWriter.ENCRYPTION_AES_256);
	      stamper.close();
	      reader.close();
	      System.out.println("Done");
	}
} 

build.gradle

To resolve JAR for iText, bcprov-jdk15on and bcpkix-jdk15on, we need to use below gradle file.
build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
archivesBaseName = 'ITEXT'
version = '1' 
repositories {
    mavenCentral()
}
dependencies {
    compile 'com.itextpdf:itextpdf:5.5.4'
    compile 'org.bouncycastle:bcprov-jdk15on:1.51'
    compile 'org.bouncycastle:bcpkix-jdk15on:1.51'
} 

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us