I need to dynamically create a PDF file, so I use a MemoryStream and iTextSharp for editing the PDF. But I want to digitally sign the PDF file before sending it to the browser. I've seen in a sample project that iTextSharp has a PDFSigner class, which mostly does all the work. But as I've seen it, it works like this:
PDFSigner signer = new PDFSigner(inputFileName, outputFileName, certificate, metaData);
signer.Sign(......)
But with MemoryStream, I can't provide a physical file name for input or output either. My code is 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 is how can I use a PDFSigner object to sign the document, giving that PDFSigner takes only strings - filenames as arguments.
PDFSigner, at least none that I could find. Did you find this code via an online tutorial? Also, what version of iTextSharp as you using? - Chris Haas