It came to me to rework old code which signs PDF files into new one, which signs MemoryStreams (byte arrays) that come and are sent by web services. Simple, right? Well, tha
Not specific to your signing code, but when working with MemoryStream
and PdfStamper
, follow this general pattern:
using (MemoryStream ms = new MemoryStream()) {
using (PdfStamper stamper = new PdfStamper(reader, ms, '\0', true)) {
// do stuff
}
return ms.ToArray();
}
MemoryStream
implements IDisposable
, so include a using
statement.PdfStamper
using
statement takes care of disposing the object, so you don't need to call Close()
, and don't need to set the CloseStream
property.PdfStamper
using
statement, so your MemoryStream
is effectively a no-op. Return the byte array outside of the PdfStamper
using
statement, and inside the MemoryStream
using
statement.MemoryStream
Position
property.PdfStamper
constructor above - it's from some test code I had for filling forms, and use whatever constructor/method you need to do your signing.