This project is read-only.

How To Load Documents From The Web

The following step-by-step tutorial demonstrates how to download documents from a website and display them using Document Toolkit. The download link for the complete source code of this tutorial is available at the bottom of this page.

1) Launch Visual Studio 2010 and create a new Silverlight 4 Application Project hosted in a new web site.
2) Use Nuget to add references to Document Toolkit. See also How To Install Document Toolkit using NuGet.
2.1) Right click the references of the Silverlight 4 Project
2.2) Select 'Add Library Package References...'
2.3) Select the First Floor Software packages
2.4) Install the DocumentToolkit.Reader.DotNetZip package. This will implicitly install the DocumentToolkit package as well.
3) Add sample XPS documents to the web project
3.1) Right click the references of the Web Project
3.2) Select 'Add Library Package References...'
3.3) Select the First Floor Software packages
3.4) Install the DocumentToolkit.SampleDocs package.
4) Open the MainPage.xaml and add the following XML namespace declaration:


5) Create an instance of a DocumentDataSource and DocumentViewer by adding the following elements to the page XAML.

<Grid x:Name="LayoutRoot">
  <doc:DocumentDataSource x:Name="DataSource" LoadError="DataSource_LoadError" />
  <doc:DocumentViewer x:Name="Viewer" DocumentDataSource="{Binding ElementName=DataSource}">
      <doc:ViewMode ScaleMode="FitPage" />

Setting the view mode to FitPage ensures the pages are automatically resized to fit the screen.

6) Make sure the LoadError event handler is implemented in the code-behind. The below snippet simply displays an error messagebox.

using FirstFloor.Documents.Controls;

private void DataSource_LoadError(object sender, ErrorEventArgs e)

7) Now we need to add the code to download the documents. We use the WebClient to perform the download. Add the following code to the constructor of the MainPage class:

var client = new WebClient();
client.OpenReadCompleted += (o, e) => {
    if (e.Error != null) {
    else if (!e.Cancelled) {
        // create package reader for result stream
        var reader = new DotNetZip.DotNetZipPackageReader(e.Result);

        // assign reader to datasource
        this.DataSource.PackageReader = reader;

// initiate async download
client.OpenReadAsync(new Uri("/Document Toolkit.xps", UriKind.Relative));

The code asynchronously downloads the document from the web using a WebClient instance. Once the download has completed without errors, a package reader is created using the loaded document stream. The Document Toolkit.xps document was previously added to the website by installing the DocumentToolkit.SampleDocs package.

8) Hit F5 to compile and run the application.

Tutorial source code:

Last edited Jul 4, 2011 at 8:25 PM by kozw, version 7


No comments yet.