skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Saturday, October 13, 2012

Add Password Protection to PDF using java

Posted by Raju Gupta at 6:00 AM – 1 comments
 
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();
  }
 }
}


Labels: iText Example , PDF File Example

One Response so far.

  1. Viswateja says:
    February 5, 2013 at 12:39 PM

    how to open password protected pdf files programatically in java with user password..

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger