实现WORD及PDF文档在线阅读 ASPNET FLEX PAPER

一、做这个功能的原因

    在一个项目中,有个文档管理模块。这个模块呢是让用户上传文档并填写文档相关的信息,存入服务器后,可以对文档进行删除、下载以及在线阅读等操作。前两个操作(删除、下载)比较的简单在这就不提出实现方法,本文只针对后者做详细的论述。

 

二、需要安装的软件

v Office 2007 Office 2007 SP2 补丁,用于将WORD文件转换成PDF文件;

v SWFTools,用于将PDF文件转换成SWF文件;

v Flex Player,用于呈现转换后的SWF文件;

 

Office2007Office 2007 SP2补丁可以闭着眼睛安装,但SWFTools需要注意一下,它的安装路径最好不要有空格,像Program Files就有空格,为避免在程序使用过程中出现问题,尽可能安装到磁盘的根目录。如:“C:\SWFTOOLS\”;

Flex Player下载解压后将三个JS文件和FlexPaperViewer文件复制到解决方案下,三个JS分别是flexpaper_flashflexpaper_flash_debug以及jquery

 

三、转换文档的步骤

第一步:将WORD转成PDF

第二步:将PDF转成SWF

如果您要阅读的文件是PDF的话,可省去第一步。

 

四、环境设置

根据您的系统环境二选其一:

IIS5.1】将项目的应用池属性中"标识""预定义账户"设置成"本地系统"

IIS6-IIS7】打开项目的应用程序池"高级设置",将"标识"设置成"LocalSystem"

 

打开"运行"-"dcomcnfg"-"组件服务"-"计算机"-"我的电脑"-"DCOM配置",将"Microsoft Office Word 97 - 2003 文档"属性的"常规""身份验证级别"设置为"";然后将"安全""启动和激活权限""访问权限""配置权限""自定义"中加入Everyone,被赋予所有权限。最后将“标识”中"交互式用户"选中。

 

 

由于Flexpaper还没获得Adobe Flash的信任,需要前住:

http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04a.html#119065

FlexPaper添加信任.进入网页之后 ,点击左边的 Global Security Settings papel 这个选项,为你的项目所在的文件夹添加信任。

 

 

五、代码转换

页面源代码:

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <script type="text/javascript" src="../js/flexpaper_flash_debug.js"></script>
    <script type="text/javascript" src="../js/flexpaper_flash.js"></script>
    <script type="text/javascript" src="../js/jquery.js"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div id="shtml" runat="server" style="margin: auto; width: 90%; height: 600px; border: 3px solid #999;">
    </div>
    </form>
</body>
</html>


 

第一步DOC2PDF

/// <summary>
    /// 把Word文件转换成为PDF格式文件
    /// </summary>
    /// <param name="sourcePath">源文件路径</param>
    /// <param name="targetPath">目标括文件路径</param>
    /// <returns></returns>
    private 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 (Exception ex)
        {
            Response.Write(ex.Message);
            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;
    } 


 

第二步PDF2SWF

 /// <summary>
    /// 把PDF文件转换成为SWF格式文件
    /// </summary>
    /// <param name="fileName"></param>
    private static void ConvertCmd(string fileName)
    {
        using (Process p = new Process())
        {
            string cmdStr = @"C:/SWFTools/pdf2swf.exe";
	   // HttpContext.Current.Server.MapPath("~/SWFTools/pdf2swf.exe");
            string savePath = HttpContext.Current.Server.MapPath("~/filefactory/");
            string sourcePath = @"""" + savePath + fileName + @"""";
            string targetPath = @"""" + savePath + fileName.Substring(0, fileName.LastIndexOf(".")) + ".swf" + @"""";
            string argsStr = "  -t " + sourcePath + " -s flashversion=9 -o " + targetPath;
            ProcessStartInfo psi = new ProcessStartInfo(cmdStr, argsStr);
            p.StartInfo = psi;
            p.Start();
            p.WaitForExit();
        }
    }


 

第三步显示SWF文件:

	protected void InitSWF(string guid)
    {
        string swfhtml = "<a id='viewerPlaceHolder' style='width: 100%; height: 98%; display: block'></a><script type='text/javascript'>var fp = new FlexPaperViewer('FlexPaperViewer','viewerPlaceHolder', { config: {SwfFile: escape('../filefactory/" + guid + ".swf'),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,InitViewMode: 'Portrait',PrintPaperAsBitmap: false,ViewModeToolsVisible: true,ZoomToolsVisible: true,NavToolsVisible: true,CursorToolsVisible: true,SearchToolsVisible: true,localeChain: 'en_US'} });</script>";
        this.shtml.InnerHtml = swfhtml;
    }


 

 

六、呈现

在方法中调用一二三步:

protected void Page_Load(object sender, EventArgs e)

    {

string guid = "3c3d23";

        //假定阅读的文件在filefactory目录下

        if (DOCConvertToPDF(Server.MapPath("~/filefactory/" + guid + ".doc"), Server.MapPath("~/filefactory/" + guid + ".pdf")))

        {

            ConvertCmd(guid + ".pdf");

            InitSWF(guid);

        }

    }

 

七、TIPS

l 代码和环境设置看似复杂,其实挺简单的。

l 我把完整的项目放了上来,大家下载看后会更明了。[下载源码]

l 有问题可发邮件至:sevenj@yeah.net新浪:@开不开都是大

l 祝大家工作顺利

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值