title: How to make pdf file password protected?
body: <p>I want to make pdf file password protected. I just goolge it for the same and find a good solution given below. It's working fine But it wipe out all the data which is already there in my pdf after i secure pdf using below given code.</p>
  
  <p>Used jar files for this code are:</p>
  
  <p>itextpdf-5.2.1.jar</p>
  
  <p>bcmail-jdk16-1.46.jar</p>
  
  <p>bcprov-jdk16-1.46.jar</p>
  
  <p>bctsp-jdk16-1.46.jar</p>
  
  <p>Code to secure PDF :</p>
  
  <pre><code>import com.itextpdf.text.Document;
  import com.itextpdf.text.DocumentException;
  import com.itextpdf.text.Paragraph;
  import com.itextpdf.text.pdf.PdfWriter;
  import java.io.FileNotFoundException;
  import java.io.FileOutputStream;
  import java.io.IOException;
  
  public class Secure_file {
      private static String USER_PASSWORD = "password";
      private static String OWNER_PASSWORD = "secured";
      public static void main(String[] args) throws IOException {
          Document document = new Document();
          try {
              PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("E:\\sample.pdf"));
              writer.setEncryption(USER_PASSWORD.getBytes(),OWNER_PASSWORD.getBytes(), PdfWriter.ALLOW_PRINTING,PdfWriter.ENCRYPTION_AES_128);
              document.open();
              document.add(new Paragraph("This is Password Protected PDF document."));
              document.close();
              writer.close();
          } catch (DocumentException e) {
              e.printStackTrace();
          } catch (FileNotFoundException e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>what changes i need to made in this program ?</p>