title: iTextSharp PdfSignatureAppearance.Close() throws Out of Memory Exception
body: <p>I have a application that digitally sign PDF files.</p>
  
  <p>But I can't digitally sign a file bigger than 400mb in my developer machine. </p>
  
  <p>I use the following code to sign. This code throws an OutOfMemoryException when call the method <code>Close</code> of <code>PdfSignatureAppearance</code>. </p>
  
  <p>With smaller files it just works ok. </p>
  
  <p>What can I do to avoid Out of Memory, when I need to sign big files?</p>
  
  <p>Here is my code :</p>
  
  <pre><code>    private static void ApplySignatureDictionary(PdfSignatureAppearance sap, byte[] pk)
      {
          try
          {
              byte[] outc = new byte[8092];
  
              PdfDictionary dicSignature = new PdfDictionary();
  
              Array.Copy(pk, 0, outc, 0, pk.Length);
  
              dicSignature.Put(PdfName.CONTENTS, new PdfString(outc).SetHexWriting(true));
              sap.Close(dicSignature); //This throws a OutOfMemoryException, for files bigger than 400mb
          }
          catch (OutOfMemoryException omEx)
          {
              throw new Exception("Not enough memory to sign the file.", omEx);
          }
          catch (Exception ex)
          {
              throw ex;
          }
      }
  </code></pre>
  
  <p>Where <code>pk</code> is the result of <code>Org.BouncyCastle.Cms.CmsSignedData.GetEncoded()</code></p>
  
  <p>EDIT :</p>
  
  <p>I instantiate PdfReader and PdfStamper this way :</p>
  
  <p>PdfReader :</p>
  
  <pre><code> PdfReader reader = new PdfReader(new iTextSharp.text.pdf.RandomAccessFileOrArray(inputPDF), null);
  </code></pre>
  
  <p>and</p>
  
  <p>PdfStamper :</p>
  
  <pre><code>MemoryStream mFile = new MemoryStream();
  
  PdfStamper stamper = PdfStamper.CreateSignature(reader, mFile, '\0');
  </code></pre>