silverlight仿“百度文庫”的文檔控件
通用的FlashPaper支持Word/Excel/PDF,到時對于Silverlight的XPS的文檔支持問題比較多,本控件提供了一個可視化的XPS文檔展示,提供放大縮小/打印/搜索/分頁等功能,主要整合了開源的Document Toolkit。
1. 使用Document Toolkit,DocumentDataSource提供數(shù)據(jù)源,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或遠(yuǎn)程xps文件作為數(shù)據(jù)源
DotNetZipPackageReader 根據(jù)分頁延遲加載文檔
- // 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. 服務(wù)端根據(jù)請求的文件ID和當(dāng)前頁碼返回指定的文件流
- 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. 源代碼下載
5. 在線預(yù)覽
http://rapidsl2.guozili.25u.com/ (admin/admin 點左邊菜單 控件展示 - 文檔查看器)
6. 截圖
原文鏈接:http://www.cnblogs.com/guozili/archive/2012/07/16/2593437.html
【編輯推薦】