文档半岛外围网上直营>>Aspose中文文档>>从流中打开文档
从流中打开文档
Aspose.Words是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。
Aspose API支持流行文件格式处理,并允许将各类文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。
如何使用 Aspose.Words创建文档
只需将包含文档的流对象传递到Document构造函数即可。
以下代码示例演示如何从流中打开文档:
Stream stream = File.Open(MyDir + "Document.docx", FileMode.Open);
using (stream)
{
Document doc = new Document(stream);
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Writeln("Append text in body - Open and add to wordprocessing stream");
doc.Save(ArtifactsDir + "Open document from stream - Aspose.Words.docx");
}
如何使用 Open XML SDK 创建文档
您还可以使用 Open XML SDK 执行相同的操作。同时请注意,它看起来有些更复杂、更麻烦。
例如,以下代码示例打开Public Documents 文件夹中的OpenDocumentFromStream.docx文件并向其中添加文本:
public void OpenDocumentFromStreamFeature()
{
using (Stream stream = File.Open(MyDir + "Document.docx", FileMode.Open))
{
using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(stream, true))
{
Body body = wordprocessingDocument.MainDocumentPart.Document.Body;
Paragraph para = body.AppendChild(new Paragraph());
Run run = para.AppendChild(new Run());
run.AppendChild(new Text("Append text in body - Open and add to wordprocessing stream"));
}
}
}
下载此示例的示例文件。

QQ交谈
在线咨询

渝公网安备
50010702500608号

客服热线