c# - iTextSharp sign PDF created in MemoryStream -


i need dynamically create pdf file, use memorystream , itextsharp editing pdf. want digitally sign pdf file before sending browser. i've seen in sample project itextsharp has pdfsigner class, work. i've seen it, works this:

pdfsigner signer = new pdfsigner(inputfilename, outputfilename, certificate, metadata); signer.sign(......) 

but memorystream, can't provide physical file name input or output either. code below:

memorystream memorystream = new memorystream(); var document = new document(itextsharp.text.pagesize.letter, 40, 40, 60, 40); pdfwriter.getinstance(document, memorystream).closestream = false;  document.open(); populatepdf(document, someid); document.close();  byte[] bytearray = memorystream.toarray();  memorystream.write(bytearray, 0, bytearray.length); memorystream.position = 0;  return new filestreamresult(memorystream, "application/pdf"); 

my question how can use pdfsigner object sign document, giving pdfsigner takes strings - filenames arguments.

as chris haas explained, there no class named pdfsigner in itext. may using unofficial version of itext, in case need make sure applying digital signature correctly.

you can read more correct digital signatures in book wrote digital signatures.

basically, how you'd sign using pkcs#12 keystore:

pkcs12store store = new pkcs12store(new filestream(keystore, filemode.open), password); string alias = ""; icollection<x509certificate> chain = new list<x509certificate>(); // searching private key foreach (string al in store.aliases)     if (store.iskeyentry(al) && store.getkey(al).key.isprivate) {         alias = al;         break;     } asymmetrickeyentry pk = store.getkey(alias); foreach (x509certificateentry c in store.getcertificatechain(alias))     chain.add(c.certificate); rsaprivatecrtkeyparameters parameters = pk.key rsaprivatecrtkeyparameters;  pdfreader reader = new pdfreader(src); filestream os = new filestream(dest, filemode.create); pdfstamper stamper = pdfstamper.createsignature(reader, os, '\0'); // creating appearance pdfsignatureappearance appearance = stamper.signatureappearance; appearance.reason = "my reason signing"; appearance.location = "the middle of nowhere"; appearance.setvisiblesignature(new rectangle(36, 748, 144, 780), 1, "sig"); // creating signature iexternalsignature pks = new privatekeysignature(pk, digestalgorithms.sha256); makesignature.signdetached(appearance, pks, chain, null, null, null, 0, cryptostandard.cms); 

obviously, want replace null values implementations revocation info , maybe allow timestamp, that's not question.

your question is: can provide pdf exists in memory? answer yes: src parameter when creating pdfreader instance can path file on disk, can byte[] obtained memorystream.


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -