c# word excel ppt 转pdf

office 2010 app

 

using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.Word;
using Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Core;
using Word = Microsoft.Office.Interop.Word;
using Excel = Microsoft.Office.Interop.Excel;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;ui

 

 1 //将word文档转换成PDF格式
 2         public bool DocConvert(string sourcePath, string targetPath, Word.WdExportFormat exportFormat = Word.WdExportFormat.wdExportFormatPDF)  3  {  4             bool result;  5             object paramMissing = Type.Missing;  6             Word.ApplicationClass wordApplication = new Word.ApplicationClass();  7             Word._Document wordDocument = null;  8             try
 9  {  10                 object paramSourceDocPath = sourcePath;  11                 string paramExportFilePath = targetPath;  12 
 13                 Word.WdExportFormat paramExportFormat = exportFormat;  14                 bool paramOpenAfterExport = false;  15                 Word.WdExportOptimizeFor paramExportOptimizeFor =
 16  Word.WdExportOptimizeFor.wdExportOptimizeForPrint;  17                 Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument;  18                 int paramStartPage = 0;  19                 int paramEndPage = 0;  20                 Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent;  21                 bool paramIncludeDocProps = true;  22                 bool paramKeepIRM = true;  23                 Word.WdExportCreateBookmarks paramCreateBookmarks =
 24  Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;  25                 bool paramDocStructureTags = true;  26                 bool paramBitmapMissingFonts = true;  27                 bool paramUseISO19005_1 = false;  28 
 29                 wordDocument = wordApplication.Documents.Open(  30                         ref paramSourceDocPath, ref paramMissing, ref paramMissing,  31                         ref paramMissing, ref paramMissing, ref paramMissing,  32                         ref paramMissing, ref paramMissing, ref paramMissing,  33                         ref paramMissing, ref paramMissing, ref paramMissing,  34                         ref paramMissing, ref paramMissing, ref paramMissing,  35                         ref paramMissing);  36 
 37                 if (wordDocument != null)  38  wordDocument.ExportAsFixedFormat(paramExportFilePath,  39  paramExportFormat, paramOpenAfterExport,  40  paramExportOptimizeFor, paramExportRange, paramStartPage,  41  paramEndPage, paramExportItem, paramIncludeDocProps,  42  paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,  43  paramBitmapMissingFonts, paramUseISO19005_1,  44                             ref paramMissing);  45                 result = true;  46  }  47             catch (Exception ex)  48  {  49                 log4net.ILog logger = log4net.LogManager.GetLogger(typeof(ConvertToPdf));  50                 logger.Error("DocConvert" + ex.Message);  51                 result = false;  52  }  53             finally
 54  {  55                 if (wordDocument != null)  56  {  57                     wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);  58                     wordDocument = null;  59  }  60                 if (wordApplication != null)  61  {  62                     wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);  63                     wordApplication = null;  64  }  65  GC.Collect();  66  GC.WaitForPendingFinalizers();  67  GC.Collect();  68  GC.WaitForPendingFinalizers();  69  }  70             return result;  71  }  72 
 73         //将excel文档转换成PDF格式
 74         public bool ExcelConvert(string sourcePath, string targetPath, XlFixedFormatType targetType = XlFixedFormatType.xlTypePDF)  75  {  76             bool result;  77             object missing = Type.Missing;  78             Excel.ApplicationClass application = null;  79             Workbook workBook = null;  80             try
 81  {  82                 application = new Excel.ApplicationClass();  83                 object target = targetPath;  84                 object type = targetType;  85                 workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,  86  missing, missing, missing, missing, missing, missing, missing, missing, missing);  87 
 88                 workBook.ExportAsFixedFormat(targetType, target, XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);  89                 result = true;  90  }  91             catch (Exception ex)  92  {  93                 log4net.ILog logger = log4net.LogManager.GetLogger(typeof(ConvertToPdf));  94                 logger.Error("ExcelConvert" + ex.Message);  95                 result = false;  96  }  97             finally
 98  {  99                 if (workBook != null) 100  { 101                     workBook.Close(true, missing, missing); 102                     workBook = null; 103  } 104                 if (application != null) 105  { 106  application.Quit(); 107                     application = null; 108  } 109  GC.Collect(); 110  GC.WaitForPendingFinalizers(); 111  GC.Collect(); 112  GC.WaitForPendingFinalizers(); 113  } 114             return result; 115  } 116 
117         //将ppt文档转换成PDF格式
118         public bool PPtConvert(string sourcePath, string targetPath, PpSaveAsFileType targetFileType = PpSaveAsFileType.ppSaveAsPDF) 119  { 120             bool result; 121             object missing = Type.Missing; 122             PowerPoint.ApplicationClass application = null; 123             Presentation persentation = null; 124             try
125  { 126                 application = new PowerPoint.ApplicationClass(); 127                 persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse); 128  persentation.SaveAs(targetPath, targetFileType, Microsoft.Office.Core.MsoTriState.msoTrue); 129 
130                 result = true; 131  } 132             catch (Exception ex) 133  { 134                 log4net.ILog logger = log4net.LogManager.GetLogger(typeof(ConvertToPdf)); 135                 logger.Error("PPtConvert" + ex.Message); 136                 result = false; 137  } 138             finally
139  { 140                 if (persentation != null) 141  { 142  persentation.Close(); 143                     persentation = null; 144  } 145                 if (application != null) 146  { 147  application.Quit(); 148                     application = null; 149  } 150  GC.Collect(); 151  GC.WaitForPendingFinalizers(); 152  GC.Collect(); 153  GC.WaitForPendingFinalizers(); 154  } 155             return result; 156         }