Unlocking a password-protected PDF using the iText library

iText is an excellent Java-PDF library that allows Java developers to easily generate and manipulate PDFs. Here is a simple code example that unlocks a password-protected PDF...

import com.lowagie.text.pdf.PdfEncryptor;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;

public class UnlockPDF
{
   public static void main(String[] args) throws Exception
   {           
      String lockedFile = "locked.pdf"                // Locked pdf filename
      String password = "p@ssw0rd";              // Locked pdf password
      String unlockedFile = "unlocked.pdf";      // Filename of the new unlocked pdf
       
      PdfReader reader = new PdfReader(lockedFile, password.getBytes());

      System.out.println("Unlocking...");

      PdfEncryptor.encrypt(reader, new FileOutputStream("unlocked.pdf"), null,
         null, PdfWriter.AllowAssembly | PdfWriter.AllowCopy
         | PdfWriter.AllowDegradedPrinting | PdfWriter.AllowFillIn
         | PdfWriter.AllowModifyAnnotations | PdfWriter.AllowModifyContents
         | PdfWriter.AllowPrinting | PdfWriter.AllowScreenReaders, false);
               
      System.out.println("PDF Unlocked!");     
   }
}

Comments

Is there anyway I can produce a owner Password protected PDF?????

Please can you send me the answer at hanzalah@gmail.com

many thanks!