PDF2SWF简单使用

最近在项目中遇到文档预览的需求,和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 = 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;
}

///<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('=')+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);
}
}

///<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}

  

   最后在页面中呈现。

复制代码
 1 <html>
2
3 <body style="padding: 0px; margin: 0px">
4
5 <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
6 width="100%"
7 height="100%"
8
9 codebase="http://active.macromedia.com/flash5/cabs/swflash.cab#version=8,0,0,0">
10
11 <param name="MOVIE" value="preview.swf">
12
13 <param name="PLAY" value="true">
14
15 <param name="LOOP" value="true">
16
17 <param name="QUALITY" value="high">
18
19 <param name="FLASHVARS" value="zoomtype=3">
20
21 <embed src="preview.swf" width="100%"
22 height="100%"
23 play="true" ALIGN="" loop="true" quality="high"
24 type="application/x-shockwave-flash"
25
26 flashvars="zoomtype=3"
27 pluginspage="http://www.macromedia.com/go/getflashplayer">
28
29 </embed>
30 </object>
31 </body>
32 </html>
复制代码

最终页面呈现效果:

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

  xpdf-3.02pl5\\xpdf-chinese-simplified

  

  
posted @ 2014-03-26 10:09  TwinStudio  阅读(979)  评论(0编辑  收藏  举报