import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfEncryptor;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;

public class EncryptedTest {

	public static void main(String[] args) throws Exception {
		String pdfEncrypted = "C:\\temp\\pdf\\encrypt.pdf";
		String pdfDecrypted = "C:\\temp\\pdf\\decrypt.pdf";
		makePDF(pdfEncrypted);					//encrypt
		readPDF(pdfEncrypted, pdfDecrypted); 	//decrypt and save
	}

	public static void makePDF(String pdfEncrypted) {
		Document document = new Document();

		try {
			PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdfEncrypted));
			String userPassword = "user", ownerPassword = "owner";
			int permissions = PdfWriter.AllowPrinting;
			// encrypt with PW !
			writer.setEncryption(PdfWriter.STRENGTH128BITS,  userPassword, ownerPassword, permissions);
			document.open();
			document.add(new Paragraph("Hallo Encrypted PDF !!!", FontFactory.getFont(FontFactory.COURIER, 8, Font.NORMAL)));
		} catch (Exception e) {
			e.printStackTrace();
		}
		document.close();
	}

	public static void readPDF(String pdfEncrypted, String pdfDecrypted) {
		try {
			PdfReader reader = new PdfReader(pdfEncrypted);	//readPDF without PW
			PdfEncryptor.encrypt(reader, new FileOutputStream(pdfDecrypted), null, null, PdfWriter.AllowPrinting, false); // save again without password
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}