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

首先需要安装 SaveAsPDFandXPS.exe 插件

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

代码如下:

   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: }

转载于:https://www.cnblogs.com/ppinfo/archive/2013/02/21/2920472.html

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

  1. php word excel转pdf文件怎么打开,php office文件(word/excel/ppt)转pdf文件,pptpdf

    php office文件(word/excel/ppt)转pdf文件,pptpdf 把代码放到了github上,点击进入 前阶段有个项目用到了线上预览功能, 关于预览office文件实现核心就是,把o ...

  2. Office系列---将Office文件(Word、PPT、Excel)转换为PDF文件,提取Office文件(Word、PPT)中的所有图片

    将Office文件转换为PDF文件,提取Office文件中的所有图片 1.Office系列---将Office文件(Word.PPT.Excel)转换为PDF文件 1.1 基于Office实现的解决方 ...

  3. office文件转换为pdf文件

    office文件转换为pdf文件 首先安装openoffice,自行百度 导入jar包,自行百度 引入坐标: <!-- 转pdf配置 --><dependency><gr ...

  4. Vue 预览word,excel,ppt等office文档-内网访问(基于onlyoffice,后端返回文件流)

    Vue 预览word,excel等office 先看效果!! 需求背景:在前端页面中预览office文件且是内网访问,服务器不可访问外网的前提. 因此微软的接口就废掉了,因为他接口的条件是可以访问外网 ...

  5. Office 办公软件 word Excel PPT(3)

    Office 办公软件 word Excel PPT(3) Excel 2007中有很多函数,其中函数SUM(区域)的功能是什么? ■使用Excel 2007复制数据,则( ) A.不能把-个区域的数 ...

  6. vba 保存word里面的图片_笔记7 【office精华课】一套课程学会Word+Excel+PPT(一)【Word】(2020年第37周 周五)...

    [office精华课] <一套课程学会Word+Excel+PPT> 课程目录:(总时长合计:28:56:25) =================================== [ ...

  7. Win10安装了Office右键没有新建Word,excel,PPT等选项解决方法

    大多用户在新建一个Office文档的时候都会快速在桌面上右键新建一个Word或Excel.PPT等,而不是先打开软件去新建,但是用户反馈在安装了office之后,右键新建中并没有Word,excel, ...

  8. Word,Excel,PPT等Office文件Web浏览器在线预览

    博主联系方式   https://fizzz.blog.csdn.net/article/details/113049879 前两天接到一个需求:需要在线预览用户上传的Word,Excel,PPT文档 ...

  9. 【免费直播】零基础Office速通,助您走向Word/Excel/PPT高手之路

     1 关于本门课程 [课程费用] 免费 [讲师介绍]  老宋,MOS讲师,2010年获得WORD和EXCEL 2003版专家级认证,2018年获得OFFICE 2016版大师级认证,OFFICE多年实 ...

  10. Office 办公软件 word Excel PPT(2)

    Office 办公软件 word Excel PPT(2) 今天分析一下Excle表格 Excel是Microsoft Office系列办公软件中的一个电子表格软件.它的主要功能有建立表格.填充数据. ...

最新文章

  1. javascript eval函数解析json数据时为什加上圆括号eval((+data+))
  2. java短_Java中的最短代码和最低延迟
  3. jar k8s 自己的 部署_怎样部署K8S服务器
  4. 规则绝对公平时,社会财富的流向谁?
  5. 【CC2530入门教程-01】CC2530微控制器开发入门基础
  6. excel合并两列内容_办公教程:Excel如何快速将两列内容合并成一列
  7. (一)泛函分析(江泽坚)习题解答
  8. linux 下文件防篡改,Rsync+inotify实现文件防篡改
  9. Python爬虫进阶必备 | 极X助手加密算法分析
  10. 交换机口不够用能再加一个吗_PoE交换机常见6大问题,一文掌握
  11. git push 使用中遇到的Permission to xxx denied to xxx问题终极解决方案
  12. 五一 七曲山大庙烧香
  13. 大数据Hive其实一点都不难,从入坑到放弃?不存在的
  14. 华为交换机boot默认密码
  15. PHP 实现汇聚adapay调用微信支付
  16. 云主机Centos7下WordPress部署指南
  17. P6770 [USACO05MAR]Checking an Alibi 不在场的证明(spfa)
  18. 软键盘输入设计(C语言)
  19. 一对一直播系统源码,一对一直播系统开发
  20. 深入学习IP数据报发送过程

热门文章

  1. 遗传算法c语言 x^2,c语言遗传算法 解决 y=x2问题
  2. 开源项目推荐ruoyi
  3. 在thinkphp中引入自定义的敏感词库
  4. 在网站上点击按钮直接聊QQ
  5. ArcGIS中GWR模型的构建
  6. 在UOS(deepin)系统下安装CPUS-PDF虚拟PDF打印机的方法
  7. 关于flymcu烧录stm32芯片超时的问题解决
  8. 小程序如何添加外部字体库
  9. et文件怎么转成excel_10秒就能将任意格式的文件转成PDF,简单易操作,不学可别后悔...
  10. 各种数据结构优缺点分析