使用FlexPaper实现office文件的预览(C#版)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
使⽤FlexPaper实现office⽂件的预览(C#版)
需求很简单,⽤户上传office⽂件(word、excel、ppt)后,可以预览上传的这些⽂件。
搜索的相关的资料后。
整理如下:
Step1.⽤户上传office⽂件。
Step2.把Office⽂件转化为pdf⽂件
Step3.把pdf⽂件转化为swf⽂件
Step4.使⽤flexpaper插件预览swf⽂件
根据这四步,我们逐步分析:
Step1.上传⽂件,在此不做赘述。
Step2.把Office⽂件转化为pdf⽂件。
必须保证你的office版本在2007之上。
我第⼀次使⽤office2003,不报错,但是也没⽤⽣成相关的pdf⽂件。
果断使⽤了最新的office2013即可完美运⾏。
在次贴出将word转化为pdf⽂件的代码,完整实例,请看附件。
///<summary>
///把Word⽂件转换成为PDF格式⽂件
///</summary>
///<param name="sourcePath">源⽂件路径</param>
///<param name="targetPath">⽬标⽂件路径</param>
///<returns>true=转换成功</returns>
public static bool WordToPDF(string sourcePath, string targetPath)
{
bool result = false;
Microsoft.Office.Interop.Word.WdExportFormat exportFormat = Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF;
Microsoft.Office.Interop.Word.ApplicationClass application = null;
Microsoft.Office.Interop.Word.Document document = null;
try
{
application = new Microsoft.Office.Interop.Word.ApplicationClass();
application.Visible = false;
document = application.Documents.Open(sourcePath);
document.SaveAs();
document.ExportAsFixedFormat(targetPath, exportFormat);
result = true;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
result = false;
}
finally
{
if (document != null)
{
document.Close();
document = null;
}
if (application != null)
{
application.Quit();
application = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
return result;
}
View Code
(考虑到有些和我⼀样的新⼿,在此赘述⼏句吧:如果你使⽤vs2010开发,在添加office引⽤的时候,直接选择.net栏⽬下的office、
Microsoft.Office.Interop.Word,Microsoft.Office.Interop.PowerPoint和Microsoft.Office.Interop.Excel,并且保证office的版本⾼于后三者就⾏啦。
不⽤再在com栏⽬下引⼊相关office的com组件)
Step3.把pdf⽂件转化为swf⽂件。
⾸先,我们可以下载,完成安装后会在安装⽬录下⾯有多个⼩⼯具。
选择pdf2swf.exe 拷贝到我们的项⽬相关⽬录下⾯,使⽤如下代码,便可完成调⽤。
///<summary>
///把PDF⽂件转化为SWF⽂件
///</summary>
///<param name="toolPah">pdf2swf⼯具路径</param>
///<param name="sourcePath">源⽂件路径</param>
///<param name="targetPath">⽬标⽂件路径</param>
///<returns>true=转化成功</returns>
public static bool PDFToSWF(string toolPah, string sourcePath, string targetPath)
{
Process pc = new Process();
bool returnValue = true;
string cmd = toolPah;
string args = " -t " + sourcePath + " -s flashversion=9 -o " + targetPath;
try
{
ProcessStartInfo psi = new ProcessStartInfo(cmd, args);
psi.WindowStyle = ProcessWindowStyle.Hidden;
pc.StartInfo = psi;
pc.Start();
pc.WaitForExit();
}
catch (Exception ex)
{
returnValue = false;
throw new Exception(ex.Message);
}
finally
{
pc.Close();
pc.Dispose();
}
return returnValue;
}
View Code
此处也有两处要重点提醒。
⾸先呢,就是所有的⽂件路径都不能有空格,这是因为pdf2swf对含有空格的⽂件路径不⽀持。
其次,就是flashversion=9这个关键的命名也要加上,否则会出现在flexpaper⽆法预览该swf⽂档的情况。
Step4.使⽤flexpaper插件预览swf⽂件
刚开始我们可以下载⼀个官⽅的,选择-》download web server package。
然后直接打开index.html发现不能预览docs中的Paper.pdf.swf 这个swf⽂件。
于是乎⽹上出现了n多关于通过通过将adobe flash player将flexpaper添加信任啥的,这个哥试了,但是发现不⾏!有种被坑的感觉!最后我的解决⽅案是:直接将这个官⽹的例⼦放在IIS服务下⾯就⾏了。
或者你通过vs2010建⽴项⽬的时候把这个插件添加进去(就是把官⽹的那些代码粘过去,我这样说你明⽩吧,但是注意⼀下路径问题)也是可以的。
有些童鞋该说了,我想把swf⽂件动态加载,也就是FlexPaperViewer中的SwfFile动态加载。
so easy。
我们在CS端通过request.QueryString["swf"]获取123.swf⽂件之后可以将其值赋给mypage.aspx页⾯的HiddenField1,然后在
mypage.aspx中使⽤
SwfFile: escape($("#HiddenField1").val()),
即可动态获取swf⽂件。
最后贴出最终的显⽰结果图:
图1.初始页⾯
图2.选择office⽂件
图3.上传后预览
================================正常⼈类分割线=================================【客户需求】做完了之后,我闲着蛋疼想着如果⽤户要上传图⽚格式的⽂件,我们要预览图⽚的话呢?
很简单,⽤jQuery图⽚的相关插件就能美好的实现了。
当然⽤swftools也能将图⽚转化为swf,和这个页⾯集成到⼀起去。
swftools中⽀持jpg、jpeg、gif和png,不⽀持bmp⽂件格式。
为了能让转化成的swf能在flexpaper中能够正确的显⽰,这⼏种图⽚的swftool命令也不⼀样,直接贴代码了哈///<summary>
/// png、jpg和jpeg⽂件的转化
///</summary>
///<param name="toolPah"></param>
///<param name="sourcePath"></param>
///<param name="targetPath"></param>
///<returns></returns>
public static bool PicturesToSwf(string toolPah, string sourcePath, string targetPath)
{
Process pc = new Process();
bool returnValue = true;
string cmd = toolPah;
string args = "" + sourcePath + " -o " + targetPath+" -T 9";
//如果是多个图⽚转化为swf 格式为 ..jpeg2swf.exe C:\1.jpg C:\2.jpg -o C:\swf1.swf
try
{
ProcessStartInfo psi = new ProcessStartInfo(cmd, args);
psi.WindowStyle = ProcessWindowStyle.Hidden;
pc.StartInfo = psi;
pc.Start();
pc.WaitForExit();
}
catch (Exception ex)
{
returnValue = false;
throw new Exception(ex.Message);
}
finally
{
pc.Close();
pc.Dispose();
}
return returnValue;
}
///<summary>
/// Gif⽂件转化为swf
///</summary>
///<param name="toolPah"></param>
///<param name="sourcePath"></param>
///<param name="targetPath"></param>
///<returns></returns>
public static bool GifPicturesToSwf(string toolPah, string sourcePath, string targetPath)
{
Process pc = new Process();
bool returnValue = true;
string cmd = toolPah;
string args = "" + sourcePath + " -o " + targetPath;
try
{
ProcessStartInfo psi = new ProcessStartInfo(cmd, args);
psi.WindowStyle = ProcessWindowStyle.Hidden;
pc.StartInfo = psi;
pc.Start();
pc.WaitForExit();
}
catch (Exception ex)
{
returnValue = false;
throw new Exception(ex.Message);
}
finally
{
pc.Close();
pc.Dispose();
}
return returnValue;
}
View Code
这⼏个命令虽然⽹上有,但是很多都是有问题的,有的在flexpaper当中不能⽤。
本⼈的这个是亲测可⽤的,欢迎拍砖!
PostScript:
最后附上本⼈的项⽬附件,第⼀次这么仔细的写博客,⼤家多多关照。