How to load a DOCX document in C# and VB.Net

How to load a DOCX document in C# and VB.Net

  1. Load from a file:
    
    DocumentCore dc = DocumentCore.Load(@"d:\Book.docx");
    
    The dc represents the document "Book.docx" loaded in memory. The file format is detected automatically from the file extension: ".docx".

    After loading you'll get the document presented as the Tree Of Objects, where the root node is DocumentCore class.

    To guarantee that a loadable content is really DOCX and set some loading options, use DocxLoadOptions as 2nd parameter.

    
    DocumentCore dc = DocumentCore.Load(@"d:\Book.docx", new DocxLoadOptions());
    
  2. Load using a memory:
    
                // Let us say we already have a DOCX document as array of bytes.
                byte[] docxBytes = null;
                // docxBytes = ...
                
                DocumentCore dc = null;
                using (MemoryStream docxStream = new MemoryStream(docxBytes))
                {
                    dc = DocumentCore.Load(docxStream, new DocxLoadOptions());
                }
                // Here we can do with our document 'dc' anything we need.
    
 

Complete code

using System;
using System.IO;
using SautinSoft.Document;

namespace Example
{
    class Program
    {      
        static void Main(string[] args)
        {
            LoadDocxFromFile();
            //LoadDocxFromStream();
        }

        /// <summary>
        /// Loads a DOCX document into DocumentCore (dc) from a file.
        /// </summary>
        /// <remarks>
        /// Details: https://www.sautinsoft.com/products/document/examples/load-docx-document-net-csharp-vb.php
        /// </remarks>
        static void LoadDocxFromFile()
        {
            string filePath = @"..\..\example.docx";
            // The file format is detected automatically from the file extension: ".docx".
            // But as shown in the example below, we can specify DocxLoadOptions as 2nd parameter
            // to explicitly set that a loadable document has Docx format.
            DocumentCore dc = DocumentCore.Load(filePath);

            if (dc != null)
                Console.WriteLine("Loaded successfully!");
            Console.ReadKey();
        }

        /// <summary>
        /// Loads a DOCX document into DocumentCore (dc) from a MemoryStream.
        /// </summary>
        /// <remarks>
        /// Details: https://www.sautinsoft.com/products/document/examples/load-docx-document-net-csharp-vb.php
        /// </remarks>
        static void LoadDocxFromStream()
        {
            // Assume that we already have a DOCX document as bytes array.
            byte[] fileBytes = File.ReadAllBytes(@"..\..\example.docx");

            DocumentCore dc = null;

            // Create a MemoryStream
            using (MemoryStream ms = new MemoryStream(fileBytes))
            {
                // Load a document from the MemoryStream.
                // Specifying DocxLoadOptions we explicitly set that a loadable document is Docx.
                dc = DocumentCore.Load(ms, new DocxLoadOptions());
            }
            if (dc != null)
                Console.WriteLine("Loaded successfully!");
            Console.ReadKey();
        }
    }
}

Download.

        
            Imports System
Imports System.IO
Imports SautinSoft.Document

Module Sample
    Sub Main()
        LoadDocxFromFile()
        'LoadDocxFromStream();
    End Sub

    ''' <summary>
    ''' Loads a DOCX document into DocumentCore (dc) from a file.
    ''' </summary>
    ''' <remarks>
    ''' Details: https://www.sautinsoft.com/products/document/examples/load-docx-document-net-csharp-vb.php
    ''' </remarks>
    Sub LoadDocxFromFile()
        Dim filePath As String = "..\example.docx"
        ' The file format is detected automatically from the file extension: ".docx".
        ' But as shown in the example below, we can specify DocxLoadOptions as 2nd parameter
        ' to explicitly set that a loadable document has Docx format.
        Dim dc As DocumentCore = DocumentCore.Load(filePath)

        If dc IsNot Nothing Then
            Console.WriteLine("Loaded successfully!")
            Console.ReadKey()
        End If
    End Sub

    ''' <summary>
    ''' Loads a DOCX document into DocumentCore (dc) from a MemoryStream.
    ''' </summary>
    ''' <remarks>
    ''' Details: https://www.sautinsoft.com/products/document/examples/load-docx-document-net-csharp-vb.php
    ''' </remarks>
    Sub LoadDocxFromStream()
        ' Assume that we already have a DOCX document as bytes array.
        Dim fileBytes() As Byte = File.ReadAllBytes("..\example.docx")

        Dim dc As DocumentCore = Nothing

        ' Create a MemoryStream
        Using ms As New MemoryStream(fileBytes)
            ' Load a document from the MemoryStream.
            ' Specifying DocxLoadOptions we explicitly set that a loadable document is Docx.
            dc = DocumentCore.Load(ms, New DocxLoadOptions())
        End Using
        If dc IsNot Nothing Then
            Console.WriteLine("Loaded successfully!")
            Console.ReadKey()
        End If
    End Sub
End Module

Download.


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.

© SautinSoft 2002 - 2019