Office2007需要借助SaveAsPDFandXPS的插件完成,Office2010可以直接兼容。
Office2PDF主要采用的是 Microsoft.Office.Interop的方式进行,PDF2SWF主要采用的是SWFTools的pdf2swf工具。至于SWFTools的各种命令,网上有很多的资料可以参考,这里就不一一举例了。
1 /// <summary> 2 /// 把Word文件转换成为PDF格式文件 3 /// </summary> 4 /// <param name="sourcePath">源文件路径</param> 5 /// <param name="targetPath">目标文件路径</param> 6 /// <returns>true=转换成功</returns> 7 public static bool WordToPDF(string sourcePath, string targetPath) 8 { 9 bool result = false; 10 Word.WdExportFormat exportFormat = Word.WdExportFormat.wdExportFormatPDF; 11 object paramMissing = Type.Missing; 12 Word.Application wordApplication = new Word.Application(); 13 Word.Document wordDocument = null; 14 try 15 { 16 object paramSourceDocPath = sourcePath; 17 string paramExportFilePath = targetPath; 18 Word.WdExportFormat paramExportFormat = exportFormat; 19 bool paramOpenAfterExport = false; 20 Word.WdExportOptimizeFor paramExportOptimizeFor = Microsoft.Office.Interop.Word.WdExportOptimizeFor.wdExportOptimizeForPrint; 21 Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument; 22 int paramStartPage = 0; 23 int paramEndPage = 0; 24 Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent; 25 bool paramIncludeDocProps = true; 26 bool paramKeepIRM = true; 27 Word.WdExportCreateBookmarks paramCreateBookmarks = Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks; 28 bool paramDocStructureTags = true; 29 bool paramBitmapMissingFonts = true; 30 bool paramUseISO19005_1 = false; 31 wordDocument = wordApplication.Documents.Open( 32 ref paramSourceDocPath, ref paramMissing, ref paramMissing, 33 ref paramMissing, ref paramMissing, ref paramMissing, 34 ref paramMissing, ref paramMissing, ref paramMissing, 35 ref paramMissing, ref paramMissing, ref paramMissing, 36 ref paramMissing, ref paramMissing, ref paramMissing, 37 ref paramMissing); 38 if (wordDocument != null) 39 wordDocument.ExportAsFixedFormat(paramExportFilePath, 40 paramExportFormat, paramOpenAfterExport, 41 paramExportOptimizeFor, paramExportRange, paramStartPage, 42 paramEndPage, paramExportItem, paramIncludeDocProps, 43 paramKeepIRM, paramCreateBookmarks, paramDocStructureTags, 44 paramBitmapMissingFonts, paramUseISO19005_1, 45 ref paramMissing); 46 result = true; 47 } 48 catch 49 { 50 result = false; 51 } 52 finally 53 { 54 if (wordDocument != null) 55 { 56 wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing); 57 wordDocument = null; 58 } 59 if (wordApplication != null) 60 { 61 wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing); 62 wordApplication = null; 63 } 64 GC.Collect(); 65 GC.WaitForPendingFinalizers(); 66 GC.Collect(); 67 GC.WaitForPendingFinalizers(); 68 } 69 return result; 70 //bool result = false; 71 //Microsoft.Office.Interop.Word.WdExportFormat exportFormat = Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF; 72 //Microsoft.Office.Interop.Word.Application application = null; 73 74 //Microsoft.Office.Interop.Word.Document document = null; 75 //try 76 //{ 77 // application = new Microsoft.Office.Interop.Word.Application(); 78 // application.Visible = false; 79 // document = application.Documents.Open(sourcePath); 80 // document.SaveAs(); 81 // document.ExportAsFixedFormat(targetPath, exportFormat); 82 // result = true; 83 //} 84 //catch (Exception e) 85 //{ 86 87 // result = false; 88 // throw e; 89 //} 90 //finally 91 //{ 92 // if (document != null) 93 // { 94 // document.Close(); 95 // document = null; 96 // } 97 // if (application != null) 98 // { 99 // application.Quit(); 100 // application = null; 101 // } 102 // GC.Collect(); 103 // GC.WaitForPendingFinalizers(); 104 // GC.Collect(); 105 // GC.WaitForPendingFinalizers(); 106 //} 107 //return result; 108 }
1 /// <summary> 2 /// 把Microsoft.Office.Interop.Excel文件转换成PDF格式文件 3 /// </summary> 4 /// <param name="sourcePath">源文件路径</param> 5 /// <param name="targetPath">目标文件路径</param> 6 /// <returns>true=转换成功</returns> 7 public static bool ExcelToPDF(string sourcePath, string targetPath) 8 { 9 bool result = false; 10 Excel.XlFixedFormatType targetType = Excel.XlFixedFormatType.xlTypePDF; 11 object missing = Type.Missing; 12 Excel.Application application = null; 13 Excel.Workbook workBook = null; 14 try 15 { 16 application = new Excel.Application(); 17 object target = targetPath; 18 object type = targetType; 19 workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing, 20 missing, missing, missing, missing, missing, missing, missing, missing, missing); 21 workBook.ExportAsFixedFormat(targetType, target, Excel.XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing); 22 result = true; 23 } 24 catch 25 { 26 result = false; 27 } 28 finally 29 { 30 if (workBook != null) 31 { 32 workBook.Close(true, missing, missing); 33 workBook = null; 34 } 35 if (application != null) 36 { 37 application.Quit(); 38 application = null; 39 } 40 GC.Collect(); 41 GC.WaitForPendingFinalizers(); 42 GC.Collect(); 43 GC.WaitForPendingFinalizers(); 44 } 45 return result; 46 //bool result = false; 47 //Microsoft.Office.Interop.Excel.XlFixedFormatType targetType = Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF; 48 //object missing = Type.Missing; 49 //Microsoft.Office.Interop.Excel.Application application = null; 50 //Microsoft.Office.Interop.Excel.Workbook workBook = null; 51 //try 52 //{ 53 // application = new Microsoft.Office.Interop.Excel.Application(); 54 // application.Visible = false; 55 // workBook = application.Workbooks.Open(sourcePath); 56 // workBook.SaveAs(); 57 // workBook.ExportAsFixedFormat(targetType, targetPath); 58 // result = true; 59 //} 60 //catch (Exception e) 61 //{ 62 63 // result = false; 64 // throw e; 65 //} 66 //finally 67 //{ 68 // if (workBook != null) 69 // { 70 // workBook.Close(true, missing, missing); 71 // workBook = null; 72 // } 73 // if (application != null) 74 // { 75 // application.Quit(); 76 // application = null; 77 // } 78 // GC.Collect(); 79 // GC.WaitForPendingFinalizers(); 80 // GC.Collect(); 81 // GC.WaitForPendingFinalizers(); 82 //} 83 //return result; 84 }
1 /// <summary> 2 /// 把PowerPoint文件转换成PDF格式文件 3 /// </summary> 4 /// <param name="sourcePath">源文件路径</param> 5 /// <param name="targetPath">目标文件路径</param> 6 /// <returns>true=转换成功</returns> 7 public static bool PowerPointToPDF(string sourcePath, string targetPath) 8 { 9 bool result; 10 PowerPoint.PpSaveAsFileType targetFileType = PowerPoint.PpSaveAsFileType.ppSaveAsPDF; 11 12 object missing = Type.Missing; 13 PowerPoint.Application application = null; 14 PowerPoint.Presentation persentation = null; 15 try 16 { 17 application = new PowerPoint.Application(); 18 persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse); 19 persentation.SaveAs(targetPath, targetFileType, Microsoft.Office.Core.MsoTriState.msoTrue); 20 result = true; 21 } 22 catch 23 { 24 result = false; 25 } 26 finally 27 { 28 if (persentation != null) 29 { 30 persentation.Close(); 31 persentation = null; 32 } 33 if (application != null) 34 { 35 application.Quit(); 36 application = null; 37 } 38 GC.Collect(); 39 GC.WaitForPendingFinalizers(); 40 GC.Collect(); 41 GC.WaitForPendingFinalizers(); 42 } 43 return result; 44 45 //bool result; 46 //Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType targetFileType = Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsPDF; 47 //object missing = Type.Missing; 48 //Microsoft.Office.Interop.PowerPoint.Application application = null; 49 //Microsoft.Office.Interop.PowerPoint.Presentation persentation = null; 50 //try 51 //{ 52 // application = new Microsoft.Office.Interop.PowerPoint.Application(); 53 // //application.Visible = MsoTriState.msoFalse; 54 // persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse); 55 // persentation.SaveAs(targetPath, targetFileType, Microsoft.Office.Core.MsoTriState.msoTrue); 56 57 // result = true; 58 //} 59 //catch (Exception e) 60 //{ 61 // result = false; 62 // throw e; 63 //} 64 //finally 65 //{ 66 // if (persentation != null) 67 // { 68 // persentation.Close(); 69 // persentation = null; 70 // } 71 // if (application != null) 72 // { 73 // application.Quit(); 74 // application = null; 75 // } 76 // GC.Collect(); 77 // GC.WaitForPendingFinalizers(); 78 // GC.Collect(); 79 // GC.WaitForPendingFinalizers(); 80 //} 81 //return result; 82 }
1 /// <summary> 2 /// 把Visio文件转换成PDF格式文件 3 /// </summary> 4 /// <param name="sourcePath">源文件路径</param> 5 /// <param name="targetPath">目标文件路径</param> 6 /// <returns>true=转换成功</returns> 7 public static bool VisioToPDF(string sourcePath, string targetPath) 8 { 9 bool result; 10 11 Microsoft.Office.Interop.Visio.VisFixedFormatTypes targetType = Microsoft.Office.Interop.Visio.VisFixedFormatTypes.visFixedFormatPDF; 12 object missing = Type.Missing; 13 Microsoft.Office.Interop.Visio.Application application = null; 14 Microsoft.Office.Interop.Visio.Document document = null; 15 try 16 { 17 application = new Microsoft.Office.Interop.Visio.Application(); 18 application.Visible = false; 19 document = application.Documents.Open(sourcePath); 20 document.Save(); 21 document.ExportAsFixedFormat(targetType, targetPath, Microsoft.Office.Interop.Visio.VisDocExIntent.visDocExIntentScreen, Microsoft.Office.Interop.Visio.VisPrintOutRange.visPrintAll); 22 result = true; 23 } 24 catch (Exception e) 25 { 26 result = false; 27 28 throw e; 29 } 30 finally 31 { 32 if (application != null) 33 { 34 application.Quit(); 35 application = null; 36 } 37 GC.Collect(); 38 GC.WaitForPendingFinalizers(); 39 GC.Collect(); 40 GC.WaitForPendingFinalizers(); 41 } 42 return result; 43 }
因为这里的代码只支持Office2007以及以上版本,所以需要检测目标机器的Office版本,这里我的方法是查询注册表信息,对于绿色安装的Office楼主没有想到好的办法。
1 /// <summary> 2 /// 获取当前某个版本Office的安装路径 3 /// </summary> 4 /// <param name="Path">返回当前系统Office安装路径</param> 5 /// <param name="Version">返回当前系统Office版本信息</param> 6 public static void GetOfficePath(out string Path, out string Version) 7 { 8 string strPathResult = ""; 9 string strVersionResult = ""; 10 string strKeyName = "Path"; 11 object objResult = null; 12 Microsoft.Win32.RegistryValueKind regValueKind; 13 Microsoft.Win32.RegistryKey regKey = null; 14 Microsoft.Win32.RegistryKey regSubKey = null; 15 16 try 17 { 18 regKey = Microsoft.Win32.Registry.LocalMachine; 19 if (regSubKey == null) 20 {//office2010 21 regSubKey = regKey.OpenSubKey(@"SOFTWARE\Microsoft\Office\14.0\Common\InstallRoot", false); 22 strVersionResult = "office2010"; 23 strKeyName = "Path"; 24 try 25 { 26 27 objResult = regSubKey.GetValue(strKeyName); 28 29 regValueKind = regSubKey.GetValueKind(strKeyName); 30 31 } 32 33 catch (Exception ex) 34 { 35 36 regSubKey = null; 37 38 } 39 40 } 41 if (regSubKey == null) 42 {//office2007 43 regSubKey = regKey.OpenSubKey(@"SOFTWARE\Microsoft\Office\12.0\Common\InstallRoot", false); 44 strVersionResult = "office2007"; 45 strKeyName = "Path"; 46 try 47 { 48 49 objResult = regSubKey.GetValue(strKeyName); 50 51 regValueKind = regSubKey.GetValueKind(strKeyName); 52 53 } 54 55 catch (Exception ex) 56 { 57 58 regSubKey = null; 59 60 } 61 } 62 63 if (regSubKey == null) 64 {//Office2003 65 regSubKey = regKey.OpenSubKey(@"SOFTWARE\Microsoft\Office\11.0\Common\InstallRoot", false); 66 strVersionResult = "office2003"; 67 strKeyName = "Path"; 68 try 69 { 70 71 objResult = regSubKey.GetValue(strKeyName); 72 73 regValueKind = regSubKey.GetValueKind(strKeyName); 74 75 } 76 77 catch (Exception ex) 78 { 79 80 regSubKey = null; 81 82 } 83 } 84 if (regSubKey == null) 85 {//officeXp 86 regSubKey = regKey.OpenSubKey(@"SOFTWARE\Microsoft\Office\10.0\Common\InstallRoot", false); 87 strVersionResult = "officeXP"; 88 strKeyName = "Path"; 89 try 90 { 91 92 objResult = regSubKey.GetValue(strKeyName); 93 94 regValueKind = regSubKey.GetValueKind(strKeyName); 95 96 } 97 98 catch (Exception ex) 99 { 100 101 regSubKey = null; 102 103 } 104 } 105 if (regSubKey == null) 106 {//Office2000 107 regSubKey = regKey.OpenSubKey(@"SOFTWARE\Microsoft\Office\9.0\Common\InstallRoot", false); 108 strVersionResult = "office2000"; 109 strKeyName = "Path"; 110 try 111 { 112 113 objResult = regSubKey.GetValue(strKeyName); 114 115 regValueKind = regSubKey.GetValueKind(strKeyName); 116 117 } 118 119 catch (Exception ex) 120 { 121 122 regSubKey = null; 123 124 } 125 } 126 if (regSubKey == null) 127 {//office97 128 regSubKey = regKey.OpenSubKey(@"SOFTWARE\Microsoft\Office\8.0\Common\InstallRoot", false); 129 strVersionResult = "office97"; 130 strKeyName = "OfficeBin"; 131 try 132 { 133 134 objResult = regSubKey.GetValue(strKeyName); 135 136 regValueKind = regSubKey.GetValueKind(strKeyName); 137 138 } 139 140 catch (Exception ex) 141 { 142 143 regSubKey = null; 144 145 } 146 } 147 148 objResult = regSubKey.GetValue(strKeyName); 149 regValueKind = regSubKey.GetValueKind(strKeyName); 150 if (regValueKind == Microsoft.Win32.RegistryValueKind.String) 151 { 152 strPathResult = objResult.ToString(); 153 } 154 } 155 catch (System.Security.SecurityException ex) 156 { 157 throw new System.Security.SecurityException("您没有读取注册表的权限", ex); 158 } 159 catch (Exception ex) 160 { 161 throw new Exception("读取注册表出错!", ex); 162 } 163 finally 164 { 165 166 if (regKey != null) 167 { 168 regKey.Close(); 169 regKey = null; 170 } 171 172 if (regSubKey != null) 173 { 174 regSubKey.Close(); 175 regSubKey = null; 176 } 177 } 178 179 Path = strPathResult; 180 Version = strVersionResult; 181 }
当文档转成PDF后,就成功一半啦,接下来就只剩下转换成SWF了。
转换的命令其实很简单的,但是楼主在实际操作的时候出现了问题,在转换的时候我们一般使用的是Process在进行命令行的执行,但是此类提供的标准output流只有2k,对于页数很多的PDF转换SWF的时候,会出现SWF无法生成的问题,借助网上很多同仁的资料,改良后的方面对于处理五六十页以内的PDF转SWF的问题不大,具体代码如下:
1 //pdf转swf 2 string pdf2swfExe = @"SWFTools\pdf2swf.exe"; 3 4 string args = " -t \"" + szPDF + "\" -o \"" + szSWF 5 + "\" -s drawonlyshapes -s flashversion=9 -s poly2bitmap"; 6 using (Process p = new Process()) 7 { 8 9 string cmd = pdf2swfExe; 10 11 12 p.StartInfo.FileName = cmd; 13 p.StartInfo.Arguments = args; 14 p.StartInfo.UseShellExecute = false; 15 16 ////此类提供的标准output流只有2k,不要重定向 17 p.StartInfo.RedirectStandardOutput = true; 18 19 p.StartInfo.CreateNoWindow = true; 20 p.Start(); 21 22 p.PriorityClass = ProcessPriorityClass.High; 23 p.BeginOutputReadLine(); 24 p.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived); 25 26 }
主要采用的方式是自行读取OUTPUT中的数据流,
1 private void process_OutputDataReceived(object sender, DataReceivedEventArgs e) 2 { 3 4 // 这里仅做输出的示例,实际上您可以根据情况取消获取命令行的内容 5 6 // 参考:process.CancelOutputRead() 7 8 9 (sender as Process).Close(); 10 11 if (String.IsNullOrEmpty(e.Data) == false) 12 { 13 14 string szPDF = Fileinfo.FileNewName.Split('.')[0] + ".pdf"; 15 string szSWF = Fileinfo.FileNewName.Split('.')[0] + ".swf"; 16 if (File.Exists(szSWF)) 17 { 18 try 19 { 20 21 22 23 ThreadDelegate changeProcessDel = delegate() //后台中要更改主线程中的UI,于是我们还是用委托来实现,再创建一个实例 24 { 25 DataGridTemplateColumn templeColumn = Datagrid.Columns[Datagrid.Columns.Count - 1] as DataGridTemplateColumn; 26 FrameworkElement element = templeColumn.GetCellContent(Datagrid.Items[Index]); 27 GifImage img = (GifImage)templeColumn.CellTemplate.FindName("gifimage", element); 28 img.Visibility = System.Windows.Visibility.Hidden; 29 Label Successed = (Label)templeColumn.CellTemplate.FindName("Successed", element); 30 Successed.Visibility = System.Windows.Visibility.Visible; 31 Successed.Content = "文件转换成功!"; 32 };//要调用的过程 33 Dispatcher.BeginInvoke(DispatcherPriority.Send, changeProcessDel); //使用分发器让这个委托等待执行 34 Thread.Sleep(3000); 35 36 37 38 //删除原文件 39 if (File.Exists(Fileinfo.FileNewName)) 40 { 41 File.Delete(Fileinfo.FileNewName); 42 } 43 //删除pdf 44 //if (File.Exists(szPDF)) 45 //{ 46 // File.Delete(szPDF); 47 //} 48 } 49 catch (Exception ex) 50 { 51 throw ex; 52 } 53 } 54 else 55 { 56 //ThreadDelegate changeProcessDel = delegate() //后台中要更改主线程中的UI,于是我们还是用委托来实现,再创建一个实例 57 //{ 58 // DataGridTemplateColumn templeColumn = Datagrid.Columns[Datagrid.Columns.Count - 1] as DataGridTemplateColumn; 59 // FrameworkElement element = templeColumn.GetCellContent(Datagrid.Items[Index]); 60 // GifImage img = (GifImage)templeColumn.CellTemplate.FindName("gifimage", element); 61 // img.Visibility = System.Windows.Visibility.Hidden; 62 // Label Successed = (Label)templeColumn.CellTemplate.FindName("Successed", element); 63 // Successed.Visibility = System.Windows.Visibility.Visible; 64 // Successed.Content = "文件转换出错!"; 65 //};//要调用的过程 66 //Dispatcher.BeginInvoke(DispatcherPriority.Send, changeProcessDel); //使用分发器让这个委托等待执行 67 //Thread.Sleep(3000); 68 69 } 70 } 71 72 }
本程序的框架是WPF搭建的。
关于PDF转SWF就说这么多了,当然楼主所有的代码中不乏是网上同仁的心血,楼主在这里只是对某些代码进行整改和总结,并非侵权。