文档在线预览的几种实现方法

1 比较老的一种方法了 Aspose.dll+SwfTool+flexpaper

可以实现excel表格 world文档 pdf文件的在线预览。

首先利用Aspose将文件转换为pdf格式。Aspose.Words是一款先进的类库,通过它可以直接在各个应用程序中执行各种文档处理任务。它里面包含了几个主要的可以操作不同格式文件的dll组件,我们这里主要使用了Aspose.worlds和Aspose.cells。其实它们能实现更为复杂的文件生成和操作,但是我这里只使用了它们的文件格式转换功能。我下载的是aspose 的破解版本。把这两个dll引到自己的项目里。写一个方法用来转换格式。

 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。

     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进行转换。

最后,使用flexpaper把生成的swf文件在页面上显示。到flexpaper的官网上下载js文件。在你的页面里面把<script src="~/Scripts/js/flexpaper.js"></script><link href="~/Scripts/css/flexpaper.css" rel="stylesheet" />这两个引进来。swffile的地址是上面生成的swf文件的地址。

    <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  

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>

  

 然后就可以了。

3 aspose+pdf.js

 

posted @ 2016-07-29 16:59  喵~喵  阅读(16131)  评论(3编辑  收藏  举报