最近要实现了一个附件预览的功能。在网上搜了好多。最终实现了该功能。现在总结一下,以便以后查看
1、要用到的组建:aspose.words.dll;aspose.cells.dll;aspose.sliders.dll;用来实现将office文档转换为pdf文档。效果很不错。具体代码如下:
public bool ConvertDocToPdf(string sourceFileName, string targetFileName)//将word转换成pdf
{
try
{
using (System.IO.Stream stream = new System.IO.FileStream(sourceFileName, FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite))
{
Aspose.Words.Document doc = new Aspose.Words.Document(sourceFileName);
doc.Save(targetFileName, Aspose.Words.SaveFormat.Pdf);
}
}
catch (Exception ex)
{
return false;
}
return System.IO.File.Exists(targetFileName);
}
//将excell转换成html文件
public bool ConvertExcelToHtml(string sourcePath, string targetPath)
{
try
{
using (System.IO.Stream stream = new System.IO.FileStream(sourcePath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite))
{
Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(stream);
workbook.Save(targetPath, Aspose.Cells.SaveFormat.Html);
}
}
catch (Exception ex)
{
return false;
}
return System.IO.File.Exists(targetPath);
}
2,将转换好的pdf文件转换成swf文件。用pdf2swf;如果是图片,则用png2swf,gif2swf,jpeg2swf;在网上下载swftools安装之后就有。
3,将安装之后的所需的exe文件放到项目中bin目录下;然后在代码中如下所写:
public bool ConvertToSwf(string pdfPath, string swfPath, int page, string type)
{
try
{
string exe = null;
StringBuilder sb = new StringBuilder();
if (type == ".pdf")
{
exe = HttpContext.Current.Server.MapPath("~/Bin/pdf2swf.exe");
sb.Append(" -o \"" + swfPath + "\"");//output
sb.Append(" -z");
sb.Append(" -s flashversion=9");//flash version
sb.Append(" -s disablelinks");//禁止PDF里面的链接
sb.Append(" -p " + "1" + "-" + page);//page range
sb.Append(" -T 9");
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
}
else if (type == ".png")
{
exe = HttpContext.Current.Server.MapPath("~/Bin/png2swf.exe");
sb.Append(" \"" + pdfPath + "\"");
sb.Append(" -o \"" + swfPath + "\"");
sb.Append(" -T 9");
sb.Append(" -j 100");
}
else if (type == ".jpg")
{
exe = HttpContext.Current.Server.MapPath("~/Bin/jpeg2swf.exe");
sb.Append(" \"" + pdfPath + "\"");
sb.Append(" -o \"" + swfPath + "\"");
sb.Append(" -T 9");
sb.Append(" -j 100");
}
else if (type == ".gif")
{
exe = HttpContext.Current.Server.MapPath("~/Bin/gif2swf.exe");
sb.Append(" \"" + pdfPath + "\"");
sb.Append(" -o \"" + swfPath + "\"");
sb.Append(" -T 9");
sb.Append(" -j 100");
}
if (!File.Exists(exe))
{
throw new ApplicationException("Can not find: " + exe);
}
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();
return true;
}
catch (Exception ex)
{
return false;
}
}
4.利用flexpaperview在页面中显示转换好的swf文件
先下载flexpaperview2.2文件。然后在,页面引用flexpaper.js和flexpaper_handlers.js
打开flexpaperview.js文件,修改用红色标识的地方
_jsDirectory = (config.jsDirectory != null ? config.jsDirectory : "../../Resources/");
_cssDirectory = (config.cssDirectory!=null?config.cssDirectory:"css/");
_localeDirectory = (config.localeDirectory!=null?config.localeDirectory:"locale/");
if(_SWFFile!=null && _SWFFile.indexOf("{" )==0 && _SWFFile.indexOf("[*," ) > 0 && _SWFFile.indexOf("]" ) > 0){_SWFFile = escape(_SWFFile);} // split file fix
window[instance] = flashembed(id, {
src : _jsDirectory+"Scripts/swf/FlexPaperViewer.swf",
version : [10, 0],
expressInstall : "js/expressinstall.swf",
wmode : _WMode
}
然后再js中写代码:
$("#flexpaper").FlexPaperViewer(
{ config: {
SwfFile: "../../Resources/" + arr[0], //这里需要修改,是需要预览的swf文件的路径
Scale: 0.6,
ZoomTransition: "easeOut",
ZoomTime: 0.5,
ZoomInterval: 0.1,
FitPageOnLoad: false,
FitWidthOnLoad: true,
PrintEnabled: false,
FullScreenAsMaxWindow: false,
ProgressiveLoading: true,
PrintToolsVisible: true,
ViewModeToolsVisible: true,
ZoomToolsVisible: true,
FullScreenVisible: true,
NavToolsVisible: true,
CursorToolsVisible: true,
SearchToolsVisible: true,
localeChain: "zh_CN"
}
走完以上步骤。就能实现基本文档的在线预览。如果出现不显示或显示不正常的问题。那就是这些组建的版本下载的不对。我下载了好多。最后下载了最新的才成功。