最近在项目中遇到文档预览的需求,和PM商讨了几种解决方案,最终还是选中了转为SWF的方式。下面就稍微记录一下自己的学习成果。

  工具:pdf2swf 下载地址:http://www.swftools.org/download.html

  安装完成后,在安装目录下可以看到N个单独可以运行的exe文件:

  

  提供了多种格式转swf的功能,不过这里我只用了pdf2swf这一个,在我的项目里有一个service会将上传的文件直接转成pdf保存一个副档,需要预览的时候,直接获取这个pdf的副档就OK。

  下面看C#代码:

  

 public class PDF2Swf
{
#region
//根目录
private static string ROOT_PATH = AppDomain.CurrentDomain.BaseDirectory;
//pdf转swf
private static string PDF2SWF_PATH = "Shells\\SWFTools\\pdf2swf.exe";
//合并swf
private static string SWFCOMBINE_PATH = "Shells\\SWFTools\\swfcombine.exe";
//导航
private static string SWFVIEWER_PATH="Shells\\SWF\\rfxview.swf"; private static string SWFTEMP_PATH = "Shells\\SWF\\temp.swf";
//保存转成功的swf文件
public static string SAVE_SWF_PATH = "Shells\\SWF\\preview.swf";
//保存FLM上的PDF文档
public static string SAVE_PDF_PATH = "Shells\\PDF\\preview.pdf";
//语言包路径
private static string XPDF_LANG_PATH = ConfigReader.ReadValue<string>("XPDF_LANG_PATH"); public static string PREVIEW_PAGE_PATH = "Shells\\SWF\\preview.html";
#endregion ///<summary>
/// swf格式文件播放
///</summary>
///<param name="url"></param>
///<param name="width"></param>
///<param name="height"></param>
///<returns></returns>
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();
} ///<summary>
/// 传入PDF的文件路径,以及输出文件的位置,执行pdf2swf的命令
///</summary>
///<param name="strPDFPath"></param>
///<param name="strSwfPath"></param>
public static bool DoPDF2Swf(string strPDFPath, string strSwfPath)
{
bool isSuccess = false;
//如果PDF不存在
if (!File.Exists(strPDFPath))
{
return false;
} #region 清理之前的记录
if (File.Exists(strSwfPath))
{
//已经存在,删除
File.Delete(strSwfPath);
}
if (File.Exists(GetPath(SWFTEMP_PATH)))
{
File.Delete(GetPath(SWFTEMP_PATH));
}
#endregion //将pdf文档转成temp.swf文件
string strCommand = String.Format("{0} -T 8 -s languagedir={3} {1} -o {2}",
GetPath(PDF2SWF_PATH),strPDFPath, GetPath(SWFTEMP_PATH),XPDF_LANG_PATH);
double spanMilliseconds = RunShell(strCommand); //第一步转档失败,则返回
if (!File.Exists(GetPath(SWFTEMP_PATH)))
{
return false;
} //将temp.swf加入到rfxview.swf加入翻页的导航
strCommand = String.Format("{0} {1} viewport={2} -o {3}",
GetPath(SWFCOMBINE_PATH),GetPath(SWFVIEWER_PATH),GetPath(SWFTEMP_PATH),strSwfPath);
spanMilliseconds = RunShell(strCommand); if (File.Exists(strSwfPath))
{
isSuccess = true;
}
return isSuccess;
} ///<summary>
/// 获取文件全路径
///</summary>
///<param name="path"></param>
///<returns></returns>
public static string GetPath(string path)
{
//HttpContext.Current.Server.MapPath(path);
return String.Format("{0}{1}", ROOT_PATH, path);
} ///<summary>
/// 运行命令
///</summary>
///<param name="strShellCommand">命令字符串</param>
///<returns>命令运行时间</returns>
private static double RunShell(string strShellCommand)
{
double spanMilliseconds = ;
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;
} ///<summary>
/// 根据DownLoadURL从FLM获取资料,保存PDF文档到指定位置,返回文件的路径
///</summary>
///<param name="strDownLoadUrl"></param>
///<returns></returns>
public static string SavePDF(string strDownLoadUrl)
{
try
{
//截取FLM的FileID
string strFileID = strDownLoadUrl.Substring(strDownLoadUrl.LastIndexOf('=')+);
string strFileName = "";
AttachService service = new AttachService();
byte[] pdfBuffer = service.GetFileByte(strFileID, ref strFileName, "Y",Utility.GetProfile().englishName); string strPhysicalPDFPath = GetPath(SAVE_PDF_PATH); //如果已经有存在则先删掉
if (File.Exists(strPhysicalPDFPath))
{
File.Delete(strPhysicalPDFPath);
} //建一个PDF文档,将文件流写入文件保存
FileStream fs = new FileStream(strPhysicalPDFPath, FileMode.Create, FileAccess.Write);
fs.Write(pdfBuffer, , pdfBuffer.Length);
fs.Close(); return strPhysicalPDFPath;
}
catch (Exception ex)
{
throw new Exception("保存PDF文档失败:"+ex.Message);
}
} ///<summary>
/// 保证当前的文件名唯一
///</summary>
///<returns></returns>
private static string GetPDFName()
{
return DateTime.Now.ToLongTimeString().Replace(':','_')+DateTime.Now.Millisecond;
}
}

  使用的时候调用DoPDF2Swf(string strPDFPath, string strSwfPath)传入pdf以及输出的swf路径,

  任务会先调用pdf2swf.exe将pdf文档转成temp.swf文件:

    pdf2swf [-options] file.pdf -o file.swf

  

  然后再调用swfcombine.exe合并tmep.swf以及rfxview.swf文件,输出到preview.swf文件:

    swfcombine.exe rfxview.swf viewport={tmep.swf} -o {preview.swf}

  

  最后在页面中呈现。

 <html>

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

 <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
width="100%"
height="100%" codebase="http://active.macromedia.com/flash5/cabs/swflash.cab#version=8,0,0,0"> <param name="MOVIE" value="preview.swf"> <param name="PLAY" value="true"> <param name="LOOP" value="true"> <param name="QUALITY" value="high"> <param name="FLASHVARS" value="zoomtype=3"> <embed src="preview.swf" width="100%"
height="100%"
play="true" ALIGN="" loop="true" quality="high"
type="application/x-shockwave-flash" flashvars="zoomtype=3"
pluginspage="http://www.macromedia.com/go/getflashplayer"> </embed>
</object>
</body>
</html>

最终页面呈现效果:

  另外在学习的时候,其他高手有指点出语言包的问题,在我的代码里已经直接加进入去了<有时间在把处理中文的具体方法拿出来小结一下>http://www.cnblogs.com/liver.wang/archive/2011/10/27/PDF2SWFChinese.html

  xpdf-3.02pl5\\xpdf-chinese-simplified

  

  

PDF2SWF简单使用的更多相关文章

  1. pdf2swf , xpdf 部分用法

    http://hi.baidu.com/abpsoft/item/1d1eb0f50c9d1fd86225d2c0 pdf2swf详细参数使用说明 官方地址:http://www.swftools.o ...

  2. 利用pdf2swf将PDF转换成SWF

    将PDF转换成SWF可以使用SWFTools工具中的pdf2swf(http://www.swftools.org/),CSDN快速免积分下载地址http://download.csdn.net/de ...

  3. 【造轮子】打造一个简单的万能Excel读写工具

    大家工作或者平时是不是经常遇到要读写一些简单格式的Excel? shit!~很蛋疼,因为之前吹牛,就搞了个这东西,还算是挺实用,和大家分享下. 厌烦了每次搞简单类型的Excel读写?不怕~来,喜欢流式 ...

  4. Fabio 安装和简单使用

    Fabio(Go 语言):https://github.com/eBay/fabio Fabio 是一个快速.现代.zero-conf 负载均衡 HTTP(S) 路由器,用于部署 Consul 管理的 ...

  5. node.js学习(三)简单的node程序&&模块简单使用&&commonJS规范&&深入理解模块原理

    一.一个简单的node程序 1.新建一个txt文件 2.修改后缀 修改之后会弹出这个,点击"是" 3.运行test.js 源文件 使用node.js运行之后的. 如果该路径下没有该 ...

  6. 哪种缓存效果高?开源一个简单的缓存组件j2cache

    背景 现在的web系统已经越来越多的应用缓存技术,而且缓存技术确实是能实足的增强系统性能的.我在项目中也开始接触一些缓存的需求. 开始简单的就用jvm(java托管内存)来做缓存,这样对于单个应用服务 ...

  7. 在Openfire上弄一个简单的推送系统

    推送系统 说是推送系统有点大,其实就是一个消息广播功能吧.作用其实也就是由服务端接收到消息然后推送到订阅的客户端. 思路 对于推送最关键的是服务端向客户端发送数据,客户端向服务端订阅自己想要的消息.这 ...

  8. 我的MYSQL学习心得(一) 简单语法

    我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) 数据类型 我的MYSQL学习心得(五) 运 ...

  9. 使用 Nodejs 搭建简单的Web服务器

    使用Nodejs搭建Web服务器是学习Node.js比较全面的入门教程,因为要完成一个简单的Web服务器,你需要学习Nodejs中几个比较重要的模块,比如:http协议模块.文件系统.url解析模块. ...

随机推荐

  1. Android 查看联系人电话和姓名(ContentProvider)

    1.介绍 2.使用方法 3.在AndroidManifest.xml文件中添加相关设置 <uses-permission android:name="android.permissio ...

  2. python之读取文件的测试数据

    假设我们有一个叫testdata.txt的文件,现在在这个文件里面有测试数据,我们怎么利用前2小章学习的知识,读取测试数据呢? 测试数据如下: url:https://www.cnblogs.com/ ...

  3. async中series的实现 javascript构件

    //同步流程 var series=function(arr){ function async(i){ arr[i](function(){ if(1+i<arr.length){ async( ...

  4. server 2012 R2查询端口

    1. win+r弹出运行对话框,输入cmd,打开cmd窗口 netstat -ano | findstr "80" (注80是你想要看查看的端口号) 就会输出包含80端口使用的情况 ...

  5. git撤销commit,但未git push的命令

    在git push的时候,有时候我们会想办法撤销git commit的内容 1.找到之前提交的git commit的id git log 找到想要撤销的id 2.git reset –hard id ...

  6. zabbix-proxy 层级制监控

    一,zabbix服务规划 zabbix-server 10.0.0.71 zabbix-proxy 10.0.0.72 172.16.1.72 web01  172.16.1.7 二,zabbix 客 ...

  7. console命令详解:(转载学习)

    Console命令详解,让调试js代码变得更简单   Firebug是网页开发的利器,能够极大地提升工作效率. 但是,它不太容易上手.我曾经翻译过一篇<Firebug入门指南>,介绍了一些 ...

  8. ife task0003学习笔记(二):JavaScript原型

    function aaa(){} aaa.prototype.bbb=function(){} var obj1=new aaa() var obj2=new aaa() obj1和obj2都有一个属 ...

  9. Windows 下推荐软件

    神器 Dism++ Quicker(效率工具) Bandizip 火绒安全软件 Everyting(搜索神器并支持http远程连接) Xmanager VMware Workstation IDMan ...

  10. 51nod 1245 Binomial Coefficients Revenge

    Description C(M,N) = M! / N! / (M - N)! (组合数).给出M和质数p,求C(M,0), C(M,1)......C(M,M)这M + 1个数中,有多少数不是p的倍 ...