类似百度文库在线预览文档flash版(支持word、excel、ppt、pdf)

在网上搜索如何和将office文档的pdf文档转换为flash,实现在线预览,你会发现有好几种解决方案

    a.使用flashpaper将需要的文档通过简单的设置转换为SWF格式的Flash,扩展阅读:http://baike.baidu.com/view/917746.htm,不过由于我的电脑室win7,而flashpaper又不支持win7,所以只好

    放弃此种方案。

    b.在网上发现可以使用swftools(http://www.swftools.org/谢天谢地啊,它支持win7)将pdf格式的文件转换为flash,但是不能讲office文档转换为flash,那怎么办呢,难道这种方法又不行,可是仔

    细想一下如果我们能将office文档先转换为pdf文件不就解决问题了吗,突然之间隐隐约约觉得使用office软件就可实现这一功能,于是打开word看一下究竟

    类似百度文库在线预览文档flash版(支持word、excel、ppt、pdf)_第1张图片哈哈果然有,不过要先装一个插件                  

    一切准备工作结束以后我们就可以进行编码了

    c.首先说一下swftools使用:在命令行中运行pdf2swf src.pdf des.swf一般能满足需求。然后就行主要的代码的编写(即将office文档转换为pdf文档)

    编写Office2Pdf.cs类

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Word = Microsoft.Office.Interop.Word;
using Excel = Microsoft.Office.Interop.Excel;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Core;

/// 
/// Office2Pdf 将Office文档转化为pdf
/// 
public class Office2Pdf
{
    public Office2Pdf()
    {
        //
// TODO: 在此处添加构造函数逻辑
//
    }
    /// 
/// Word转换成pdf
/// 
/// 源文件路径
/// 目标文件路径
/// true=转换成功
    public bool DOCConvertToPDF(string sourcePath, string targetPath)
    {
        bool result = false;
        Word.WdExportFormat exportFormat = Word.WdExportFormat.wdExportFormatPDF;
        object paramMissing = Type.Missing;
        Word.ApplicationClass wordApplication = new Word.ApplicationClass();
        Word.Document wordDocument = null;
        try
        {
            object paramSourceDocPath = sourcePath;
            string paramExportFilePath = targetPath;
            Word.WdExportFormat paramExportFormat = exportFormat;
            bool paramOpenAfterExport = false;
            Word.WdExportOptimizeFor paramExportOptimizeFor = Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
            Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument;
            int paramStartPage = 0;
            int paramEndPage = 0;
            Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent;
            bool paramIncludeDocProps = true;
            bool paramKeepIRM = true;
            Word.WdExportCreateBookmarks paramCreateBookmarks = Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;
            bool paramDocStructureTags = true;
            bool paramBitmapMissingFonts = true;
            bool paramUseISO19005_1 = false;
            wordDocument = wordApplication.Documents.Open(
                ref paramSourceDocPath, ref paramMissing, ref paramMissing,
                ref paramMissing, ref paramMissing, ref paramMissing,
                ref paramMissing, ref paramMissing, ref paramMissing,
                ref paramMissing, ref paramMissing, ref paramMissing,
                ref paramMissing, ref paramMissing, ref paramMissing,
                ref paramMissing);
            if (wordDocument != null)
                wordDocument.ExportAsFixedFormat(paramExportFilePath,
                    paramExportFormat, paramOpenAfterExport,
                    paramExportOptimizeFor, paramExportRange, paramStartPage,
                    paramEndPage, paramExportItem, paramIncludeDocProps,
                    paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
                    paramBitmapMissingFonts, paramUseISO19005_1,
                    ref paramMissing);
            result = true;
        }
        catch
        {
            result = false;
        }
        finally
        {
            if (wordDocument != null)
            {
                wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);
                wordDocument = null;
            }
            if (wordApplication != null)
            {
                wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
                wordApplication = null;
            }
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
        return result;
    }

    /// 
/// 把Excel文件转换成PDF格式文件  
/// 
/// 源文件路径
/// 目标文件路径
/// true=转换成功
    public bool XLSConvertToPDF(string sourcePath, string targetPath)
    {
        bool result = false;
        Excel.XlFixedFormatType targetType = Excel.XlFixedFormatType.xlTypePDF;
        object missing = Type.Missing;
        Excel.ApplicationClass application = null;
        Excel.Workbook workBook = null;
        try
        {
            application = new Excel.ApplicationClass();
            object target = targetPath;
            object type = targetType;
            workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,
                missing, missing, missing, missing, missing, missing, missing, missing, missing);
            workBook.ExportAsFixedFormat(targetType, target, Excel.XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
            result = true;
        }
        catch
        {
            result = false;
        }
        finally
        {
            if (workBook != null)
            {
                workBook.Close(true, missing, missing);
                workBook = null;
            }
            if (application != null)
            {
                application.Quit();
                application = null;
            }
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
        return result;
    }
    ///        
/// 把PowerPoint文件转换成PDF格式文件       
///        
///源文件路径     
///目标文件路径 
///true=转换成功 
    public bool PPTConvertToPDF(string sourcePath, string targetPath)
    {
        bool result;
        PowerPoint.PpSaveAsFileType targetFileType = PowerPoint.PpSaveAsFileType.ppSaveAsPDF;
        object missing = Type.Missing;
        PowerPoint.ApplicationClass application = null;
        PowerPoint.Presentation persentation = null;
        try
        {
            application = new PowerPoint.ApplicationClass();
            persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse); persentation.SaveAs(targetPath, targetFileType, Microsoft.Office.Core.MsoTriState.msoTrue);
            result = true;
        }
        catch
        {
            result = false;
        }
        finally
        {
            if (persentation != null)
            {
                persentation.Close();
                persentation = null;
            }
            if (application != null)
            {
                application.Quit();
                application = null;
            }
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
        return result;
    }
}


然后编写Pdf2Swf.cs类调用命令行将pdf转换为swf格式的flash

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Diagnostics;
using System.IO;

/// 
/// Pdf2Swf 将pdf转化为swf
/// 
public class Pdf2Swf
{
    public Pdf2Swf()
    {
        //
// TODO: 在此处添加构造函数逻辑
//
    }
    public void PDFConvertToSWF(string sourcePath, string targetPath)
    {
        Process p = new Process();
        p.StartInfo.FileName = "cmd.exe ";
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardInput = true;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.RedirectStandardError = true;
        p.StartInfo.CreateNoWindow = true;
        p.Start();
        string cmd = "pdf2swf.exe" + " " + sourcePath + " -o " + targetPath;
        p.StandardInput.WriteLine(cmd);
        p.Close();
    }
}


然后只需要调用这两个类就可以实现将office转换为pdf再转换为flash的功能了

 (3)最后就是实现flash的显示:这时我们还要借助工具来实现功能,那就是flexpaper,首先在网上下载所需要的flash文件、javascript文件,然后编写html代码:

 
 
    
     
        中勤文库-Flash版         
         
         
        
        
        
        
         
        

        
     
     
        

To view this page ensure that Adobe Flash Player version 10.0.0 or greater is installed.


效果就这样:

类似百度文库在线预览文档flash版(支持word、excel、ppt、pdf)_第2张图片还不错吧,上面的工具栏也可以实现各种功能

至此为止,所有工作全部结束,大家只需要将这些代码进行整合,配合数据库使用,编写管理后台,就可以实现百度文库的效果,甚至可以投入实际使用。

源码下载:http://files.cnblogs.com/expectszc/Web.zip

你可能感兴趣的:(NET)