1

I have implemented a code block in order to convert Stream into Byte Array. And code snippet is shown below. But unfortunately, it gives OutOfMemory Exception while converting MemoryStream to Array (return newDocument.ToArray();). please could someone help me with this?

public byte[] MergeToBytes()
{
    using (var processor = new PdfDocumentProcessor())
    {
        AppendStreamsToDocumentProcessor(processor);
        using (var newDocument = new MemoryStream())
        {
            processor.SaveDocument(newDocument);
            return newDocument.ToArray();
        }
    }
}

public Stream MergeToStream()
{
    return new MemoryStream(MergeToBytes());        
}
1
  • 1
    I don't really get it: Why do you convert to an array? It seems what you want is a Stream anyway ( from MergeToStream) ? Commented Apr 30, 2021 at 9:53

1 Answer 1

3

Firstly: how big is the document? if it is too big for the byte[] limit: you're going to have to use a different approach.

However, a MemoryStream is already backed by an (oversized) array; you can get this simply using newDocument.TryGetBuffer(out var buffer), and noting that you must restrict yourself to the portion of the .Array indicated by .Offset (usually, but not always, zero) and .Count (the number of bytes that should be considered "live"). Note that TryGetBuffer can return false, but not in the new MemoryStream() scenario.

If is also interesting that you're converting a MemoryStream to a byte[] and then back to a MemoryStream. An alternative here would just have been to set the Position back to 0, i.e. rewind it. So:

public Stream MergeToStream()
{
    using var processor = new PdfDocumentProcessor();
    AppendStreamsToDocumentProcessor(processor);
    var newDocument = new MemoryStream();
    processor.SaveDocument(newDocument);
    newDocument.Position = 0;
    return newDocument;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the comment. The purpose of this behind is that I'm going to preview multiple documents (some documents contain large-size images). and from the above code block just trying to append each document stream together. I tried your suggestion but unfortunately gave this exception (Input data is not recognized as valid pdf.) if you don't mind could you please bit explain this one -> newDocument.Position = 0;
@John_sl I don't know your age, but: are you familiar with the concept of cassette tapes? you can think of MemoryStream as a cassette tape; when the PDF writes to it, it is rolling forwards, and it leaves it at the end. If you want to read what you just wrote, you need to rewind the position back to the start, which is what Position = 0 does; now, something else can read from it, and should get the same bytes back again. MemoryStream does not maintain separate "read" and "write" positions - there's just "the current position".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.