c执行cmd pdf2swf_PDF2SWF简单使用
最近在項(xiàng)目中遇到文檔預(yù)覽的需求,和PM商討了幾種解決方案,最終還是選中了轉(zhuǎn)為SWF的方式。下面就稍微記錄一下自己的學(xué)習(xí)成果。
安裝完成后,在安裝目錄下可以看到N個(gè)單獨(dú)可以運(yùn)行的exe文件:
提供了多種格式轉(zhuǎn)swf的功能,不過(guò)這里我只用了pdf2swf這一個(gè),在我的項(xiàng)目里有一個(gè)service會(huì)將上傳的文件直接轉(zhuǎn)成pdf保存一個(gè)副檔,需要預(yù)覽的時(shí)候,直接獲取這個(gè)pdf的副檔就OK。
下面看C#代碼:
View Code
public class PDF2Swf
{
#region
//根目錄 private static string ROOT_PATH = AppDomain.CurrentDomain.BaseDirectory;
//pdf轉(zhuǎn)swf private static string PDF2SWF_PATH = "Shells\\SWFTools\\pdf2swf.exe";
//合并swf private static string SWFCOMBINE_PATH = "Shells\\SWFTools\\swfcombine.exe";
//導(dǎo)航 private static string SWFVIEWER_PATH="Shells\\SWF\\rfxview.swf";
private static string SWFTEMP_PATH = "Shells\\SWF\\temp.swf";
//保存轉(zhuǎn)成功的swf文件 public static string SAVE_SWF_PATH = "Shells\\SWF\\preview.swf";
//保存FLM上的PDF文檔 public static string SAVE_PDF_PATH = "Shells\\PDF\\preview.pdf";
//語(yǔ)言包路徑 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的文件路徑,以及輸出文件的位置,執(zhí)行pdf2swf的命令/ public static bool DoPDF2Swf(string strPDFPath, string strSwfPath)
{
bool isSuccess = false;
//如果PDF不存在 if (!File.Exists(strPDFPath))
{
return false;
}
#region 清理之前的記錄
if (File.Exists(strSwfPath))
{
//已經(jīng)存在,刪除 File.Delete(strSwfPath);
}
if (File.Exists(GetPath(SWFTEMP_PATH)))
{
File.Delete(GetPath(SWFTEMP_PATH));
}
#endregion
//將pdf文檔轉(zhuǎn)成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);
//第一步轉(zhuǎn)檔失敗,則返回 if (!File.Exists(GetPath(SWFTEMP_PATH)))
{
return false;
}
//將temp.swf加入到rfxview.swf加入翻頁(yè)的導(dǎo)航 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);
}
//運(yùn)行命令//命令字符串///命令運(yùn)行時(shí)間 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;
}
//根據(jù)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);
//如果已經(jīng)有存在則先刪掉 if (File.Exists(strPhysicalPDFPath))
{
File.Delete(strPhysicalPDFPath);
}
//建一個(gè)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);
}
}
//保證當(dāng)前的文件名唯一// private static string GetPDFName()
{
return DateTime.Now.ToLongTimeString().Replace(':','_')+DateTime.Now.Millisecond;
}
}
使用的時(shí)候調(diào)用DoPDF2Swf(string strPDFPath, string strSwfPath)傳入pdf以及輸出的swf路徑,
任務(wù)會(huì)先調(diào)用pdf2swf.exe將pdf文檔轉(zhuǎn)成temp.swf文件:
pdf2swf [-options] file.pdf -o file.swf
然后再調(diào)用swfcombine.exe合并tmep.swf以及rfxview.swf文件,輸出到preview.swf文件:
swfcombine.exe?rfxview.swf viewport={tmep.swf} -o {preview.swf}
最后在頁(yè)面中呈現(xiàn)。
1
2
3
4
5
10
11
12
13
14
15
16
17
18
19
20
21
28
29
30
31
32
總結(jié)
以上是生活随笔為你收集整理的c执行cmd pdf2swf_PDF2SWF简单使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: saslauthd mysql_启用Me
- 下一篇: mariadb mysql同步_Cent