Aspose Words如何转换Word文档为字节数组

作者:控件中国网   出处:控件中国网   2015-08-11 10:15:40   阅读:30

在程序中经常会需要对Word文档进行序列化并且保存到字节数组里,这篇文章主要介绍如何利用Aspose Words文档处理控件对Word文档进行序列化并保存到数组,首选需要把该文档保存到 MemoryStream,然后再利用ToArray方法把MemoryStream转换为字节数组,具体代码如下:

C#

// Load the document.
Document doc = new Document(MyDir + "Document.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);
 
Visual Basic

' Load the document.
Dim doc As New Document(MyDir & "Document.doc")

' Create a new memory stream.
Dim outStream As New MemoryStream()
' Save the document to stream.
doc.Save(outStream, SaveFormat.Docx)

' Convert the document to byte form.
Dim docBytes() As Byte = outStream.ToArray()

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

' Now reverse the steps to load the bytes back into a document object.
Dim inStream As New MemoryStream(docBytes)

' Load the stream into a new document object.
Dim loadDoc As New Document(inStream)

Copyright© 2006-2015 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 渝ICP备12000264号 法律顾问:元炳律师事务所
客服软件
live chat