Convert Document to Byte Array

It shows how to serialize a Document object to obtain a byte array representing the Document and then how to unserialize the byte array to obtain a Document object again. This technique is often required when storing a document in a database or for preparing a Document for transmission across the web.

Please note that an Document object cannot be serialized using built in .NET serialization techniques but instead can be serialized by using the method detailed below.

The simplest method used to serialize a Document object is to first save it to a MemoryStream using the Document.Save method overload of the Document class. The ToArray method is then called on the stream which returns an array of bytes representing the document in byte form.

The save format chosen is important as to ensure the highest fidelity is retained upon saving and reloading into the Document object. For this reasons an OOXML format is suggested.

The steps above are then reversed to load the bytes back into a Document object.

          // Load the document.
            Document doc = new Document("Converting Document.docx");

            // Create a new memory stream.
            MemoryStream outStream = new MemoryStream();
            // Save the document to stream.
            doc.Save(outStream, SaveFormat.Docx);

            // Convert the document to byte form.
            byte[] docBytes = outStream.ToArray();

            // The bytes are now ready to be stored/transmitted.

            // Now reverse the steps to load the bytes back into a document object.
            MemoryStream inStream = new MemoryStream(docBytes);

            // Load the stream into a new document object.
            Document loadDoc = new Document(inStream);


Last edited Mar 3, 2014 at 5:13 AM by asposemarketplace, version 2