June 24, 2022

Password Protected PDF Using iText in Java

In this post we’ll see how to create a password protected PDF in Java using iText library and how to protect an already existing PDF with password.

Password protected PDF using iText

In order to create an encrypted PDF following steps are required.

  1. Using setStandardEncryption() method of the WriterProperties class you can set the encryption options for the document.
  2. Encryption constants are provided in EncryptionConstants class. Permissions and encryption algorithms provided in the EncryptionConstants class.
    • ALLOW_ASSEMBLY
    • ALLOW_COPY
    • ALLOW_DEGRADED_PRINTING
    • ALLOW_FILL_IN
    • ALLOW_MODIFY_ANNOTATIONS
    • ALLOW_MODIFY_CONTENTS
    • ALLOW_PRINTING
    • ALLOW_SCREENREADERS
    • ENCRYPTION_AES_128
    • ENCRYPTION_AES_256
    • STANDARD_ENCRYPTION_128
    • STANDARD_ENCRYPTION_40
  3. For encrypting PDF you will also need Bouncy Castle Provider. The Bouncy Castle Crypto package is a Java implementation of cryptographic algorithms. Maven dependency for the same is-
    <dependency>
      <groupId>org.bouncycastle</groupId>
      <artifactId>bcprov-jdk15on</artifactId>
      <version>1.61</version>
    </dependency>

Password protected PDF using iText Java Program

import java.io.IOException;
import com.itextpdf.kernel.pdf.EncryptionConstants;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.WriterProperties;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;

public class ReadPDF {
  public static final String CREATED_PDF = "F://knpcode//result//PP.pdf";
  final static String USER_PASSWORD = "user";
  final static String OWNER_PASSWORD = "owner";
  public static void main(String[] args) {
    try {
      PdfWriter writer = new PdfWriter(CREATED_PDF, new WriterProperties()
                    .setStandardEncryption(USER_PASSWORD.getBytes(), 
                    OWNER_PASSWORD.getBytes(), 
                    EncryptionConstants.ALLOW_PRINTING, 
                    EncryptionConstants.ENCRYPTION_AES_128 | EncryptionConstants.DO_NOT_ENCRYPT_METADATA));
      PdfDocument pdf = new PdfDocument(writer);	
      Document document = new Document(pdf); 
      document.add(new Paragraph("Password protected PDF where only content printing is permitted content can't be copied."));
      document.close();		
    }catch (IOException e) {
      System.out.println("Exception occurred " + e.getMessage());
    }
  }
}
password protected PDF

As you can see password is required to open the PDF. Once PDF is opened you can check that the content of the PDF can’t be copied.

Password protecting an already existing PDF

import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.kernel.pdf.EncryptionConstants;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.WriterProperties;

public class ReadPDF {
  public static final String SOURCE_PDF = "F://knpcode//result//List.pdf";
  public static final String RESULT_PDF = "F://knpcode//result//ListEncrypted.pdf";
  static final String USER_PASSWORD = "user";
  static final String OWNER_PASSWORD = "owner";
  public static void main(String[] args) {
    try {
      // Read existing PDF
      PdfReader reader = new PdfReader(SOURCE_PDF);
      WriterProperties props = new WriterProperties().setStandardEncryption(USER_PASSWORD.getBytes(), 
                          OWNER_PASSWORD.getBytes(), 
                          EncryptionConstants.ALLOW_PRINTING,
                          EncryptionConstants.ENCRYPTION_AES_128 | EncryptionConstants.DO_NOT_ENCRYPT_METADATA);
      // Write to PDF along with encryption properties
      PdfWriter writer = new PdfWriter(new FileOutputStream(RESULT_PDF), props);
      PdfDocument pdfDoc = new PdfDocument(reader, writer);
      pdfDoc.close();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}

That's all for the topic Password Protected PDF Using iText in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment