简体   繁体   中英

iText5 how to make password protected PDF Stream

this is my Code:

public class PasswordToPDF {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        try {
            PdfReader pdfReader = new PdfReader("C:/Users/XXXX/Desktop/CPIPDF.pdf");
            PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream("C:/Users/XXXX/Desktop/test.pdf"));
            pdfStamper.setEncryption("abc".getBytes(), "abc".getBytes(), PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);
            pdfStamper.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
}

It works fine for Documents in Filesystem, but how can I transform it to work with PDF streams?

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?

        try {
            
            
            String pdfFileOutputPath = "C:/Users/XXXX/Desktop/test.pdf";
            String pdfFileInputPath = "C:/Users/XXXX/Desktop/CPIPDF.pdf";
            
            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 = "a";
            String ownerPassword = "b";

            pdfWriter.setEncryption(userPassword.getBytes(),
                         ownerPassword.getBytes(), PdfWriter.AllowPrinting,
                         PdfWriter.ENCRYPTION_AES_128);

            //document.open();
           
            //document.add(new Paragraph("This is Password protected PDF file"));

            //document.close();
            
            in.close();
            fos.close();

            System.out.println("PDF created in >> " + pdfFileOutputPath);

     } catch (Throwable e) {
            e.printStackTrace();
     }
public class PasswordToPDF {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        try {
            
    
            byte[] bytesDecoded;
            
            String pdfFileInputPath = "C:/Users/XXXX/Desktop/CPIPDF.pdf";
            
            InputStream in = new FileInputStream(pdfFileInputPath);
            bytesDecoded = in.readAllBytes();
            
            ByteArrayInputStream inStream = new ByteArrayInputStream(bytesDecoded);
            PdfReader pdfReader = new PdfReader(inStream);
            PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream("C:/Users/XXXXX/Desktop/test.pdf"));
            pdfStamper.setEncryption("abc".getBytes(), "abc".getBytes(), PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);
            pdfStamper.close();
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
}

it Works!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM