Welcome Guest! To enable all features, please Login or Register.

Notification

Icon
Error

Options
View
Last Go to last post Unread Go to first unread post
#1 Posted : Friday, May 15, 2020 7:13:54 AM(UTC)
Abdul Rahman

Groups: Registered
Posts: 60


I have multiple document stream and I'm merging the stream into a single lead document using the below code:

Code:

var createOptions = new CreateDocumentOptions();
LEADDocument leadDocument = DocumentFactory.Create(createOptions);
leadDocument.Name = "Virtual";

try
{
    // Add page 1 and 2 from a PDF file 
    LoadDocumentOptions loadOptions = new LoadDocumentOptions();

    foreach (var folderDocumentId in folderDocumentIds)
    {
        int folderId = folderDocumentId.Key;
        IEnumerable<int> documentIds = folderDocumentId.Value;

        var folder = ..Getting Folder Details...

        var flrnostr = xyz;

        foreach (var documentId in documentIds)
        {
            var document = ...getting document details

           // Get File Binary

            var leadDocumentLoadOptions = new LoadDocumentOptions();
            // load document stream
            var documentBytes = (await fileService.GetFileBytesAsync(document.path)).Item1.ToArray();
            using var stream = new MemoryStream(documentBytes);
            
            LEADDocument childDocument = DocumentFactory.LoadFromStream(stream, loadOptions);
            leadDocument.Pages.AddRange(childDocument.Pages);
        }
    }

    // Tell the parent document to dispose any child documents when the parent is disposed 
    leadDocument.AutoDisposeDocuments = true;

    // Show the info of this document, should say 5 pages 
    //Console.WriteLine("Original document information");

    // Now save, the parent document into the cache 
    //leadDocument.SaveToCache();

    // And tell all documents to not delete themselves from the cache 
    leadDocument.AutoDeleteFromCache = true;

    // Save the ID so we can load it 
    //documentId = leadDocument.DocumentId;

    return File(leadDocument.Documents.First().GetDocumentStream(), "application /octet-stream"); // Here I get the error cannot access the closed stream
}
finally
{
    if (leadDocument != null)
        leadDocument.Dispose();
}


Multiple documents get converted into single lead document now If I try to access the stream from lead document using leadDocument.Documents.First().GetDocumentStream() I keep getting the error cannot access the closed stream.

I'm trying to convert multiple document to single document and return it back to browser using asp.net core web api 3.1.

Please assist on how to read the stream from lead document.
 

Try the latest version of LEADTOOLS for free for 60 days by downloading the evaluation: https://www.leadtools.com/downloads

Wanna join the discussion? Login to your LEADTOOLS Support accountor Register a new forum account.

#2 Posted : Friday, May 15, 2020 9:27:16 AM(UTC)
Matthew Bresson

Groups: Registered, Tech Support, Administrators
Posts: 98

Was thanked: 3 time(s) in 3 post(s)

Hello Abdul,

The "cannot access the closed stream" error typically occurs when you are trying to work on or access a MemoryStream that has already been disposed of. Typically trying to access the MemoryStream outside of its `using` statement throws this exception. For testing purposes ensure that you have not disposed of any resources you are trying to access.
https://www.leadtools.co...t-getdocumentstream.html

"The stream returned from this method is the same one used by the various parts of the object when loading the pages and should not be disposed by the user."

If you have any questions about the above process please let us know.

Thanks
Matt Bresson
Developer Support Engineer
LEAD Technologies, Inc.
LEADTOOLS
 
#3 Posted : Friday, May 15, 2020 9:51:46 AM(UTC)
Abdul Rahman

Groups: Registered
Posts: 60


Dear Matt,

From the above code that I have shared in the post, you can see that I'm not disposing the document. I'm loading multiple files into LeadDocument using LoadFromStream and then trying to get the merged Stream from the LeadDocument and I get that error even before disposing. Am I still missing anything?

Please assist.

Thanks,
Abdul
 
#4 Posted : Friday, May 15, 2020 3:02:50 PM(UTC)
Matthew Bresson

Groups: Registered, Tech Support, Administrators
Posts: 98

Was thanked: 3 time(s) in 3 post(s)

Hello Abdul,

So, the error message, "cannot access the closed stream", is being cause by the `using` statement for the MemoryStream. However, the code that you provided will most likely return a null stream and not the stream of your created document. Since you are using a virtual document, you are going to need to finalize the new document. Once you finalize the document you will be able to get the document's stream. There are two options for finalizing a document:

1. Set the document to the DocumentViewer
2. Send the document to the DocumentConverter

For your use-case you will want to add the DocumentConverter to your application and also change how you are disposing of the streams of the documents you are adding to your virtual document. I have some sample code to illustrate your use-case.

Code:

static MemoryStream Test(string folder)
{
     // Where you manage all the streams used for disposal
     List<MemoryStream> streams = new List<MemoryStream>();
     var outputStream = new MemoryStream();
     var createOptions = new CreateDocumentOptions();
     LEADDocument leadDocument = DocumentFactory.Create(createOptions);
     leadDocument.AutoDisposeDocuments = true;
     leadDocument.AutoDeleteFromCache = true;
     leadDocument.Name = "Virtual";
     var files = Directory.GetFiles(folder);
     foreach (var file in files)
     {
          var documentBytes = File.ReadAllBytes(file);
          MemoryStream ms = new MemoryStream(documentBytes);
          streams.Add(ms);
          LEADDocument childDocument = DocumentFactory.LoadFromStream(ms, new LoadDocumentOptions());
          leadDocument.Pages.AddRange(childDocument.Pages);
      }
      // Finalized the new document
      DocumentConverter documentConverter = new DocumentConverter();
      var jobData = new DocumentConverterJobData
      {
           Document = leadDocument,
           // This is the stream you want to send back to the browser
           OutputDocumentStream = outputStream,
           DocumentFormat = DocumentFormat.Pdf,
       };
       var job = documentConverter.Jobs.CreateJob(jobData);
       documentConverter.Jobs.RunJob(job);
       foreach (var error in job.Errors)
           Console.WriteLine($"There was an error:{error.Error}");
       // Return the stream of the new document
       return outputStream;
}
// Dispose of the streams
finally
{
       foreach (var stream in streams)
       stream?.Dispose();
}


If you have any questions about the above information please let me know.

Thanks
Matt Bresson
Developer Support Engineer
LEAD Technologies, Inc.
LEADTOOLS
 
#5 Posted : Monday, May 18, 2020 7:31:23 AM(UTC)
Abdul Rahman

Groups: Registered
Posts: 60


Many thanks Matt. That works like a charm...
 
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Powered by YAF.NET | YAF.NET © 2003-2024, Yet Another Forum.NET
This page was generated in 0.079 seconds.