Converti Word (DOC, DOCX) in Byte Array C# Java

Aspose.Words API fornisce il modo più semplice per convertire un documento Microsoft Word DOC o DOCX in una matrice di byte in C# e Java.

La conversione di un documento Word in un array di byte è utile quando si archiviano documenti nel database e/o li si recupera dal database.

Aspose.Words API fornisce la funzionalità per manipolare i file di Microsoft Word senza utilizzare Microsoft Word. Se non hai l’API Aspose.Words installata, segui le istruzioni fornite nella pagina installazione per .NET e nella pagina installazione per l’utilizzo con Java.

Converti un documento di Word in una matrice di byte usando C#

Aspose.Words for .NET può essere utilizzato per convertire un oggetto Document per ottenere una matrice di byte che rappresenta il documento in qualsiasi applicazione .NET. Il frammento di codice seguente illustra la conversione di un file DOC in una matrice di byte.

// 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);

Converti un documento di Word in un array di byte utilizzando Java

Il frammento di codice seguente illustra la conversione di un file DOC in una matrice di byte utilizzando l’API Aspose.Words for Java.

// 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);

Guarda anche