C# print2flash3文件转化
1.下载print2flash3 并且安装print2flash3
2.转换工具类
(1)需要导入using Print2Flash3; 这个程序集
using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using Print2Flash3; using System.IO; using System.Collections.Generic; namespace WebThreadTest { public class FileConvertSwf { //保存所有文件名 private static List<string> fileList; //可以打印的文件格式 private List<string> printFileFormat = new List<string>() { ".doc", ".docx", ".xlsx", ".xls", ".ppt" }; public FileConvertSwf() { fileList = new List<string>(); } /// <summary> /// 将文件转化为swf文件 /// 将swf文件保存到当前目录下 /// </summary> /// <param name="filePath">原文件路径</param> public void ConvertToSwf(string filename) { //获取文件后缀名 string fileExtension = Path.GetExtension(filename); //是否可以打印 if (fileExtension != null && printFileFormat.Contains(fileExtension.ToLower())) { int extensionIndex = filename.LastIndexOf(fileExtension); //获取文件前缀名 string fileBeforeExtension = filename.Remove(extensionIndex); //swf文件名 string targetFilename = fileBeforeExtension + ".swf"; FileInfo file = new FileInfo(targetFilename); //不存在才转化 if (!file.Exists) { //使用Print2Flash3服务 IServer server = new Server(); //设置默认打印机 server.SetP2FPrinterAsDefault(); //文件转化 server.ConvertFile(@filename, @targetFilename, null, null, null); } } } /// <summary> /// 获取某个目录下的所有文件及子目录下的所有文件 /// </summary> /// <param name="dirPath">目录路径</param> public void GetFilename(string dirPath) { //当前目录 DirectoryInfo dir = new DirectoryInfo(dirPath); //当前目录下的所有文件 FileInfo[] files = dir.GetFiles(); foreach (var f in files) { //保存到文件名集合中 fileList.Add(f.FullName); //文件转换 ConvertToSwf(f.FullName); } //获取当前目录下的子目录下的所有文件 DirectoryInfo[] dirs = dir.GetDirectories(); foreach (var d in dirs) { //递归获取文件名 GetFilename(d.FullName); } } public List<string> GetFileNameList() { return fileList; } } }
3.测试代码
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Collections.Generic; namespace WebThreadTest { public partial class testGetFile : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string dirPath = Server.MapPath(@"/resource/UploadedFiles"); FileConvertSwf fcSwf = new FileConvertSwf(); fcSwf.GetFilename(dirPath); List<string> fileList = fcSwf.GetFileNameList(); foreach (var f in fileList) { Response.Write(f); Response.Write("<br />"); } } } } }
4.html页面显示swf格式
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="convertFileTest.aspx.cs" Inherits="WebThreadTest.print2flash.demo.convertFileTest" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>无标题页</title> </head> <body> <form id="form1" runat="server"> <div> <object id="forfun" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="1000" height="600"> <param name="movie" value="/resource/ConvertedFiles/target.swf"> <param name="quality" value="high"> <param name="bgcolor" value="#F0F0F0"> <param name="menu" value="false"> <param name="wmode" value="opaque"> <param name="FlashVars" value=""> <param name="allowScriptAccess" value="sameDomain"> <embed id="forfunex" src="/resource/ConvertedFiles/target.swf" width="1000" height="600" align="middle" quality="high" bgcolor="#f0fff8" menu="false" play="true" loop="false" FlashVars="" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer"> </embed> </object> </div> </form> </body> </html>