我正在使用Java中的Itext处理pdf文档。

它工作正常,但是我可以在不保存文件的情况下签署pdf文档吗?

这是代码的一部分:

 FileOutputStream os = (FileOutputStream) readWriteFiles(2);
        System.out.println("FileOutputStream created");

        if (os == null) {
            System.out.println("Operation canceled by the user. He chose to not overwrite existing file.");
            return;
        }

         PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0', null, true);
       // PdfStamper stamper = PdfStamper.createSignature;

        System.out.println("stamper created");


        /* Creating the appearance */
        PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
        appearance.setReason(reason);
        appearance.setLocation(location);

        /* Creating the signature */
        ExternalDigest digest = new BouncyCastleDigest();

        ExternalSignature signature =
                new PrivateKeySignature(pk, digestAlgorithm, provider);

        System.out.println(signature.toString() + "\n\n\n" + appearance.toString());
        MakeSignature.signDetached(appearance, digest, signature, chain,
                null, null, null, 0, subfilter);


操作系统包含输出pdf文件的路径。
当我尝试将os设置为null时,它没有签名。

我的问题是如何在不保存输出pdf文件的情况下对pdf进行签名?

以及如何才能以字节或流的形式获取PDF以便在javascript中使用它(它是一个applet)?

最佳答案

根据文档

公共静态PdfStamper createSignature(PdfReader阅读器,
                                         OutputStream os,
                                         char pdfVersion)
                                  抛出DocumentException,
                                         IOException

您可以提供所需的任何OutputStream,所以我建议您这样做

ByteArrayOutputStream output = new ByteArrayOutputStream();
PdfStamper stamper = PdfStamper.createSignature(reader, output, '\0', null, true);


然后,您可以将输出内容检索为字节数组(http://docs.oracle.com/javase/7/docs/api/java/io/ByteArrayOutputStream.html

08-06 15:49