PSD2swfHelper.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System.Web;
  2. using System.Text;
  3. namespace Ant.Service.Utilities
  4. {
  5. public static class PSD2swfHelper
  6. {
  7. /// <summary>
  8. /// 转换所有的页,图片质量80%
  9. /// </summary>
  10. /// <param name="pdfPath">PDF文件地址</param>
  11. /// <param name="swfPath">生成后的SWF文件地址</param>
  12. public static bool PDF2SWF(string pdfPath, string swfPath)
  13. {
  14. return PDF2SWF(pdfPath, swfPath, 1, GetPageCount(HttpContext.Current.Server.MapPath(pdfPath)), 80);
  15. }
  16. /// <summary>
  17. /// 转换前N页,图片质量80%
  18. /// </summary>
  19. /// <param name="pdfPath">PDF文件地址</param>
  20. /// <param name="swfPath">生成后的SWF文件地址</param>
  21. /// <param name="page">页数</param>
  22. public static bool PDF2SWF(string pdfPath, string swfPath, int page)
  23. {
  24. return PDF2SWF(pdfPath, swfPath, 1, page, 80);
  25. }
  26. /// <summary>
  27. /// PDF格式转为SWF
  28. /// </summary>
  29. /// <param name="pdfPath">PDF文件地址</param>
  30. /// <param name="swfPath">生成后的SWF文件地址</param>
  31. /// <param name="beginpage">转换开始页</param>
  32. /// <param name="endpage">转换结束页</param>
  33. private static bool PDF2SWF(string pdfPath, string swfPath, int beginpage, int endpage, int photoQuality)
  34. {
  35. string exe = HttpContext.Current.Server.MapPath("~/DLL/tools/pdf2swf-0.9.1.exe");
  36. pdfPath = HttpContext.Current.Server.MapPath(pdfPath);
  37. swfPath = HttpContext.Current.Server.MapPath(swfPath);
  38. if (!System.IO.File.Exists(exe) || !System.IO.File.Exists(pdfPath) || System.IO.File.Exists(swfPath))
  39. {
  40. return false;
  41. }
  42. StringBuilder sb = new StringBuilder();
  43. sb.Append(" \"" + pdfPath + "\"");
  44. sb.Append(" -o \"" + swfPath + "\"");
  45. sb.Append(" -s flashversion=9");
  46. if (endpage > GetPageCount(pdfPath)) endpage = GetPageCount(pdfPath);
  47. sb.Append(" -p " + "\"" + beginpage + "" + "-" + endpage + "\"");
  48. sb.Append(" -j " + photoQuality);
  49. string Command = sb.ToString();
  50. System.Diagnostics.Process p = new System.Diagnostics.Process();
  51. p.StartInfo.FileName = exe;
  52. p.StartInfo.Arguments = Command;
  53. p.StartInfo.WorkingDirectory = HttpContext.Current.Server.MapPath("~/DLL/");
  54. p.StartInfo.UseShellExecute = false;
  55. p.StartInfo.RedirectStandardError = true;
  56. p.StartInfo.CreateNoWindow = false;
  57. p.Start();
  58. p.BeginErrorReadLine();
  59. p.WaitForExit();
  60. p.Close();
  61. p.Dispose();
  62. return true;
  63. }
  64. /// <summary>
  65. /// 返回页数
  66. /// </summary>
  67. /// <param name="pdfPath">PDF文件地址</param>
  68. private static int GetPageCount(string pdfPath)
  69. {
  70. byte[] buffer = System.IO.File.ReadAllBytes(pdfPath);
  71. int length = buffer.Length;
  72. if (buffer == null)
  73. return -1;
  74. if (buffer.Length <= 0)
  75. return -1;
  76. string pdfText = Encoding.Default.GetString(buffer);
  77. System.Text.RegularExpressions.Regex rx1 = new System.Text.RegularExpressions.Regex(@"/Type\s*/Page[^s]");
  78. System.Text.RegularExpressions.MatchCollection matches = rx1.Matches(pdfText);
  79. return matches.Count;
  80. }
  81. }
  82. }