C# Word Library
How to C#: Load, Save Word Document
Overview for How to Use XDoc.Word to load and save Word document in C# .NET Programming Project
RasterEdge XDoc.Word has provided three ways to create word document object that are load from file, load from stream and load from byte array. Conversely, the Word SDK can also save word document object to file, stream or byte array.
Load Word Document From Existing File Using C#
You may also load or create a Word document object from existing Word file in C#.net.
// Load from a file
String inputFilePath = Program.RootPath + "\\" + "1.docx";
DOCXDocument doc = new DOCXDocument(inputFilePath);
if (doc == null) throw new Exception("fail to load the file");
// ...
Load Word From Stream Object in C# Project
Word document can be loaded from a stream object in C# programming.
// Load from a stream
String inputFilePath = Program.RootPath + "\\" + "2.docx";
using (FileStream fileStream = File.Open(inputFilePath, FileMode.Open, FileAccess.Read))
{
DOCXDocument doc = new DOCXDocument(fileStream);
if (doc == null) throw new Exception("fail to load word document from the stream");
// ...
}
Load Word From Byte Array Object in C# Project
Word document can be loaded from a byte array object in C# programming.
// Load from a stream
String inputFilePath = Program.RootPath + "\\" + "2.docx";
using (FileStream fileStream = File.Open(inputFilePath, FileMode.Open, FileAccess.Read))
{
byte[] array = new byte[fileStream.Length];
fileStream.Read(array, 0, array.Length);
DOCXDocument doc = new DOCXDocument(fileStream);
if (doc == null) throw new Exception("fail to load word document from the stream");
// ...
}
Save Word Document To File in C# Project
Word document can be saved to a word file in C# programming.
String inputFilePath = Program.RootPath + "\\" + "2.docx";
String outputFilePath = Program.RootPath + "\\" + "output.docx";
DOCXDocument doc = new DOCXDocument(fileStream);
if (doc == null) throw new Exception("fail to load word document from the stream");
// Save document to a word file
doc.Save(outputFilePath);
Save Word Document To Stream Object in C# Project
Word document can be saved to a stream object in C# programming.
String inputFilePath = Program.RootPath + "\\" + "2.docx";
Stream stream = new MemoryStream();
DOCXDocument doc = new DOCXDocument(fileStream);
if (doc == null) throw new Exception("fail to load word document from the stream");
// Save document to a stream object
doc.SaveToStream(stream);
Save Word Document To Byte Array in C# Project
Word document can be saved to a byte array object in C# programming.
String inputFilePath = Program.RootPath + "\\" + "2.docx";
DOCXDocument doc = new DOCXDocument(inputFilePath);
if (doc == null) throw new Exception("fail to load word document from the stream");
// Save document to a byte array object
byte[] array = doc.SaveToByte();