Showing posts with label PDF generation with password. Show all posts
Showing posts with label PDF generation with password. Show all posts

Thursday, December 15, 2011

Password Protected PDF

I got a time to post the important feature for web applications. User need to access statements through PDF. But that PDF needs to be Password protected.

To open the PDF, we can have different Password implemenations like (telephone no, DOB,Bank id, Last 4 digit of account no and name Etc...)

Now i am implementing password feature through the Random number Genaration.

import java.io.FileOutputStream;
import java.util.Random;

import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

public class ProtectPDFPassword {

public static byte[] UserPassword= "UserPassword".getBytes();
public static byte[] OwnerPassword = "OwnerPassword".getBytes();
public static void main(String[] args){
try {

Document Document_For_Protection = new Document();
PdfWriter EncryptPDF= PdfWriter.getInstance(Document_For_Protection, new FileOutputStream(generatePin()+".pdf"));
EncryptPDF.setEncryption(UserPassword, String.valueOf(generatePin()).getBytes(),
PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128);
EncryptPDF.createXmpMetadata();
Document_For_Protection.open();
Document_For_Protection.add(new Paragraph("Some Contents for Password Protection"));
Document_For_Protection.add(new Paragraph("Password is :"+"password"));
Document_For_Protection.close();
}
catch (Exception i)
{
System.out.println(i);
}
}

public static int generatePin() throws Exception {
Random generator = new Random();
generator.setSeed(System.currentTimeMillis());
int num = generator.nextInt(99999) + 99999;
System.out.println("Random number value---" + num);

if (num < 100000 || num > 999999) {
num = generator.nextInt(99999) + 99999;

if (num < 100000 || num > 999999) {

throw new Exception("Unable to generate PIN at this time..");
}
}
return num;
}

Required Jars's


itext-5.0.2.jar
bcprov-jdk15-136.jar

AddToAny

Contact Form

Name

Email *

Message *