This project is read-only.

How To Use a Custom Package Reader

The following step-by-step tutorial introduces the use of custom package readers.

Package readers

Document Toolkit uses so-called package readers to retrieve the contents of a document. A package reader is a class that knows how to read a document package. Package readers implement the interface FirstFloor.Documents.IO.IPackageReader.

Document Toolkit includes one built-in package reader; FirstFloor.Documents.IO.DefaultPackageReader. The DefaultPackageReader is able to read XPS documents from a System.IO.Stream object. Unfortunately DefaultPackageReader has some severe limitations; it uses Application.GetResourceStream to fetch files from a ZIP file. Application.GetResourceStream is slow and doesn't support all available ZIP formats. For this reason the Document Extensions project provides two custom package readers; DotNetZipPackageReader and SharpZipPackageReader. Both readers use a 3rd party ZIP implementation to read the contents of a document package. The biggest difference between the reader is the licensing. DotNetZip is available under Microsoft Public License, SharpZipLib uses a modified GPL license.

Using the DotNetZip package reader

1) Create a new Silverlight 4 application

2) Use NuGet to add references to the DocumentToolkit and DocumentToolkit.IO.DotNetZip packages (see How To Install Document Toolkit using NuGet).

3) Open the MainPage.xaml and add the following XML namespace declarations:

4) Add the following elements to the MainPage xaml:

<Grid x:Name="LayoutRoot">
    <ColumnDefinition Width="136" />
    <ColumnDefinition />
    <RowDefinition Height="*" />
    <RowDefinition Height="Auto" />

  <doc:DocumentDataSource x:Name="DataSource" LoadError="DataSource_LoadError" />

  <doc:ThumbnailListBox x:Name="Thumbnails" />
  <doc:DocumentViewer x:Name="Viewer" Grid.Column="1" /> 

  <Button Grid.Row="1" Grid.Column="1" Margin="10" Content="Open XPS document..." HorizontalAlignment="Center" />
5) Make sure the LoadError event handler is implemented in the code-behind. The following snippet simply displays an error messagebox.

private void DataSource_LoadError(object sender, ErrorEventArgs e)
6) Assign the following command to the button:

  <dte:OpenFromDiskDotNetZipCommand DocumentDataSource="{Binding ElementName=DataSource}" />
The OpenFromDiskDotNetZipCommand is available in the DotNetZip library. On button click the command displays the Open File dialog and uses the DotNetZipPackageReader to read the contents from the selected document. The DocumentDataSource property of the command is required so the command can assign the package reader to the associated data source.

Last edited Jul 14, 2011 at 11:06 PM by kozw, version 7


No comments yet.