c# word excel ppt 转pdf

office 2010 

 

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;

 

  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         }

 

转载于:https://www.cnblogs.com/dingshuaixing/p/10906675.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
项目使用VS2017打开,.net 2.0下运行。 项目使用的微软官方的插件方法,可以将doc, docx, xls, xlsx, ppt, pptx文件换为pdf文件,但是需要: 1、用户需要首先安装一个SaveAsPDFandXPS.exe的工具; 2、如果用户是xp系统,则: 2.1 如果用户安装的是office 2007,则用户在安装office 2007的时候必须要安装Visual Basic for Application 和 Microsoft Office Document Imaging这2个选项,否则换失败; 2.2 如果用户安装的是office 2010,则在安装office 2010时必须要安装Visual Basic for Application选项,然后从office 2007安装包里面安装Microsoft Office Document Imaging(因为2010删除了这个选项,好麻烦~),否则换失败; 2.3 xp不能安装office 2013/2016; 3、如果用户是win7系统,则: 3.1 如果用户安装的是office 2007,则用户在安装office 2007的时候必须要安装Visual Basic for Application 和 Microsoft Office Document Imaging这2个选项,否则换失败; 3.2 如果用户安装的是office 2010,则在安装office 2010时必须要安装Visual Basic for Application选项(win7 + office 2010不需要安装Microsoft Office Document Imaging) 3.3 如果用户安装的是office 2013或2016,则不需要额外选项; 4、如果用户是win10系统,则: 4.1 如果用户安装的是office 2007,则用户在安装office 2007的时候必须要安装Visual Basic for Application这个选项,(win10 + office 2007不需要安装Microsoft Office Document Imaging)否则换失败; 4.2 如果用户安装的是office 2010,则在安装office 2010时必须要安装Visual Basic for Application选项(win10 + office 2010不需要安装Microsoft Office Document Imaging) 4.3 如果用户安装的是office 2013或2016,则不需要额外选项; 5、如果用户安装了wps 2016或者wps 2019也可以正常换。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值