2017-01-09 13 views
0

나는 데이터베이스에 저장된 여러 개의 PDF 파일을 varbinary으로 가져와 Aspose - PDF을 사용하여 단일 파일로 병합하는 서비스를 개발 중입니다. 병합 된 파일은 Memory Stream으로 변환 된 후 blob으로 변환 된 다음 웹 페이지로 전송됩니다.Aspose PDF Merge PDF from byte [] []

public MemoryStream GetPrintContent(List<ConfirmationRequestNoticeViewModel> models) 
    { 
     // Instantiate Pdf instance by calling its empty constructor 
     Document pdf1 = new Document(); 

     byte[][] bytes = new byte[models.Count][]; 

     for (int i = 0; i < models.Count; i++) 
     { 
      ConfirmationRequestNoticeViewModel model = models[i]; 
      byte[] fileContent = _dataService.GetPrintContent(model.ConfirmationRequestId); 
      bytes[i] = fileContent; 

     } 
     MemoryStream stream = new MemoryStream(); 
     List<Document> documents = GeneratePdfs(bytes, stream); 
     stream = ConcatenatePdf(documents); 
     return stream; 
    } 

    private MemoryStream ConcatenatePdf(List<Document> documents) 
    { 
     MemoryStream stream = new MemoryStream(); 
     Document mergedPdf = documents[0]; 
     for(int index = 1; index < documents.Count; index++) 
     { 
      for (int i = 0; i < documents[index].Pages.Count; i++) 
      { 
       mergedPdf.Pages.Add(documents[index].Pages[i + 1]); 
      } 
     } 
     mergedPdf.Save(stream); 
     return stream; 
    } 

    private List<Document> GeneratePdfs(byte[][] content, MemoryStream stream) 
    { 
     List<Document> documents = new List<Document>(); 
     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; 
    } 

모든 닫힌 스트림에 액세스 할 수 없습니다 오류 을 돌려 줄 mergedPdf.Save(stream);를 제외하고 큰 노력하고 있습니다 :

여기 내 서비스입니다.

저는이 작업을 해왔으며 메모리 스트림이 닫힌 이유를 이해할 수 없습니다. 다른 사람이이 문제를 겪었습니까?

편집 :

나는 문제가 나는 완전히 리팩토링했다, 그래서 나는 현재 구현 폐쇄 MemoryStreams의 문제를 해결할 수 here

답변

0

을 나열 발견했습니다.

대신 PdfFileEditor.Concatenate() 방법 explained in this forum post으로갔습니다. 다음과 같이

내 구현은 다음과 같습니다

public byte[] GetPrintContent(List<ConfirmationRequestNoticeViewModel> models) 
    { 
     PdfFileEditor pdfEditor = new PdfFileEditor(); 

     MemoryStream[] inputStreams = new MemoryStream[models.Count]; 
     MemoryStream fileStream = new MemoryStream(); ; 


     using (MemoryStream outputStream = new MemoryStream()) 
     { 
      for (int i = 0; i < models.Count; i++) 
      { 
       ConfirmationRequestNoticeViewModel model = models[i]; 
       byte[] fileContent = _dataService.GetPrintContent(model.ConfirmationRequestId); 

       fileStream = new MemoryStream(fileContent); 

       inputStreams[i] = fileStream; 

      } 
      bool success = pdfEditor.Concatenate(inputStreams, outputStream); 
      byte[] data = outputStream.ToArray(); 
      fileStream.Dispose(); 
      return data; 
     } 

    }