title: Aspose PDF Merge PDF from byte[][]
body: <p>I'm working on a service that pulls multiple PDF files stored in a database as <code>varbinary</code> and merges them into a single file using <code>Aspose - PDF</code>. The merged file is then converted to a <code>Memory Stream</code> and then a <code>blob</code> and then sent to the webpage.</p>
  
  <p><strong>Here's my service:</strong></p>
  
  <pre><code>    public MemoryStream GetPrintContent(List&lt;ConfirmationRequestNoticeViewModel&gt; models)
      {
          // Instantiate Pdf instance by calling its empty constructor
          Document pdf1 = new Document();
  
          byte[][] bytes = new byte[models.Count][];
  
          for (int i = 0; i &lt; models.Count; i++)
          {
              ConfirmationRequestNoticeViewModel model = models[i];
              byte[] fileContent = _dataService.GetPrintContent(model.ConfirmationRequestId);
              bytes[i] = fileContent;
  
          }
          MemoryStream stream = new MemoryStream();
          List&lt;Document&gt; documents = GeneratePdfs(bytes, stream);
          stream = ConcatenatePdf(documents);
          return stream;
      }
  
      private MemoryStream ConcatenatePdf(List&lt;Document&gt; documents)
      {
          MemoryStream stream = new MemoryStream();
          Document mergedPdf = documents[0];
          for(int index = 1; index &lt; documents.Count; index++)
          {
              for (int i = 0; i &lt; documents[index].Pages.Count; i++)
              {
                  mergedPdf.Pages.Add(documents[index].Pages[i + 1]);
              }
          }
          mergedPdf.Save(stream);
          return stream;
      }
  
      private List&lt;Document&gt; GeneratePdfs(byte[][] content, MemoryStream stream)
      {
          List&lt;Document&gt; documents = new List&lt;Document&gt;();
          Document pdf = new Document();
          foreach (byte[] fileContent in content)
          {
              using (MemoryStream fileStream = new MemoryStream(fileContent))
              {
                  pdf = new Document(fileStream);
                  pdf.Save(stream);
                  documents.Add(pdf);
              }
          }
          return documents;
      }
  </code></pre>
  
  <p>Everything is working great except for the line <code>mergedPdf.Save(stream);</code> which returns the error <em>Cannot access a closed stream</em>.</p>
  
  <p>I've been working on this and can't seem to understand why the Memory Stream has been closed. Has anyone else encountered this issue?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>I've found the issue listed <a href="http://aspose136.rssing.com/browser.php?indx=5613837&amp;item=1917" rel="nofollow noreferrer">here</a></p>