首页 > 代码库 > Office文件转换PDF格式文件

Office文件转换PDF格式文件

技术分享
 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             Microsoft.Office.Interop.Word.WdExportFormat exportFormat = Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF;11             Microsoft.Office.Interop.Word.ApplicationClass application = null;12 13             Microsoft.Office.Interop.Word.Document document = null;14             try15             {16                 application = new Microsoft.Office.Interop.Word.ApplicationClass();17                 application.Visible = false;18                 document = application.Documents.Open(sourcePath);19                 document.SaveAs2();20                 document.ExportAsFixedFormat(targetPath, exportFormat);21                 result = true;22             }23             catch (Exception e)24             {25                 Console.WriteLine(e.Message);26                 result = false;27             }28             finally29             {30                 if (document != null)31                 {32                     document.Close();33                     document = 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         }
Word文件转换成为PDF格式文件
技术分享
 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             Microsoft.Office.Interop.Excel.XlFixedFormatType targetType = Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF;11             object missing = Type.Missing;12             Microsoft.Office.Interop.Excel.ApplicationClass application = null;13             Microsoft.Office.Interop.Excel.Workbook workBook = null;14             try15             {16                 application = new Microsoft.Office.Interop.Excel.ApplicationClass();17                 application.Visible = false;18                 workBook = application.Workbooks.Open(sourcePath);19                 workBook.SaveAs();20                 workBook.ExportAsFixedFormat(targetType, targetPath);21                 result = true;22             }23             catch (Exception e)24             {25                 Console.WriteLine(e.Message);26                 result = false;27             }28             finally29             {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         }
Excel文件转换成PDF格式文件
技术分享
 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             Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType targetFileType = Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsPDF;11             object missing = Type.Missing;12             Microsoft.Office.Interop.PowerPoint.ApplicationClass application = null;13             Microsoft.Office.Interop.PowerPoint.Presentation persentation = null;14             try15             {16                 application = new Microsoft.Office.Interop.PowerPoint.ApplicationClass();17                 //application.Visible = MsoTriState.msoFalse;  18                 persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);19                 persentation.SaveAs(targetPath, targetFileType, Microsoft.Office.Core.MsoTriState.msoTrue);20 21                 result = true;22             }23             catch (Exception e)24             {25                 Console.WriteLine(e.Message);26                 result = false;27             }28             finally29             {30                 if (persentation != null)31                 {32                     persentation.Close();33                     persentation = 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         }
PowerPoint文件转换成PDF格式文件
技术分享
 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             Microsoft.Office.Interop.Visio.VisFixedFormatTypes targetType = Microsoft.Office.Interop.Visio.VisFixedFormatTypes.visFixedFormatPDF;11             object missing = Type.Missing;12             Microsoft.Office.Interop.Visio.ApplicationClass application = null;13             Microsoft.Office.Interop.Visio.Document document = null;14             try15             {16                 application = new Microsoft.Office.Interop.Visio.ApplicationClass();17                 application.Visible = false;18                 document = application.Documents.Open(sourcePath);19                 document.Save();20                 document.ExportAsFixedFormat(targetType, targetPath, Microsoft.Office.Interop.Visio.VisDocExIntent.visDocExIntentScreen, Microsoft.Office.Interop.Visio.VisPrintOutRange.visPrintAll);21                 result = true;22             }23             catch (Exception e)24             {25                 Console.WriteLine(e.Message);26                 result = false;27             }28             finally29             {30                 if (application != null)31                 {32                     application.Quit();33                     application = null;34                 }35                 GC.Collect();36                 GC.WaitForPendingFinalizers();37                 GC.Collect();38                 GC.WaitForPendingFinalizers();39             }40             return result;41         }
Visio文件转换成PDF格式文件

 

Office文件转换PDF格式文件