【问题标题】:How can I add a password to a base 64 pdf?如何将密码添加到 base 64 pdf?
【发布时间】:2023-03-25 12:37:02
【问题描述】:

我有这种方法,可以在 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();
        }
    }

【问题讨论】:

    标签: java pdf itext7


    【解决方案1】:

    如何处理 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;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-14
      • 2019-04-07
      • 1970-01-01
      • 1970-01-01
      • 2017-07-20
      • 1970-01-01
      • 2023-01-26
      相关资源
      最近更新 更多