C#: Online Guide
How To: Word SDK
Load, Save Document
  |  
Home ›› XDoc.Word ›› C# Word: Load, Save Document

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.


Related .net document control helps:
document annotation asp.net: ASP.NET Document Annotation Control: text comment, drawing on files online using ASP.NET component
asp.net open pdf password: ASP.NET PDF Password Edit Control: online add, remove, update PDF file open password using C#
asp.net azure pdf editor: ASP.NET Azure PDF Editor Control: view, edit, annotate, redact PDF files online
asp.net sharepoint pdf editor: ASP.NET SharePoint PDF Editor Control: view, edit, annotate, redact PDF document in SharePoint sites
asp.net document viewer: ASP.NET Document Viewer Control in C#: View, Annotate, Redact web document files in ASP.NET using C#
asp.net webforms document viewer: ASP.NET WebForms application document viewer control: view, annotate, redact, convert files online in ASP.NET
asp.net office document viewer: ASP.NET Office Word Document Viewer: view MS Office Word files online using C# in ASP.NET web applications




C# DLLs for Word File Loading and Saving



Add references:


  RasterEdge.Imaging.Basic.dll


  RasterEdge.Imaging.Basic.Codec.dll


  RasterEdge.XDoc.Office.Inner.Common.dll


  RasterEdge.Imaging.Drawing.dll


  RasterEdge.Imaging.Processing.dll


  RasterEdge.XDoc.Office.Inner.Office03.dll


  RasterEdge.Imaging.Font.dll


  RasterEdge.XDoc.Word.dll


  RasterEdge.XImage.Raster.Core.dll


  RasterEdge.XImage.Raster.dll


Use corresponding namespaces;


  using RasterEdge.Imaging.Basic;


  using RasterEdge.XDoc.Word;




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 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))
{
    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();