文檔金喜正規買球>>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")); } } }下載此示例的示例文件。