第一步先将Office文件转换为PDF文件,第二步将PDF文件转换为SWF文件.
Office文件转为PDF文件使用微软的插件saveaspdf,(此插件应用于office2007下).
PDF文件转换为SWF文件使用SWFTOOLS下的pdf2swf.exe
举例Word文件转换为SWF文件,代码如下:
Code
using Microsoft.Office.Interop.Word;
private Application _wordApp = null;
private Object Nothing = System.Reflection.Missing.Value;
//实例化App
try
{
_wordApp = (Microsoft.Office.Interop.Word.Application)
Microsoft.VisualBasic.Interaction.GetObject(null, "Word.Application");
}
catch
{
_wordApp = new Microsoft.Office.Interop.Word.Application();
}
/// <summary>
/// 释放所有资源
/// </summary>
public void Close()
{
if (_wordApp != null)
{
if (_wordApp.Documents.Count != 0)
{
_wordApp.ActiveDocument.Close(ref Nothing, ref Nothing, ref Nothing);
}
_wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
_wordApp = null;
}
}
/// <summary>
/// 将DOC转为HTML
/// </summary>
/// <param name="docFilePath">DOC文件路径</param>
/// <param name="pdfFilePath">PDF文件路径</param>
public void DocConvertPDF(string docFilePath,string pdfFilePath)
{
this.ReadTemplate(docFilePath);
Type wordType = _wordApp.GetType();
Type docType = _wordApp.ActiveDocument.GetType();
object saveFileName = pdfFilePath;
Object objFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;
_wordApp.ActiveDocument.SaveAs(ref saveFileName, ref objFormat, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
}
/// <summary>
/// 将DOC转为SWF
/// </summary>
/// <param name="docFilePath">DOC文件路径</param>
/// <param name="swfFilePath">SWF文件路径</param>
public bool DocConvertSWF(string docFilePath, string swfFilePath)
{
string strfilePath = @"C:\temp\";
if (docFilePath.Contains(@"\"))
{
strfilePath = docFilePath.Substring(0,docFilePath.LastIndexOf(@"\")+1);
}
strfilePath += DateTime.Now.ToString("yyyyMMddHHmmssfff")+".pdf";
this.DocConvertPDF(docFilePath, strfilePath);
_wordApp.ActiveDocument.Close(ref Nothing, ref Nothing, ref Nothing);
System.Diagnostics.Process p = System.Diagnostics.Process.Start(@"D:\Program Files\SWFTools\pdf2swf.exe",strfilePath + " -s flashversion=9 " + swfFilePath + " -t");
bool flag = false;
try
{
p.Start();
flag = true;
}
catch
{
flag = false;
}
try
{
if (p.HasExited)
{
p.Kill();
}
}
catch
{
}
return flag;
} 转换过程需要注意的几点:
1.一定得是在安装了Office2007的机器上运行,并安装了saveaspdfandxps插件.
2.转换完成PDF后,记得关闭Word文档,否则文件会一直占用,无法打开转换为SWF.
3.转换SWF文件时要加上"-s flashversion=9",否则在Flex里使用swfloader加载swf后,无法将其转换为MovieClip.
4.pdf2swf.exe有很多参数,这里使用"-t"代表转换后的swf文件每一帧后都会加上stop()停止播放.