签署PDF文档并以字节或流的形式获取pdf

时间:2014-01-08 07:18:27

标签: java javascript pdf applet itext

我正在使用Java中的Itext开发sign 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);

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

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

如何将PDF作为字节或流来获取以便在javascript(它是applet)中使用它?

1 个答案:

答案 0 :(得分:4)

根据文件

public static PdfStamper createSignature(PdfReader reader,                                          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