GroupDocs.Viewer是一個在線文檔查看器,無論是否安裝了建立某個文檔的軟件,GroupDocs.Viewer都容許使用瀏覽器查看這個文檔。GroupDocs.Viewer支持查看多種文件文檔(DOC、DOCX、TXT、RTF、ODT),演示文檔(PPT、PPTX),電子表格(XLS、XLSX),便攜式文件(PDF)以及圖像文件(JPG、BMP、TIFF)。javascript
咱們這個項目就是探尋一款乾淨簡便的HTML5文檔查看器,關於這方面的產品是不少的,因此這個探索過程是很繁瑣的,不少人的第一想法就是是使用Google Docs Viewer或相似的解決方案,但因爲應用程序將在客戶端Intranet上運行,申請和文件都不容許暴露在互聯網上,因此這是不可行的。web
通過探討,咱們制定瞭如下標準:瀏覽器
適用於Windows Server和.NET。 服務器
支持PDF,包括新舊MS Office格式。 測試
在不離開觸摸應用程序環境的情 最好支持從URL或.NET流中讀取文檔。 fetch
無需在客戶端計算機上安裝額外的插件。網站
探索this
接下來就是大量的研究探索,最終咱們尋找到了三個備選產品。url
Accusofts Prizm Client Connect
這款產品符合咱們的大多數標準,支持各類格式,但有一些缺點。它須要安裝一個單獨的服務器來進行文檔轉換。從他們提供下載的示例項目來判斷,它須要編寫大量代碼才能使其打勾。除此以外,它只提供從文件路徑加載文檔的可能性。
Snowbounds Virtualviewer
這款產品的必須做爲服務器上的單獨網站運行,能夠經過添加提供程序來擴展文檔的加載方式,在示例項目中支持文件和URL。可是用戶界面有點擁擠,視覺體驗不太好,並且還有許多不經常使用的功能。
雖然這兩種產品可能已經完成了這項工做,但我測試的第三種產品可以彌補以上兩種產品的缺點。
與其餘產品相比,這是很輕便的一個產品,只需添加一個DLL便可,它有一個很是簡潔的API。 最初不支持咱們想要的打印的要求,但在與Groupdocs.交談並解釋須要的功能後,他們很快就推出瞭解決它的新版本。
添加代碼
在ASP.NET MVC應用程序中使用GroupDocs Viewer,必須完成四個步驟。
第一步:引用dll到項目中去
第二步:將如下項目加入Global.asax.
Viewer.InitRoutes(); Viewer.SetRootStoragePath(Server.MapPath("SomePath")); // Documents will also be cached here Viewer.SetLicensePath(Server.MapPath("SomePathToYourLisenceFile"));
第三步:在web.config文件中添加一些處理程序。
<add name="ViewDocumentHandler" verb="GET,POST" path="document-viewer/ViewDocumentHandler" type="Groupdocs.Web.UI.Handlers.ViewDocumentHandler, Groupdocs.Viewer, Culture=neutral" /> <add name="GetDocumentPageImageHandler" verb="GET,POST" path="document-viewer/GetDocumentPageImageHandler" type="Groupdocs.Web.UI.Handlers.GetDocumentPageImageHandler, Groupdocs.Viewer, Culture=neutral" /> <add name="LoadFileBrowserTreeDataHandler" verb="GET,POST" path="document-viewer/LoadFileBrowserTreeDataHandler" type="Groupdocs.Web.UI.Handlers.LoadFileBrowserTreeDataHandler, Groupdocs.Viewer, Culture=neutral" /> <add name="GetImageUrlsHandler" verb="GET,POST" path="document-viewer/GetImageUrlsHandler" type="Groupdocs.Web.UI.Handlers.GetImageUrlsHandler, Groupdocs.Viewer, Culture=neutral" /> <add name="GetCssHandler" verb="GET" path="document-viewer/CSS/GetCssHandler" type="Groupdocs.Web.UI.Handlers.CssHandler, Groupdocs.Viewer, Culture=neutral" /> <add name="images" verb="GET" path="document-viewer/images/*" type="Groupdocs.Web.UI.Handlers.EmbeddedImageHandler, Groupdocs.Viewer, Culture=neutral" /> <add name="GetScriptHandler" verb="GET,POST" path="document-viewer/GetScriptHandler" type="Groupdocs.Web.UI.Handlers.ScriptHandler, Groupdocs.Viewer, Culture=neutral" /> <add name="GetFileHandler" verb="GET" path="document-viewer/GetFileHandler" type="Groupdocs.Web.UI.Handlers.GetFileHandler, Groupdocs.Viewer, Culture=neutral" /> <add name="GetPdf2JavaScriptHandler" verb="GET,POST" path="document-viewer/GetPdf2JavaScriptHandler" type="Groupdocs.Web.UI.Handlers.GetPdf2JavaScriptHandler, Groupdocs.Viewer, Culture=neutral" /> <add name="GetPdfWithPrintDialogHandler" verb="POST" path="document-viewer/GetPdfWithPrintDialogHandler" type="Groupdocs.Web.UI.Handlers.GetPdfWithPrintDialogHandler, Groupdocs.Viewer, Culture=neutral" /> <add name="GetPrintableHtmlHandler" verb="GET,POST" path="document-viewer/GetPrintableHtmlHandler" type="Groupdocs.Web.UI.Handlers.GetPrintableHtmlHandler, Groupdocs.Viewer, Culture=neutral" />
第四步:在視圖中加入如下內容
//loads the javascripts that groupsdocs viewer needs@Html.CreateViewerScriptLoadBlock().LoadJquery().LoadJqueryUi() @(Html.ViewerClientCode() .TargetElementSelector("#documentContainer") .Stream(SomeDotNetStream) // fetch document from a stream //.Url("SomeUrl") fetch from a url //.FilePath("SomeFile") fetch from filepath .DocViewerId("SomeViewerId") //different functionality can be turned on and off, this is just an example on how we have set them .EnableRightClickMenu(true) .ShowThumbnails(false) .OpenThumbnails(false) .ShowFolderBrowser(false) .ShowDownload(false) .ShowViewerStyleControl(false) .ShowSearch(false) .UsePdfPrinting(false) .BackgroundColor("black") .Width(960) .Height(900) .ZoomToFitWidth(true) .Locale("nb-no") )