Convert Save Word Document DOC DOCX Object to Byte Array using Java - byte[] - ByteArrayOutputStream ByteArrayInputStream toByteArray

As the title says, is there any way to obtain a raw byte array of an object of the Document class using Aspose.Words?

Hi

Thanks for your request. If you need to save a document to bytes array and vice versa, the code is pretty simple:

// Open document
Document doc = new Document("C:\\Temp\\in.doc");
// Create stream where we will save document
ByteArrayOutputStream docOutStream = new ByteArrayOutputStream();
// Save document to stream
doc.save(docOutStream, SaveFormat.DOC);
// Get document bytes
byte[] docBytes = docOutStream.toByteArray();
// Now recreate document from byte array
// Create document input stream from byte array.
ByteArrayInputStream docInStream = new ByteArrayInputStream(docBytes);
// Create document from stream
Document outDoc = new Document(docInStream);
// Save document
outDoc.save("C:\\Temp\\out.doc");

Please let me know in case of any issues.
Best regards.

Thanks for this solution. I’ll try it now and see if it works!

EDIT: I tried it and it worked flawlessly =D thank you!