将pdf转为swf文件
前章中介绍了怎样将文档转成pdf档,本章继续介绍将pdf档转为swf文件。
实现在线阅读文件,用iframe直接能够加载pdf文件,可是在显示的时候,效果不是很好,而且客户端一定要有pdf阅读软件。网上常见的在线阅读实现方式是播放flash,即播放swf文件。
有两种方式进行转换:
01 flashpaper,这种方式能直接从office之类的文档转为swf文件。但是不支持64位系统,且服务器一定要安装这个软件。64位系统的话,可以用Print2Flash代替。示例代码:
public static void ConvertToSwf(string strDocPath,string strSwfPath) { try { Process process = new Process(); // ProcessStartInfo startInfo = new ProcessStartInfo(); string paperroot = @"C:\Program Files (x86)\Print2Flash3\p2fServer.exe"; string docFile = strDocPath; string swfFile = strSwfPath; startInfo.FileName = paperroot; startInfo.Arguments = docFile + " " + swfFile; startInfo.UseShellExecute = false; startInfo.RedirectStandardInput = false; startInfo.RedirectStandardOutput = false; startInfo.CreateNoWindow = true; process.StartInfo = startInfo; process.Start(); process.WaitForExit(); process.Close(); } catch (Exception ex) { throw ex; } }
由于这种方式要在服务器端安装软件,感觉比较麻烦,当时未选择。
02,pdf2swf.exe转换工具,只要下载,不需安装。相关代码
public static void ConvertToSwf(string pdfPath, string swfPath)
{
try
{
string exe = HttpContext.Current.Server.MapPath("~/Temp/pdf2swf.exe");//获取转换工具的安装路径
if (!File.Exists(exe))
{
throw new ApplicationException("Can not find: " + exe);
}
StringBuilder sb = new StringBuilder();
sb.Append(" -o \"" + swfPath + "\"");//output
sb.Append(" -z");
sb.Append(" -s flashversion=9");//flash version
sb.Append(" -s disablelinks");//禁止PDF里面的链接
sb.Append(" -j 100");//Set quality of embedded jpeg pictures to quality. 0 is worst (small), 100 is best (big). (default:85)
sb.Append(" \"" + pdfPath + "\"");//input
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = exe;
proc.StartInfo.Arguments = sb.ToString();
proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
proc.Start();
proc.WaitForExit();
proc.Close();
}
catch (Exception ex)
{
throw ex;
}
}
两种方式都可以实现,各有优劣,可以根据实际情况选择
浙公网安备 33010602011771号