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

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

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

下面看C#代码:

View Code

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("XPDF_LANG_PATH");

public static string PREVIEW_PAGE_PATH = "Shells\\SWF\\preview.html";

#endregion

//swf格式文件播放/// public static string AddSwf(string url)

{

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

sb.Append("

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

sb.Append("");

sb.Append("");

sb.Append("");

sb.Append("");

sb.Append("");

sb.Append("

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

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

sb.Append("

");

sb.Append("

");

return sb.ToString();

}

//传入PDF的文件路径,以及输出文件的位置,执行pdf2swf的命令/ 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;

}

//获取文件全路径/ public static string GetPath(string path)

{

//HttpContext.Current.Server.MapPath(path); return String.Format("{0}{1}", ROOT_PATH, path);

}

//运行命令//命令字符串///命令运行时间 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;

}

//根据DownLoadURL从FLM获取资料,保存PDF文档到指定位置,返回文件的路径/ public static string SavePDF(string strDownLoadUrl)

{

try

{

//截取FLM的FileID string strFileID = strDownLoadUrl.Substring(strDownLoadUrl.LastIndexOf('=')+1);

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, 0, pdfBuffer.Length);

fs.Close();

return strPhysicalPDFPath;

}

catch (Exception ex)

{

throw new Exception("保存PDF文档失败:"+ex.Message);

}

}

//保证当前的文件名唯一// 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}

最后在页面中呈现。

1

2

3

4

5

10

11

12

13

14

15

16

17

18

19

20

21

28

29

30

31

32

c执行cmd pdf2swf_PDF2SWF简单使用相关推荐

  1. c执行cmd pdf2swf_pdf2swf 和pdf2html 使用命令详解

    pdf2swf 将pdf文档转换为flash方式阅读,可以满足公式.图片的格式定义: pdf2htmlEX 将pdf文档转换为html方式阅读,有一下优点: 在HTML文件中精确显示原生文本 保持PD ...

  2. 怎样在Python中执行cmd

    这篇文章主要给大家分享在Python中执行cmd,下文描述了三个方法使用os.system()方法.使用os.popen()方法.使用subprocess.Popen(),需要的朋友可以参考一下 1. ...

  3. java -version cmd_java如何运行步骤cmd?Java执行cmd命令方法有哪些?

    写好一个java程序之后,我们的最终目的就是可以正确的运行程序,如果程序运行正确了,那么代码也就没有什么问题了,可是java如何运行步骤cmd?接下来,我们就来给大家讲解一下这方面的内容. 1.首先用 ...

  4. python执行cmd并返回是否成功_python脚本执行CMD命令并返回结果的例子

    最近写脚本的时想要用python直接在脚本中去执行cmd命令,并且将返回值打印出来供下面调用,所以特意查了下,发现主要有一下几种方式来实现,很简单: 就拿执行adb, adb shell, adb d ...

  5. python中cmd全称_【转】Python中执行cmd的三种方式

    原文链接:http://blog.csdn.net/menglei8625/article/details/7494094 目前我使用到的python中执行cmd的方式有三种: 1. 使用os.sys ...

  6. python 调用控制台并获取返回结果_python脚本执行CMD命令并返回结果的例子

    最近写脚本的时想要用python直接在脚本中去执行cmd命令,并且将返回值打印出来供下面调用,所以特意查了下,发现主要有一下几种方式来实现,很简单: 就拿执行adb, adb shell, adb d ...

  7. python脚本执行CMD命令并返回结果

    最近写脚本的时想要用python直接在脚本中去执行cmd命令,并且将返回值打印出来供下面调用,所以特意查了下,发现主要有一下几种方式来实现,很简单: 就拿执行adb,   adb shell, adb ...

  8. LadonGo实现菜刀连接webshell一句话执行cmd代码

    背景 最近VPS被人D比较卡,有时候M都不定连得上,或者连上了也难代理出来,所以需要一个命令行下连接内网WEBSHELL执行命令的工具,当然这个功能Ladon早有了.主要是因为在Linux下横向渗透连 ...

  9. java 执行 cmd 命令(转)

    原文出处:http://blog.csdn.net/saindy5828/article/details/11975527 用JAVA代码实现执行CMD命令的方法 java的Runtime.getRu ...

最新文章

  1. [原] Jenkins Android 自动打包配置
  2. Silverlight Tips(1)
  3. 取 Oracle Schema信息
  4. python 图像变化检测_python hough变换检测直线的实现方法
  5. python定义数据框大小_python – 如何设置框架的最小和最大高度或宽度?
  6. HDOJ---1232 畅通工程[并查集]
  7. python爱好者社区 周琦_这么多年来,我算想明白了!
  8. 任意角度旋转图片(python)
  9. Adobe AI软件解决界面字体过小的方法
  10. SpringBoot中发送QQ邮件
  11. springboot-jpa-querydsl
  12. WIN7 旗舰版、专业版、家庭高级版32位64位官方原版下载地址
  13. npm ETIMEDOUT 问题
  14. oracle 12c创建归档,oracle 12c 数据归档 即Using In-Database Archiving feature
  15. android edittext 输入法表情,Android开发中EditText禁止输入Emoji表情符
  16. 使用Jmeter输出错误响应结果到日志
  17. Matlab: 多项式表示及其基本运算
  18. 计算机主机内部的除尘课件,怎么给电脑主机机箱内部除尘
  19. 什么是软件驻场开发,它的优势和不足有哪些?
  20. 跨境自由职业者如何把境外的PayPal收款,方便快捷的汇入大陆的银联卡?

热门文章

  1. 时频分析:短时傅立叶变换实现(4)
  2. 程序php!index.,php.index
  3. [云炬创业管理笔记]第6章制定创业行动测试5
  4. 完结篇 | 吴恩达《序列模型》精炼笔记(3)-- 序列模型和注意力机制
  5. 算法导论-排序算法-分治法
  6. 3DSlicer31:结构的实例分析IGSReader
  7. 新兴机器学习算法:从无监督降维到监督降维
  8. VTK修炼之道63:纹理映射体绘制_二维纹理映射
  9. net4.0 兼容2.0的方法
  10. C#中串口通信编程 收藏