Add Password Protection to PDF using Java
//Please include following jars //itextpdf-5.2.1.jar //bcmail-jdk16-1.46.jar //bcprov-jdk16-1.46.jar //bctsp-jdk16-1.46.jar //------------------------------------- import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; import java.util.Date; import com.itextpdf.text.Document; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfWriter; public class GeneratePDF { public static void main(String[] args) { try { System.out.println("--Process started--"); String usrPwd = "hello"; String authorPwd = "hello123"; OutputStream file = new FileOutputStream(new File( "c:\\prd\\Test_pdf_file.pdf")); Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, file); writer.setEncryption(usrPwd.getBytes(), authorPwd.getBytes(), PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128); document.open(); document.add(new Paragraph("Hello World, iText")); document.add(new Paragraph(new Date().toString())); document.close(); file.close(); System.out.println("--Process ended--"); } catch (Exception e) { e.printStackTrace(); } } }
how to open password protected pdf files programatically in java with user password..