03
2023
05
09:18:39

页面中展示PDF(转成Swf文件)



推荐点击下面图片,通过本站淘宝优惠价购买:

image.png

由于直接在页面上展示PDF会有编码困难及不安全等问题,所以想到先利用SWFTOOLS工具把PDF转成SWF格式的Flash文件然后在页面上利用ASP.NET自带的Flash播放器展示Flash文件,这样即简便又安全。


我们可以在用户上传PDF文件时把它转成SWF格式的Flash文件,这样就可以在用户需要查阅自己的PDF文件时直接调用Flash展示。


1.      下载SWFTOOLS软件,下载地址:http://www.swftools.org/download.html


2.      下载两个中文字体Gbsn00lp.ttf和gkai00mp.ttf,下载地址:http://code.google.com/p/atyu30/downloads/detail?name=gbsn00lp.ttf.tar.gz&can=2&q=


       http://download.csdn.net/detail/blackjack2007u/1841186。


3.xpdfbin-win-3.03.zip、xpdf-chinese-simplified.tar.gz 


  下载地址:http://www.foolabs.com/xpdf/download.html


4.      首先安装SWFTOOLS,不考虑中文的情况下,一个pdf2swf.exe已经足够我们使用转换任务。安装完成   后,在安装目录下可以看到多个单独可以运行的exe文件:

我们只需要使用gpdf2swf.exe应用程序。


4.  因为C#代码中不能直接调用应用程序,所以我们应该调用运行命令CMD                来调用gpdf2swf.exe应用程序。


5.  SWFTOOLS安装完成后,就可以使用,但因为该程序不能识别中文,所                    以下面解决中文字符的问题。


a.解压缩xpdfbin-win-3.03.zip到指定目录(C:\xpdf)


b.解压缩xpdf-chinese-simplified.tar.gz到上面的目录下


c.拷贝两个字体文件gkai00mp.ttf、Gbsn00lp.ttf到CMap目录下


d.修改C:\xpdf\xpdf-chinese-simplified下的add-to-xpdfrc文件将路径该为本机安装                     路径



e. 修改C:\xpdf\xpdfbin-win-3.03\doc 下的xpdfrc文件 ,把地址修改为本机地址



 部署步骤:

1.上述步骤完成后我们就能在C#代码中使用了,可以先在CMD中试用


命令解释:-o后为你所要保存已转换成功的SWF文件保存地址,-t后为你需要转换的PDF的地址,-t后为调用中文字符

2.在C#中调用,贴上代码。

    public partial class upload : System.Web.UI.Page

    {

        #region


//根目录

        private static string ROOT_PATH = AppDomain.CurrentDomain.BaseDirectory;

        //pdf转swf

        private static string PDF2SWF_PATH = "D:\\PDFToSwf\\pdf2swf.exe";

        //语言包路径

        private static string XPDF_LANG_PATH = "C:\\xpdf\\xpdf-chinese-simplified";

        //swf合并程序,目的为可以有翻页功能

        private static string SWFCOMBINE_PATH = "D:\\PDFToSwf\\swfcombine.exe";

        //SWFTOOLS工具自带的翻页FLASH

        private static string SWFVIEWER_PATH = "D:\\PDFToSwf\\rfxview.swf";


#endregion


///

        /// 运行Cmd命令

        ///

        ///命令字符串

        ///命令运行时间

        private static double RunShell(string strShellCommand)

        {

            double spanMilliseconds = 0;

            DateTime beginTime = DateTime.Now;


Process cmd = new Process();

            cmd.StartInfo.FileName = "cmd.exe";

            cmd.StartInfo.UseShellExecute = false;

            cmd.StartInfo.CreateNoWindow = true;

            cmd.StartInfo.Arguments = String.Format(@"/c {0}", strShellCommand);

            cmd.Start();

            cmd.WaitForExit();

            cmd.Close();


DateTime endTime = DateTime.Now;

            TimeSpan timeSpan = endTime - beginTime;

            spanMilliseconds = timeSpan.TotalMilliseconds;


return spanMilliseconds;

        }


public static bool DoPDF2Swf(string strPDFPath, string strSwfPath)

        {

            bool isSuccess = false;

            //如果PDF不存在

            if (!File.Exists(strPDFPath))

            {

                return false;

            }


#region 清理已存在的Swf记录


if (!File.Exists(strSwfPath))

            {

                //已经存在,删除

                File.Delete(strSwfPath);

            }

               

            #endregion


#region 将PDF转成Swf文件


string strCommand = string.Format("{0} -o {1} -t {2} -s languagedir={3}",

                                PDF2SWF_PATH, strSwfPath, strPDFPath, XPDF_LANG_PATH);

            double spanMilliseconds = RunShell(strCommand);



            strCommand = String.Format("{0} {1} viewport={2} -o {3}",

                SWFCOMBINE_PATH, SWFVIEWER_PATH, strSwfPath, strSwfPath);

            spanMilliseconds = RunShell(strCommand);


//第一步转档失败,则返回

            if (!File.Exists(strSwfPath))

            {

                return false;

            }


if (File.Exists(strSwfPath))

            {

                isSuccess = true;

            }


return isSuccess;


#endregion

        }


protected void Page_Load(object sender, EventArgs e)

        {

            HttpPostedFile httpFile = Request.Files["FileData"];

            if (httpFile != null)

            {

                string fileName = httpFile.FileName;

                #region

                //string preStr = "";

                //WebRequest req = WebRequest.Create(imgurlAry[i]);

                //preStr = System.DateTime.Now.ToString() + "_";

                //preStr = preStr.Replace("-", "");

                //preStr = preStr.Replace(":", "");

                //preStr = preStr.Replace(" ", "");

                //preStr = "111";

                //string uploadPath = Server.MapPath("temp\") + preStr + fileName;

                //string uploadPath = "D:\\temp\" + fileName;

                #endregion


            //加入@""""是为了解决CMD不能识别空格的问题

                string uploadPath = @""""+Server.MapPath("PDFFile\") + fileName+@"""";

                if (Directory.Exists(Server.MapPath("PDFFile\")))

                {

                    httpFile.SaveAs(uploadPath);

                }

                else

                {  

                    Directory.CreateDirectory(Server.MapPath("PDFFile\"));

                    httpFile.SaveAs(uploadPath);

                }

                string _fileName = Path.GetFileNameWithoutExtension(uploadPath);//获取文件名,除去后缀     

                string strSwfPath = Server.MapPath("SwfFile\") + _fileName + ".swf";


DoPDF2Swf(uploadPath, strSwfPath);



                Session["filelist"] += uploadPath + "|";

                Response.Write("文件: " + uploadPath + " 上传成功!");

            }

        }

    }

3.完成上述步骤后,PDF文件就成功转换成SWF文件了,最后在页面上展示,贴上代码。


        public static string AddSwf(string url)

        {

            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            sb.Append("<OBJECT codeBase='http://active.macromedia.com/flash5/cabs/swflash.cab#version=8,0,0,0'");

            sb.Append(" height='100%' width='100%' classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000'>");

            sb.Append("<PARAM NAME='Movie' VALUE='" + url + "'>");

            sb.Append("<PARAM NAME='Play' VALUE='true'>");

            sb.Append("<PARAM NAME='Loop' VALUE='true'>");

            sb.Append("<PARAM NAME='Quality' VALUE='High'>");

            sb.Append("<PARAM NAME='FLASHVARS' VALUE='zoomtype=3'>");

            sb.Append("<embed src='" + url + @"' height='100%' height='100%' play='true' ALIGN='' loop='true' quality='high'             

                         pluginspage='http://www.macromedia.com/go/getflashplayer'            

                         type='application/x-shockwave-flash' flashvars='zoomtype=3'>");

            sb.Append("</embed>");

            sb.Append("</OBJECT>");

            return sb.ToString();        }

然后aspx上调用,贴上代码


<head id="Head1" runat="server">

    <title></title>

</head>

<body style="padding: 0px; margin: 0px">

    <div>

        <%=AddSwf("SwfFile/222.swf")%>

    </div>

</body>

</html>

最后页面展示效果,有百度文库的感觉有么有



729bcbb54922a504c3f0c7956c60a7a5_Center.gif


     



本文链接:http://www.hqyman.cn/post/4029.html 非本站原创文章欢迎转载,原创文章需保留本站地址!

分享到:





休息一下,本站随机推荐观看栏目:


« 上一篇 下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

您的IP地址是: