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.IO;
using SautinSoft.Document;

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

        // From a file
        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);
        }

        // From a Stream
        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());
            }
        }
    }
}

Download.

        
            Imports System.IO
Imports SautinSoft.Document

Module ExampleVB

    Sub Main()
        LoadFromFile()
    End Sub

    ' From a file
    Public Sub LoadFromFile()
        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)
    End Sub

    ' From a Stream
    Public Sub LoadFromStream()

        ' We've knowingly created an empty DocumentCore instance before "Using {}"
        ' to continue work with it after stream will be closed.
        Dim dc As DocumentCore = Nothing
        Using fs As New FileStream("..\example.docx", FileMode.Open)

            ' Here we explicitly set that a loadable document is Docx.
            dc = DocumentCore.Load(fs, New DocxLoadOptions())
        End Using
    End Sub

    ' From an array of bytes
    Public Sub LoadFromBytes()

        ' Get document bytes from a file.
        Dim fileBytes() As Byte = File.ReadAllBytes("..\example.pdf")

        Dim dc As DocumentCore = Nothing
        Using ms As New MemoryStream(fileBytes)

            ' With PdfLoadOptions we explicitly set that a loadable document is PDF.
            Dim pdfLO As New PdfLoadOptions() With {
                    .RasterizeVectorGraphics = False,
                    .PageCount = 2
                }
            dc = DocumentCore.Load(ms, pdfLO)
        End Using
    End Sub
End Module

Download.

© SautinSoft 2019