Reputation: 226
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 ) ?
Upvotes: 1
Views: 1932
Reputation: 1777
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)
Upvotes: 4