繁体   English   中英

如何将密码添加到 base 64 pdf?

[英]How can I add a password to a base 64 pdf?

我有这种方法,我在 pdf 中添加密码,但我是用计算机上的 pdf 来做的。 我想要尝试的是接收一个字符串作为输入参数,该字符串将是 base 64 中的 pdf 并响应 base64。

    public static void main(String[] args) {
            try {

                OutputStream file = new FileOutputStream(new File("D:\\Test.pdf"));

            Document document = new Document();
            PdfWriter writer = PdfWriter.getInstance(document, file);

            writer.setEncryption(USER_PASS.getBytes(), OWNER_PASS.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();

        } catch (Exception e) {

            e.printStackTrace();
        }
    }

如何处理 Base64 pdf 很复杂,因为这是第一次,但最终我能够开发出一种方法,您可以将密码添加到已经存在于 base64 中的 pdf 中。

   public String EncriptarPDFconContraseña(String pdfBase64, String passwordUser, String passwordOwner) throws IOException, DocumentException {

        PdfReader reader = new PdfReader(Base64.decode(pdfBase64));
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfStamper stamper = new PdfStamper(reader, baos);
        stamper.setEncryption(passwordUser.getBytes(), passwordOwner.getBytes(), PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);
        stamper.close();
        String base64 = Base64.encodeBytes(baos.toByteArray());

        return base64;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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