您的位置:首页 > 编程语言 > ASP

word文档转换成swf格式文件在网页中用flash显示

2016-10-25 16:36 555 查看

word文档转换成swf格式文件在网页中用flash显示

在OA系统中我们常常需要将上传的word文档在网页中阅览,一般上传后的文档用html的形式查看是会导致排版混乱的,这次我介绍在ASP.NET中如何将上传的word文档转换为swf格式的文件显示在网页中。首先介绍下我的装换过程,我首先利用了office的com接口中的另存为,直接将word装换成pdf文档,然后我使用了swftool工具将pdf装换成swf格式文件,最后在网页中显示使用了Flexpaper。首先是装换为pdf的代码如下:

public static bool wordToPdf2(string wordPath,string pdfPath){
bool result = false;
Microsoft.Office.Interop.Word.Applicationapplication=Microsoft.Office.Interop.Word.Application();
Document document = null;
try {
application.Visible = false;
document = application.Documents.Open(wordPath);
document.ExportAsFixedFormat(pdfPath, WdExportFormat.wdExportFormatPDF);
result = true;
}
catch (Exception e)
{
Log4NetHelp.Error(e);
result = false;
}
finally
{
document.Close();
}
return result;
}


接着是pdf转换为swf格式的文件的代码

public static bool ChangeToSwf(string pdfPath, string swfPath)
{
#region
try
{
bool isStart = false;
string cmd = @"C:\Program Files (x86)\SWFTools\pdf2swf.exe";
ProcessStartInfo startInfo = new ProcessStartInfo(cmd);
startInfo.Arguments = string.Concat(pdfPath, " -o ", swfPath, " -f -T 9 -t -s storeallcharacters");
Process process = new Process();
process.StartInfo = startInfo;
process.StartInfo.WorkingDirectory = pdfPath;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
isStart = process.Start();
process.WaitForExit();
process.Close();
return isStart;
}
catch (Exception ex)
{
return false;
}
#endregion
}


最后是显示swf格式文件的代码

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>  
<title>显示swf格式文件</title>  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="FlexPaper/js/flexpaper.js"></script>
<script type="text/javascript" src="FlexPaper/js/flexpaper_handlers.js"></script>
</head>
<body> 
<div id="documentViewer" class="flexpaper_viewer"
style="position:absolute;top:10px;width:100%;height:100%"></div>
<script type="text/javascript">
function GetQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
var r = window.location.search.substr(1).match(reg);
if (r != null) return (r[2]); return null;
}
var fp = $('#documentViewer').FlexPaperViewer(
{
config: {
SwfFile:'/swf/'+GetQueryString('id') + '.swf',
Scale:0.8,
ZoomTransition: 'easeOut',
ZoomTime: 0.5,
ZoomInterval: 0.2,
FitPageOnLoad: true,
FitWidthOnLoad: true,
FullScreenAsMaxWindow: true,
ProgressiveLoading: true,
MinZoomSize: 0.2,
MaxZoomSize: 5,
SearchMatchAll: false,
InitViewMode: 'Portrait',
ViewModeToolsVisible: true,
ZoomToolsVisible: true,
NavToolsVisible: true,
CursorToolsVisible: true,
SearchToolsVisible: true,
localeChain: 'zh_CN',
jsDirectory:'/FlexPaper/js/',  /*设置FlexPaper的js文件目录,包含FlexPaperVier.swf文件,否则默认在flex目录下.*/
cssDirectory: '/FlexPaper/css/'
}
}
);
</script>
</body>
</html>


几个问题

1. 在使用另存为功能时需要测试的机器上安装2007以上的office

2. 在使用swftool工具时我是直接默认,程序中也是使用的绝对路径,使用这个工具时关于参数的填写像我这种形式最好,为了解决转换弹出黑框和报错找不到文件路径的错误进程配置信息按照我的代码就可以解决。

3. 在使用flexpaper显示时会出现右上角一直在加载的情况是转换时没有填写我加的参数。

4. 下载的Flexpaper中的FlexPaperVier.swf需要放在js文件目录,至于配置config的json串中各个参数的含义可以自己找自己了解,基本用我这个参数的填写就可以。

5. 在vs自带的应用服务器中测试时上面代码是没有问题的,当部署在iis上时就会报错,解决方法之一是将iis中的应用程序池中的标识改为localsystem,权限问题基本就解决了除非你的配置文件中加了类似
<identity impersonate="true"/>
的代码,然后以为问题都解决就错了,还是报错了,这次是说
document = application.Documents.Open(wordPath);
这个代码问题报document对象为空导致空引用异常,还是被我找到解决办法至于为什么是这样我也不知道,在64位系统中才有的一个文件夹中添加一个名为Desktop的文件夹,”C:\Windows\SysWOW64\config\systemprofile\”就是这个目录下。

6. 这是我能找到的可以方便的解决问题的办法的流程,可以直接将word转换为flash的工具print2Flash需要付费,至于说破解的好像不太好,flashprint又没有更新了只能在32位系统中使用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息