title: getting error while making a pdf as password protected in java
body: <p>I have developed a java mail api program which will send the mail and it also attaches the pdf file , so finally a mail is delivered in which pdf file is attached and that pdf will be password protected so for that i am using itext library </p>
  
  <p>so I have develop this code below </p>
  
  <pre><code>private static final byte[]  USER = "password 1234".getBytes();
  private static final byte[] OWNER =  "password 1234".getBytes();
  
  
    //  attachment part
               MimeBodyPart attachPart = new MimeBodyPart();
               String filename = "c:\\index.pdf";
  
               PdfReader reader = new PdfReader(filename);
               PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(filename));
               stamper.setEncryption(USER, OWNER,
                       PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128 | PdfWriter.DO_NOT_ENCRYPT_METADATA);
                   stamper.close();
                   reader.close();
               DataSource source = new FileDataSource(filename);
               attachPart.setDataHandler(new DataHandler(source));
               attachPart.setFileName(filename);
  </code></pre>
  
  <p>but i am getting this below error  ,please advise how to proceed</p>
  
  <pre><code>Exception in thread "main" java.io.FileNotFoundException: c:\index.pdf (The requested operation cannot be performed on a file with a user-mapped section open)
  </code></pre>
  
  <p>now can you please advise i want to make that pdf file as password protected through my java program itself for example i want to modify my below program such as that for opening an pdf file password 1234 is created and whenever an mail is sent the client should open the pdf file but before opening he should enter 1234 in the pop up box of pdf file to see it , can you please advise how can i achieve this in java program itself please. Thanks in advance below is my java program</p>