Convert Word (DOC, DOCX) to Byte Array

Convert Word (DOC, DOCX) to Byte Array C# Java

Aspose.Words API provides the simplest way to convert a Microsoft Word DOC or DOCX document to a byte array in C# and Java.

The conversion of a Word document to a byte array is helpful when storing documents in the database and/ or then retrieving them from the database.

Aspose.Words API provides the functionality to manipulate Microsoft Word files without using Microsoft Word. If you do not have Aspose.Words API installed, please follow the instructions given on installation page for .NET and installation page for using with Java.

Convert a Word Document to a Byte Array using C#

Aspose.Words for .NET can be used to convert a Document object to obtain a byte array representing the Document in any .NET application.
The following code snippet demonstrates the conversion of a DOC file to a byte array.

// Load the document from disk.
Document doc = new Document("Sample.doc");

// 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);
// Save the document.
loadDoc.Save("loadDoc.docx",SaveFormat.Docx);

Convert a Word Document to a Byte Array using Java

The following code snippet demonstrates the conversion of a DOC file to a byte array using Aspose.Words for Java API.

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

// Create a new memory stream.
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
// Save the document to stream.
doc.save(outStream, SaveFormat.DOCX);

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

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

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

// Load the stream into a new document object.
Document loadDoc = new Document(inStream);
// Save the document.
loadDoc.save("loadDoc.docx",SaveFormat.Docx);

See Also