30

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, that was yesterday. Today I just can't get it to work.

This is the old code, which uses FileStreams and it works:

    public static string OldPdfSigner(PdfReader pdfReader, string destination, string password, string reason, string location, string pathToPfx)
    {
        using (FileStream pfxFile = new FileStream(pathToPfx, FileMode.Open, FileAccess.Read))
        {
            ...

            using (PdfStamper st = PdfStamper.CreateSignature(pdfReader, new FileStream(destination, FileMode.Create, FileAccess.Write), '\0'))
            {
                PdfSignatureAppearance sap = st.SignatureAppearance;
                sap.SetCrypto(key, chain, null, PdfSignatureAppearance.WINCER_SIGNED);
                sap.Reason = reason;
                sap.Location = location;
                return destination;
            }
        }
    }

Below is what I've redone myself which throws System.ObjectDisposedException: Cannot access a closed Stream.

    public static byte[] PdfSigner(PdfReader pdfReader, string password, string reason, string location, string pathToPfx)
    {
        using (FileStream pfxFile = new FileStream(pathToPfx, FileMode.Open, FileAccess.Read))
        {
            ...

            MemoryStream outputStream = new MemoryStream();
            using (PdfStamper st = PdfStamper.CreateSignature(pdfReader, outputStream, '\0'))
            {
                st.Writer.CloseStream = false;
                PdfSignatureAppearance sap = st.SignatureAppearance;
                sap.SetCrypto(key, chain, null, PdfSignatureAppearance.WINCER_SIGNED);
                sap.Reason = reason;
                sap.Location = location;
                st.Close();
                outputStream.Position = 0;
                return outputStream.ToArray();
            }
        }
    }

and if I comment out

st.Close();

it creates an empty document. What am I doing wrong?

kuujinbo
  • 9,272
  • 3
  • 44
  • 57
ADSMarko
  • 363
  • 1
  • 3
  • 8
  • Have a look at [this answer](http://stackoverflow.com/a/25427872/1729265). – mkl Nov 29 '14 at 13:08
  • Safety of the signatures is **not** my responsibility. My sole responsibility is applying the signature to a MemoryStream instead of a FileStream. And I did put `st.Writer.CloseStream = false;` before `st.Close();` Can anyone help me with signing a MemoryStream, please? – ADSMarko Nov 29 '14 at 19:21

1 Answers1

31

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.
  • The 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.
  • Your code snippet is returning the byte array too soon, inside the 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.
  • Generally there's no need to reset the MemoryStream Position property.
  • Ignore the 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.
kuujinbo
  • 9,272
  • 3
  • 44
  • 57
  • Thank you, @kuujinbo, you're a life saver. Now how do I get rid of that downvote, since the question was perfectly legitimate? – ADSMarko Nov 30 '14 at 15:05
  • @ADSMarko: Just an idiosyncrasy of SO, so don't let it get to you. :) For whatever reason someone didn't like your question. For what it's worth, wouldn't have answered if I thought it wasn't a legitimate question, and seen **much** worse that don't get downvoted, so you got an upvote from me. If possible, consider changing the question title to reflect the direct `PdfStamper` / `MemoryStream` relationship, since that was the underlying problem. – kuujinbo Nov 30 '14 at 18:17
  • I was seriously worried that people would simply skip a -1 question – ADSMarko Nov 30 '14 at 19:29
  • 1
    `PdfStamper` no longer seems to implement `IDisposable`. – Robert Harvey Aug 11 '16 at 23:54
  • @RobertHarvey - [The latest version of the source code](https://github.com/itext/itextsharp/blob/develop/src/core/iTextSharp/text/pdf/PdfStamper.cs), 5.5.9 says `PdfStamper` does indeed implement `IDisposable`. – kuujinbo Aug 12 '16 at 21:35
  • @kuujinbo: Then why does the compiler complain if you wrap `using` around it? Anyway, I switched to Mailkit; I needed to solve some other problems. – Robert Harvey Aug 12 '16 at 22:45
  • 2
    Just as an FYI...if you do reset the MemoryStream position to 0 before calling ToArray() you will get an exception saying you cannot access a closed stream - even INSIDE the memory stream using. ToArray by itself doesn't throw. I guess PdfStamper closes it. – samneric Apr 09 '17 at 18:07
  • 1
    I have no idea why converting to Byte[] makes this process work instead of just sending back a memorystream, but it solved my problem. – KeithL Sep 02 '20 at 13:45
  • I get the following as soon as I define the new pdf stamper in the using statement: System.NullReferenceException: 'Object reference not set to an instance of an object.' – MC9000 Aug 28 '22 at 23:13