分享基于silverlight的仿“百度文库”的文档控件

通用的FlashPaper支持Word/Excel/PDF,到时对于Silverlight的XPS的文档支持问题比较多,本控件提供了一个可视化的XPS文档展示,提供放大缩小/打印/搜索/分页等功能,主要整合了开源的Document Toolkit。

 

1. 使用Document Toolkit,DocumentDataSource提供数据源,PageNvaigator提供分页 :


<doc:DocumentDataSource x:Name="dataSource"/>

        <doc:DocumentViewer Grid.Row="1" x:Name="viewer" DocumentDataSource="{Binding ElementName=dataSource}" ViewMode="{Binding SelectedViewMode, ElementName=viewModePicker}" BorderBrush="#9fa9a4" BorderThickness="1"/>

        <doc:PageNavigator x:Name="navigator" HorizontalAlignment="Center" 
                    PageCount
="{Binding PageCount, ElementName=viewer}"
                    PageIndex
="{Binding PageIndex, ElementName=viewer, Mode=TwoWay}"
                    
/>
                
        <doc:ViewModePicker Grid.Column="1" x:Name="viewModePicker" Visibility="Collapsed"/>

 

2. WebPackageReader读取本地xps或远程xps文件作为数据源

    DotNetZipPackageReader 根据分页延迟加载文档


// loads the sample XPS document from the web
            var url = string.Format("/DocumentService.ashx?id={0}", HtmlPage.Document.GetElementById("documentId").GetProperty("value"));
            webClient.OpenReadAsync(new Uri(HtmlPage.Document.DocumentUri, url));

            var reader = new WebPackageReader(new Uri(HtmlPage.Document.DocumentUri, url + "&part="));
            this.dataSource.PackageReader = reader;
            var xpsClient = new XpsClient();
            xpsClient.LoadXpsDocumentAsync(reader);

 

3. 服务端根据请求的文件ID和当前页码返回指定的文件流


private void Response(HttpContext context, string xpsFileName, string partName)
        {
            using (FileStream stream = File.OpenRead(xpsFileName))
            {
                ZipFile file = new ZipFile(stream);
                ZipEntry entry = file.GetEntry(partName);
                if (entry != null)
                {
                    using (Stream entryStream = file.GetInputStream(entry))
                    {
                        // TODO: set mime-type as defined in XPS package
                        context.Response.ContentType = "application/octet-stream";
                        byte[] buffer = new byte[2 << 14];    // write blocks of 32768 bytes
                        int read;
                        while ((read = entryStream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            context.Response.OutputStream.Write(buffer, 0, read);
                        }
                    }
                }
                else
                {
                    // return 404 Not Found
                    context.Response.StatusCode = (int)HttpStatusCode.NotFound;
                }
            }

        }

 

4. 源代码下载

 https://199.47.216.171/u/10032723/DocumentToolkit.rar

 

5. 在线预览

 

http://rapidsl2.guozili.25u.com/  (admin/admin  点左边菜单 控件展示 - 文档查看器)

 

 6. 截图

 

 

 

posted on 2012-07-16 13:54  guozili  阅读(4667)  评论(20编辑  收藏  举报