Back to Devexpress

How to: Load Document Properties

officefileapi-401950-word-processing-document-api-examples-files-how-to-load-document-properties.md

latest5.2 KB
Original Source

How to: Load Document Properties

  • Aug 13, 2020
  • 3 minutes to read

The Word Processing Document API allows you to load the document metadata (document properties) without loading the document itself. You can use the retrieved information to search for a specific document, catalogue files or collect statistics.

In this example, the information is used to sort documents by their modification date. Call the RichEditDocumentServerExtensions.LoadDocumentProperties method for each file in the folder. Check the Modified property and copy the file to the corresponding folder to sort it by tis modification date.

Important

The RichEditDocumentServerExtensions class is defined in the DevExpress.Docs.v25.2.dll assembly and DevExpress.Document.Processor NuGet package. Add this assembly or package to your project to use the RichEditDocumentServerExtensions members. You need a license for the DevExpress Office File API Subscription or DevExpress Universal Subscription to use this library in production code.

csharp
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
using System;
using System.IO;
using System.Diagnostics;

static void Main(string[] args)
{
    DirectoryInfo directoryInfo = 
    new DirectoryInfo(@"C:\Users\Public\Documents\DevExpress Demos 25.2\Components\Data");
    FileInfo[] files = directoryInfo.GetFiles("*.docx");
    if (directoryInfo.Exists)
    {
        foreach (FileInfo file in files)
        {
            SortDocuments(file);
        }
        Process.Start("explorer.exe", @"C:\Documents");
    }
}

static void SortDocuments(FileInfo file)
{
  using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer())
  {
    //Load the metadata from the specified document:
    ReadOnlyDocumentProperties docProperties = 
    wordProcessor.LoadDocumentProperties(File.OpenRead(file.FullName));
    DateTime date = docProperties.BuiltIn.Modified;

    //Check the year the document was last modified,
    //And copy the file to the corresponding folder:
    switch (date.Year)
    {
        case 2017:
            string destFolder = "C://Documents//2017";
            CreateDirectory(destFolder);
            file.CopyTo(Path.Combine(destFolder, file.Name), true);
            break;
        case 2018:
            destFolder = "C://Documents//2018";
            CreateDirectory(destFolder);
            file.CopyTo(Path.Combine(destFolder, file.Name), true);
            break;
        case 2019:
            destFolder = "C://Documents//2019";
            CreateDirectory(destFolder);
            file.CopyTo(Path.Combine(destFolder, file.Name), true);
            break;
        case 2020:
            destFolder = "C://Documents//2020";
            CreateDirectory(destFolder);
            file.CopyTo(Path.Combine(destFolder, file.Name), true);
            break;
    }
  }
}

static void CreateDirectory(string path)
{
    if (!Directory.Exists(path))
        Directory.CreateDirectory(path);
}
vb
Imports DevExpress.XtraRichEdit
Imports DevExpress.XtraRichEdit.API.Native
Imports System
Imports System.IO
Imports System.Diagnostics

Shared Sub Main(ByVal args() As String)
    Dim directoryInfo
     As New DirectoryInfo("C:\Users\Public\Documents\DevExpress Demos 25.2\Components\Data")
    Dim files() As FileInfo = directoryInfo.GetFiles("*.docx")
    If directoryInfo.Exists Then
        For Each file As FileInfo In files
            SortDocuments(file)
        Next file
        Process.Start("explorer.exe", "C:\Documents")
    End If
End Sub

Shared Sub SortDocuments(ByVal file As FileInfo)
    Using wordProcessor As New RichEditDocumentServer()
    'Load the metadata from the specified document:
    Dim docProperties As ReadOnlyDocumentProperties =
        wordProcessor.LoadDocumentProperties(File.OpenRead(file.FullName))
    Dim [date] As Date = docProperties.BuiltIn.Modified
    Dim destFolder As String

    'Check the year the document was last modified,
    'And copy the file to the corresponding folder:
    Select Case [date].Year
        Case 2017
            destFolder = "C://Documents//2017"
            CreateDirectory(destFolder)
            file.CopyTo(Path.Combine(destFolder, file.Name), True)
        Case 2018
            destFolder = "C://Documents//2018"
            CreateDirectory(destFolder)
            file.CopyTo(Path.Combine(destFolder, file.Name), True)
        Case 2019
            destFolder = "C://Documents//2019"
            CreateDirectory(destFolder)
            file.CopyTo(Path.Combine(destFolder, file.Name), True)
        Case 2020
            destFolder = "C://Documents//2020"
            CreateDirectory(destFolder)
            file.CopyTo(Path.Combine(destFolder, file.Name), True)
    End Select
  End Using
End Sub

Shared Sub CreateDirectory(ByVal path As String)
    If Not Directory.Exists(path) Then
        Directory.CreateDirectory(path)
    End If
End Sub