c执行cmd pdf2swf_PDF2SWF简单使用
最近在項目中遇到文檔預覽的需求,和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简单使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: saslauthd mysql_启用Me
- 下一篇: 南核目录2020pdf_北核+南核|《消