1 比較老的一種方法了 Aspose.dll+SwfTool+flexpaperjavascript
能夠實現excel表格 world文檔 pdf文件的在線預覽。css
首先利用Aspose將文件轉換爲pdf格式。Aspose.Words是一款先進的類庫,經過它能夠直接在各個應用程序中執行各類文檔處理任務。它裏面包含了幾個主要的能夠操做不一樣格式文件的dll組件,咱們這裏主要使用了Aspose.worlds和Aspose.cells。其實它們能實現更爲複雜的文件生成和操做,可是我這裏只使用了它們的文件格式轉換功能。我下載的是aspose 的破解版本。把這兩個dll引到本身的項目裏。寫一個方法用來轉換格式。html
public class AsPoseHelper { //將word文檔轉換成pdf public static bool GetPdfFromWord(string soursefilepath, string outpdfpath) { //讀取word文檔 try { using (System.IO.Stream stream = new System.IO.FileStream(soursefilepath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { Aspose.Words.Document doc = new Aspose.Words.Document(soursefilepath); doc.Save(outpdfpath, Aspose.Words.SaveFormat.Pdf); return true; } } catch (Exception ex) { return false; } } //excel文檔轉換問題 public static bool GetPdfFromExcel(string soursefilepath, string outpdfpath) { try { using (System.IO.Stream stream = new System.IO.FileStream(soursefilepath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { Aspose.Cells.Workbook workbook = new Workbook(stream); PdfSaveOptions pdfSaveOptions = new PdfSaveOptions(); pdfSaveOptions.Compliance = PdfCompliance.PdfA1b; workbook.Save(outpdfpath, Aspose.Cells.SaveFormat.Pdf); return true; } } catch (Exception ex) { return false; } } }
而後,將轉換成pdf格式的文件轉成swf格式,這裏我使用的是swftool。裏面的pdf2swf.exe,這個也是有別的相互轉換的功能,可是我這裏只用了這個exe。java
public static bool GetSwfFromPdf(string pdfpath, string swfpath) { try { string exepath = HttpContext.Current.Server.MapPath("~/exe/pdf2swf.exe"); StringBuilder sb = new StringBuilder(); sb.Append("\"" + pdfpath + "\"");//輸入位置 sb.Append(" -T 9");//flash版本 sb.Append(" -o " + "\"" + swfpath + "\"");//輸出位置 sb.Append(" -j 100");//生成flash的質量 if (!File.Exists(exepath)) { //exe程序 不存在 return false; } else { //拼接cmd命令行 string command = "cmd.exe /C " + Path.GetFileName(exepath) + " " + sb.ToString(); using (Process process = new Process()) { ProcessStartInfo startinfo = new ProcessStartInfo(); startinfo.FileName = "cmd.exe"; startinfo.Arguments = command; startinfo.WorkingDirectory = Path.GetDirectoryName(exepath); process.StartInfo = startinfo; process.Start(); process.WaitForExit(); if (File.Exists(HttpContext.Current.Server.MapPath(swfpath)))//代表寫入成功 { return true; } else { return false; } } } } catch (Exception ex) { return false; } }
這裏是先拼接好轉換命令再使用cmd調用pdf2swf.exe進行轉換。flex
最後,使用flexpaper把生成的swf文件在頁面上顯示。到flexpaper的官網上下載js文件。在你的頁面裏面把<script src="~/Scripts/js/flexpaper.js"></script><link href="~/Scripts/css/flexpaper.css" rel="stylesheet" />這兩個引進來。swffile的地址是上面生成的swf文件的地址。ui
<div id="documentViewer" class="flexpaper_viewer" style="position:relative;width:80%;height:98%"></div> <script type="text/javascript"> $("#documentViewer").FlexPaperViewer( { config: { SWFFile: @ViewBag.URL, Scale: 0.6, ZoomTransition: 'easeOut', ZoomTime: 0.5, ZoomInterval: 0.2, FitPageOnload: true, FitWidthOnLoad: false, FullScreenAsMaxWindow: false, ProgressiveLoading: false, MinZoomSize: 0.2, MaxZoomSize: 5, SearchMatchAll: false, // Toolbar: toolbarData, //BottomToolbar: 'UI_flexpaper_annotations.html', InitViewMode: 'Portrait', RenderingOrder: 'flash', StartAtPage: '', ViewModeToolsVisible: true, ZoomToolsVisible: true, NavToolsVisible: true, CursorToolsVisible: true, SearchToolsVisible: true, WMode: 'window', localeChain: 'en_US' } } ); </script>
2 不把pdf文件轉換成swf,aspose+pdfobject.js url
pdfobject.js是在網頁中顯示pdf文件的插件,很是簡單好用。插件
首先在官網上下載文件 https://pdfobject.com/ 將js文件引用到你的頁面中。命令行
@{ ViewBag.Title = "Index"; } <h2>Index</h2> <script src="~/pdfobject/pdfobject.js"></script> <style type="text/css"> .pdfobject-container { height: 900px; } .pdfobject { border: 1px solid #666; } </style> <div id="exm"> </div> <script type="text/javascript"> var url = "./Content/test.pdf"; PDFObject.embed(url, "#exm"); </script>
而後就能夠了。excel
3 aspose+pdf.js