silverlight仿“百度文库”的文档控件

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

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

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

  1. <doc:DocumentDataSource x:Name="dataSource"/>  
  2.  
  3.         <doc:DocumentViewer Grid.Row="1" x:Name="viewer" DocumentDataSource="{Binding ElementName=dataSource}" ViewMode="{Binding SelectedViewMode, ElementName=viewModePicker}" BorderBrush="#9fa9a4" BorderThickness="1"/>  
  4.  
  5.         <doc:PageNavigator x:Name="navigator" HorizontalAlignment="Center"   
  6.                     PageCount="{Binding PageCount, ElementName=viewer}" 
  7.                     PageIndex="{Binding PageIndex, ElementName=viewer, Mode=TwoWay}" 
  8.                     />  
  9.                   
  10.         <doc:ViewModePicker Grid.Column="1" x:Name="viewModePicker" Visibility="Collapsed"/> 

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

DotNetZipPackageReader 根据分页延迟加载文档

  1. // loads the sample XPS document from the web  
  2.             var url = string.Format("/DocumentService.ashx?id={0}", HtmlPage.Document.GetElementById("documentId").GetProperty("value"));  
  3.             webClient.OpenReadAsync(new Uri(HtmlPage.Document.DocumentUri, url));   
  4.             var reader = new WebPackageReader(new Uri(HtmlPage.Document.DocumentUri, url + "&part="));  
  5.             this.dataSource.PackageReader = reader;  
  6.             var xpsClient = new XpsClient();  
  7.             xpsClient.LoadXpsDocumentAsync(reader); 

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

  1. private void Response(HttpContext context, string xpsFileName, string partName)  
  2.         {  
  3.             using (FileStream stream = File.OpenRead(xpsFileName))  
  4.             {  
  5.                 ZipFile file = new ZipFile(stream);  
  6.                 ZipEntry entry = file.GetEntry(partName);  
  7.                 if (entry != null)  
  8.                 {  
  9.                     using (Stream entryStream = file.GetInputStream(entry))  
  10.                     {  
  11.                         // TODO: set mime-type as defined in XPS package  
  12.                         context.Response.ContentType = "application/octet-stream";  
  13.                         byte[] buffer = new byte[2 << 14];    // write blocks of 32768 bytes  
  14.                         int read;  
  15.                         while ((read = entryStream.Read(buffer, 0, buffer.Length)) > 0)  
  16.                         {  
  17.                             context.Response.OutputStream.Write(buffer, 0, read);  
  18.                         }  
  19.                     }  
  20.                 }  
  21.                 else 
  22.                 {  
  23.                     // return 404 Not Found  
  24.                     context.Response.StatusCode = (int)HttpStatusCode.NotFound;  
  25.                 }  
  26.             }  
  27.  
  28.         } 

4. 源代码下载

点击

5. 在线预览

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

6. 截图

 

 

原文链接:http://www.cnblogs.com/guozili/archive/2012/07/16/2593437.html

【编辑推荐】

责任编辑:张伟 来源: guozili的博客
相关推荐

2012-06-18 16:29:48

Web

2012-06-18 16:37:41

Web

2012-06-19 13:25:15

Web

2012-06-19 13:32:23

Web

2015-10-28 13:40:28

高仿百度糯米源码

2012-06-19 13:45:57

Web

2012-10-19 09:47:30

百度云百度音乐云计算

2012-06-19 13:42:08

Web

2011-05-24 10:40:12

SEO

2011-06-29 16:02:40

jQuery

2013-08-22 17:08:50

2014-07-25 17:12:39

数据库WOT2014MongoDB

2023-10-12 17:37:50

百度文库百度世界大会

2017-09-12 17:05:02

AndroidLoading客户端

2020-12-03 06:13:46

iOS

2014-09-04 02:25:24

百度世界大会2014直达号BaiduEye

2012-03-23 12:12:37

百度开发者大会
点赞
收藏

51CTO技术栈公众号