Konversi Word (DOC, DOCX) ke Byte Array C# Java

Aspose.Words API menyediakan cara termudah untuk mengonversi dokumen Microsoft Word DOC atau DOCX menjadi array byte di C# dan Java.

Konversi dokumen Word ke array byte berguna saat menyimpan dokumen dalam database dan/atau kemudian mengambilnya dari database.

Aspose.Words API menyediakan fungsionalitas untuk memanipulasi file Microsoft Word tanpa menggunakan Microsoft Word. Jika Anda belum menginstal Aspose.Words API, harap ikuti petunjuk yang diberikan pada halaman installation untuk .NET dan halaman installation untuk digunakan dengan Java.

Mengonversi Dokumen Word ke Array Byte menggunakan C#

Aspose.Words for .NET dapat digunakan untuk mengonversi objek Document untuk mendapatkan larik byte yang mewakili Dokumen dalam aplikasi .NET apa pun. Cuplikan kode berikut menunjukkan konversi file DOC ke array 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);

Konversikan Dokumen Word ke Byte Array menggunakan Java

Cuplikan kode berikut menunjukkan konversi file DOC ke array byte menggunakan 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);

Lihat juga