简体   繁体   中英

Sign PDF document and get the pdf as bytes or stream

I'm working on sign pdf document using Itext in Java .

it works fine but can i sign the pdf Document without save the file ?

here's part of the code :

 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 is include the path of the output pdf file . and when i try to put the os as null it didn't sign .

my Question is how to sign the pdf without save the output pdf file ?

and how can i get the PDF as bytes or stream in order to use it in javascript ( it's an applet ) ?

According to the documentation

public static PdfStamper createSignature(PdfReader reader, OutputStream os, char pdfVersion) throws DocumentException, IOException

You can give any OutputStream you want, so I suggest you do

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

Then you can retrieve the content of output as an array of bytes ( http://docs.oracle.com/javase/7/docs/api/java/io/ByteArrayOutputStream.html )

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