c执行cmd pdf2swf_PDF2SWF简单使用

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

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

8e253d88df5f6d45862f4aecfea04acc.png

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

下面看C#代码:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.pngView Code

48304ba5e6f9fe08f3fa1abda7d326ab.png

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;

}

}

48304ba5e6f9fe08f3fa1abda7d326ab.png

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

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

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

9bc09067f5c6526d5d24ba39fe8be25b.png

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

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

801c26d0be12ac4ae6677cfafc3064f9.png

最后在页面中呈现。

48304ba5e6f9fe08f3fa1abda7d326ab.png

1

2

3

4

5

10

11

12

13

14

15

16

17

18

19

20

21

28

29

30

31

32

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值