C# 调用office 2007 及 SaveAsPDFandXPS.exe 将Word、Excel、PPT转换为PDF文件

时间:2024-02-21 20:49:54

为了将Office文档转换为PDF文件,在网上找了些工具类,但转换效果不理想,采用office自带的功能能比较好的实现转换。

首先需要安装 SaveAsPDFandXPS.exe 插件

在工程中添加com引用,如下图

image

 

image

 

代码如下:

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Text;
   4:  
   5: using Microsoft.Office.Core;
   6: using Word = Microsoft.Office.Interop.Word;
   7: using Excel = Microsoft.Office.Interop.Excel;
   8: using PowerPoint = Microsoft.Office.Interop.PowerPoint;
   9:  
  10: using System.IO;
  11: using System.Runtime.InteropServices;
  12:  
  13: namespace OfficeToPdf
  14: {
  15:     [Guid("21010F24-2B12-415F-A349-3467EFF056FA")]
  16:     public interface IOfficeConvertToPdf
  17:     {
  18:         [DispId(1)]
  19:         bool WordConvertToPDF(string sourcePath, string targetPath);
  20:  
  21:         [DispId(2)]
  22:         bool ExcelConvertToPDF(string sourcePath, string targetPath);
  23:  
  24:         [DispId(3)]
  25:         bool PPTConvertToPDF(string sourcePath, string targetPath);
  26:     }
  27:  
  28:     [Guid("B02BC204-8CC4-4328-8731-6AB9BCC6E925"),ClassInterface(ClassInterfaceType.None)]
  29:     public class OfficeConvertToPdf:IOfficeConvertToPdf
  30:     {
  31:  
  32:         #region 实际方法
  33:         /// <summary>
  34:         /// 将Word转换为pdf
  35:         /// </summary>
  36:         /// <param name="sourcePath"></param>
  37:         /// <param name="targetPath"></param>
  38:         /// <returns></returns>
  39:         public  bool WordConvertToPDF(string sourcePath, string targetPath)
  40:         {
  41:             bool result = false;
  42:             Word.WdExportFormat exportFormat = Word.WdExportFormat.wdExportFormatPDF;
  43:             object paramMissing = Type.Missing;
  44:             Word.ApplicationClass wordApplication = new Word.ApplicationClass();
  45:             Word.Document wordDocument = null;
  46:             try
  47:             {
  48:                 object paramSourceDocPath = sourcePath;
  49:                 string paramExportFilePath = targetPath;
  50:  
  51:                 Word.WdExportFormat paramExportFormat = exportFormat;
  52:                 bool paramOpenAfterExport = false;
  53:                 Word.WdExportOptimizeFor paramExportOptimizeFor = Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
  54:                 Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument;
  55:                 int paramStartPage = 0;
  56:                 int paramEndPage = 0;
  57:                 Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent;
  58:                 bool paramIncludeDocProps = true;
  59:                 bool paramKeepIRM = true;
  60:                 Word.WdExportCreateBookmarks paramCreateBookmarks = Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;
  61:                 bool paramDocStructureTags = true;
  62:                 bool paramBitmapMissingFonts = true;
  63:                 bool paramUseISO19005_1 = false;
  64:  
  65:                 wordDocument = wordApplication.Documents.Open(
  66:                                 ref paramSourceDocPath, ref paramMissing, ref paramMissing,
  67:                                 ref paramMissing, ref paramMissing, ref paramMissing,
  68:                                 ref paramMissing, ref paramMissing, ref paramMissing,
  69:                                 ref paramMissing, ref paramMissing, ref paramMissing,
  70:                                 ref paramMissing, ref paramMissing, ref paramMissing,
  71:                                 ref paramMissing);
  72:  
  73:                 if (wordDocument != null)
  74:                     wordDocument.ExportAsFixedFormat(paramExportFilePath,
  75:                     paramExportFormat, paramOpenAfterExport,
  76:                     paramExportOptimizeFor, paramExportRange, paramStartPage,
  77:                     paramEndPage, paramExportItem, paramIncludeDocProps,
  78:                     paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
  79:                     paramBitmapMissingFonts, paramUseISO19005_1,
  80:                     ref paramMissing);
  81:                 result = true;
  82:             }
  83:             catch
  84:             {
  85:                 result = false;
  86:             }
  87:             finally
  88:             {
  89:                 if (wordDocument != null)
  90:                 {
  91:                     wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);
  92:                     wordDocument = null;
  93:                 }
  94:                 if (wordApplication != null)
  95:                 {
  96:                     wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
  97:                     wordApplication = null;
  98:                 }
  99:                 GC.Collect();
 100:                 GC.WaitForPendingFinalizers();
 101:                 GC.Collect();
 102:                 GC.WaitForPendingFinalizers();
 103:             }
 104:             return result;
 105:         }
 106:  
 107:         /// <summary>
 108:         /// 将Excel 转换为pdf
 109:         /// </summary>
 110:         /// <param name="sourcePath"></param>
 111:         /// <param name="targetPath"></param>
 112:         /// <returns></returns>
 113:         public  bool ExcelConvertToPDF(string sourcePath, string targetPath)
 114:         {
 115:             bool result = false;
 116:             Excel.XlFixedFormatType targetType = Excel.XlFixedFormatType.xlTypePDF;
 117:             object missing = Type.Missing;
 118:             Excel.ApplicationClass application = null;
 119:             Excel.Workbook workBook = null;
 120:             try
 121:             {
 122:                 application = new Excel.ApplicationClass();
 123:                 object target = targetPath;
 124:                 object type = targetType;
 125:                 workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
 126:  
 127:                 workBook.ExportAsFixedFormat(targetType, target, Excel.XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
 128:                 result = true;
 129:             }
 130:             catch
 131:             {
 132:                 result = false;
 133:             }
 134:             finally
 135:             {
 136:                 if (workBook != null)
 137:                 {
 138:                     workBook.Close(true, missing, missing);
 139:                     workBook = null;
 140:                 }
 141:                 if (application != null)
 142:                 {
 143:                     application.Quit();
 144:                     application = null;
 145:                 }
 146:                 GC.Collect();
 147:                 GC.WaitForPendingFinalizers();
 148:                 GC.Collect();
 149:                 GC.WaitForPendingFinalizers();
 150:             }
 151:             return result;
 152:         }
 153:  
 154:         /// <summary>
 155:         /// 将ppt转换为pdf
 156:         /// </summary>
 157:         /// <param name="sourcePath"></param>
 158:         /// <param name="targetPath"></param>
 159:         /// <returns></returns>
 160:         public  bool PPTConvertToPDF(string sourcePath, string targetPath)
 161:         {
 162:             bool result;
 163:             PowerPoint.PpSaveAsFileType targetFileType = PowerPoint.PpSaveAsFileType.ppSaveAsPDF;
 164:             object missing = Type.Missing;
 165:             PowerPoint.ApplicationClass application = null;
 166:             PowerPoint.Presentation persentation = null;
 167:             try
 168:             {
 169:                 application = new PowerPoint.ApplicationClass();
 170:                 persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
 171:                 persentation.SaveAs(targetPath, targetFileType, Microsoft.Office.Core.MsoTriState.msoTrue);
 172:  
 173:                 result = true;
 174:             }
 175:             catch
 176:             {
 177:                 result = false;
 178:             }
 179:             finally
 180:             {
 181:                 if (persentation != null)
 182:                 {
 183:                     persentation.Close();
 184:                     persentation = null;
 185:                 }
 186:                 if (application != null)
 187:                 {
 188:                     application.Quit();
 189:                     application = null;
 190:                 }
 191:                 GC.Collect();
 192:                 GC.WaitForPendingFinalizers();
 193:                 GC.Collect();
 194:                 GC.WaitForPendingFinalizers();
 195:             }
 196:             return result;
 197:         }
 198:  
 199:         #endregion
 200:     }
 201:  
 202:  
 203: }