C#的Process类调用第三方插件实现PDF文件转SWF文件

2019-12-30 14:33:17于海丽


[
    DefaultValue(false),
    MonitoringDescription(SR.ProcessCreateNoWindow),
    NotifyParentProperty(true) 
    ]
    public bool CreateNoWindow { 
      get { return createNoWindow; } 
      set { createNoWindow = value; }
    }

 以上简单介绍了该类的三种常用方法和两种常用属性,在实际的开发项目中无须对每个属性方法和属性的底层实现做全面的了解,但建议在学习该类的时候,适当的了解一下某一些类的方法实现,有助于我们很好的掌握该类。

二.如何实现PDF文件转化为SWF文件

在项目如果需要将PDF文件转换为SWF文件,可以在项目中引入Swftools插件,该插件的主要功能:PDF到SWF转换器。 每页生成一帧。 使您能够在Flash Movie中拥有完全格式化的文本,包括表格,公式,图形等。 它基于Derek B. Noonburg的xpdf PDF解析器。

简单介绍一下该插件的常用参数:

-h , –help                      Print short help message and exit              打印帮助信息   

-V , –version                Print version info and exit                        打印版本号

-o , –output file.swf         Direct output to file.swf. If file.swf contains ‘13568621′ (file13568630.swf), then each page指定输出的swf文件名

-P , –password password       Use password for deciphering the pdf.指定打开pdf的密码

-z , –zlib                    Use Flash 6 (MX) zlib compression.使用Flash 6的zlib压缩机制

-i , –ignore                  Allows pdf2swf to change the draw order of the pdf. This may make the generated允许程序修改pdf的绘制顺序,可能会导致结果与原来有差异

以上是几种常用的参数,具体擦参数列表详见:http://www.easck.com/。

对实现本次操作的类和插件做了一个简单的介绍,接下来提供一个具体实现该功能的操作方法:
 


 /// <summary>
    /// PDF格式转为SWF
    /// </summary>
    /// <param name="pdfPathParameter">原视频文件地址,如/a/b/c.pdf</param>
    /// <param name="swfPathParameter">生成后的FLV文件地址,如/a/b/c.swf</param>
    /// <param name="beginpage">转换开始页</param>
    /// <param name="endpage">转换结束页</param>
    /// <param name="photoQuality">照片质量</param>
    /// <returns></returns>
    public static bool PdfConversionSwf(string pdfPathParameter, string swfPathParameter, int beginpage, int endpage, int photoQuality)
    {
      if (string.IsNullOrEmpty(pdfPathParameter))
      {
        throw new ArgumentNullException(pdfPathParameter);
      }
      if (string.IsNullOrEmpty(swfPathParameter))
      {
        throw new ArgumentNullException(swfPathParameter);
      }
      if (endpage < beginpage)
      {
        throw new ArgumentException("起始页数大于结束页数");
      }
      if (photoQuality <= 0)
      {
        throw new ArgumentException("照片质量错误");
      }
      var exe = HttpContext.Current.Server.MapPath("~/tools/swftools-2013-04-09-1007.exe");
      var pdfPath = HttpContext.Current.Server.MapPath(pdfPathParameter);
      var swfPath = HttpContext.Current.Server.MapPath(swfPathParameter);
      Process p = null;
      try
      {
        if (!File.Exists(exe) || !File.Exists(pdfPath))
        {
          return false;
        }
        if (File.Exists(swfPath))
        {
          File.Delete(swfPath);
        }
        var sb = new StringBuilder();
        sb.Append(" "" + pdfPath + """);
        sb.Append(" -o "" + swfPath + """);
        sb.Append(" -s flashversion=9");
        sb.Append(" -s disablelinks");
        if (endpage > GetPageCount(pdfPath))
        {
          endpage = GetPageCount(pdfPath);
        }
        sb.Append(" -p " + """ + beginpage + "" + "-" + endpage + """);
        //SWF中的图片质量
        sb.Append(" -j " + photoQuality);
        var command = sb.ToString();
        //Process提供对本地和远程的访问进程,使能够启动和停止系统进程。
        p = new Process
        {
          StartInfo =
          {
            FileName = exe,
            Arguments = command,
            WorkingDirectory = HttpContext.Current.Server.MapPath("~/Bin/"),
            UseShellExecute = false,
            RedirectStandardError = true,
            CreateNoWindow = false
          }
        };
        //启动线程
        p.Start();
        //开始异步读取
        p.BeginErrorReadLine();
        //等待完成
        p.WaitForExit();
        //开始同步读取
        //p.StandardError.ReadToEnd();        
        if (!File.Exists(swfPath))
          return false;
        return true;
      }
      catch (IOException ioex)
      {
        throw new IOException(ioex.Message);
      }
      catch (Exception ex)
      {
        throw new Exception(ex.Message);
      }
      finally
      {
        if (p != null)
        {
          //关闭进程
          p.Close();
          //释放资源
          p.Dispose();
        }
      }

    }