title: iText5 how to make password protected PDF Stream
body: <p>this is my Code:</p>
  <pre><code>public class PasswordToPDF {
  
      public static void main(String[] args) {
          // TODO Auto-generated method stub
  
          try {
              PdfReader pdfReader = new PdfReader(&quot;C:/Users/XXXX/Desktop/CPIPDF.pdf&quot;);
              PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream(&quot;C:/Users/XXXX/Desktop/test.pdf&quot;));
              pdfStamper.setEncryption(&quot;abc&quot;.getBytes(), &quot;abc&quot;.getBytes(), PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);
              pdfStamper.close();
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
      
  }
  </code></pre>
  <p>It works fine for Documents in Filesystem, but how can I transform it to work with PDF streams?</p>
  <p>Unfortunately, no password protection is created for reading and writing with data streams. I would like to return the PDF as a password-protected data stream. is that possible?</p>
  <pre><code>        try {
              
              
              String pdfFileOutputPath = &quot;C:/Users/XXXX/Desktop/test.pdf&quot;;
              String pdfFileInputPath = &quot;C:/Users/XXXX/Desktop/CPIPDF.pdf&quot;;
              
              InputStream in = new FileInputStream(pdfFileInputPath);
             
              
              OutputStream fos = new FileOutputStream(new File(pdfFileOutputPath));
              
              in.transferTo(fos);
              
              
              Document document = new Document();
              
              PdfWriter pdfWriter = PdfWriter.getInstance(document, fos);
              
  
              String userPassword = &quot;a&quot;;
              String ownerPassword = &quot;b&quot;;
  
              pdfWriter.setEncryption(userPassword.getBytes(),
                           ownerPassword.getBytes(), PdfWriter.AllowPrinting,
                           PdfWriter.ENCRYPTION_AES_128);
  
              //document.open();
             
              //document.add(new Paragraph(&quot;This is Password protected PDF file&quot;));
  
              //document.close();
              
              in.close();
              fos.close();
  
              System.out.println(&quot;PDF created in &gt;&gt; &quot; + pdfFileOutputPath);
  
       } catch (Throwable e) {
              e.printStackTrace();
       }
  </code></pre>