How To Take Advantage Of The Document Toolkit Client API

Document Toolkit for LightSwitch includes a client API with a number of classes that can be helpful when viewing documents in LightSwitch applications. This tutorial demonstrates what you can achieve with the client API.

The client API is available in the screen code of your LightSwitch application. In order to access the classes in the client API you need to reference the namespace DocumentToolkit.Client in your screen code.

using DocumentToolkit.Client;

Imports DocumentToolkit.Client


The DocumentReader class provides properties and methods for reading and converting documents. A DocumentReader instance is created by invoking the static Create method. The Create method supports stream, binary (= byte array) and FileInfo parameter types. Once created the DocumentReader provides information on the document type and allows for converting the document to XPS if supported.

The following code snippet demonstrates the use of the DocumentReader.

using DocumentToolkit.Client;

using (var reader = DocumentReader.Create(file)) {
  // get the document type
  var type = reader.DocumentType;

  // get document binary
  var binary = reader.ToBinary();

  // convert to XPS only when needed
  if (reader.RequiresConversionToXps) {
    var xps = reader.ToXpsBinary();

Imports DocumentToolkit.Client

Dim reader = DocumentReader.Create(file)
Using reader
  ' get the document type
  Dim type = reader.DocumentType

  ' get document binary
  Dim binary = reader.ToBinary

  ' convert to XPS only when needed
  If reader.RequiresConversionToXps Then
    Dim xps = reader.ToXpsBinary
  End If
End Using

DocumentReader implements IDisposable. It is highly recommended to dispose of a DocumentReader instance when it is no longer used. Disposing a reader ensures the underlying stream is properly disposed.


The FileDialogFilterBuilder class assists in creating file dialog filters. The OpenFileDialog allows for specifying a filter string that filters the files that can be selected. The filter requires the use of a certain syntax, which the FileDialogFilterBuilder provides. The document types supported by Document Toolkit for LightSwitch are identified with the DocumentType enum that is included in the client API. The builder sorts the output filter and excludes duplicate file extensions automatically.

The following code snippet demonstrates the use of creating a filter that filters Word and XPS documents.

using DocumentToolkit.Client;

var b = new FileDialogFilterBuilder();

var dlg = new OpenFileDialog();
dlg.Filter = b.ToString();

Imports DocumentToolkit.Client

Dim b = New FileDialogFilterBuilder

Dim dlg = New OpenFileDialog
dlg.Filter = b.ToString()

On executing the code, the filter string contains the following value:

All Documents (*.doc,*.docx)|*.doc;*.docx|Word Documents (*.doc,*.docx)|*.doc;*.docx|All Files (*.*)|*.*

The OpenFileDialog filter looks like this:


The static FileDialogFilterBuilder.DefaultFilter property contains the filter for all supported documents of the current application. When the application is not deployed as desktop application, the office documents are automatically excluded from the filter.

Last edited Jul 4, 2011 at 11:56 PM by kozw, version 5


No comments yet.