word、excel、ppt转换成pdf,调用office的API,原理是调用的office的另存为pdf的命令,将文件保存为pdf格式的文件。
office必须是2007版本,office另存为pdf必须安装一个第三方的插件,excel转换成pdf要开启打印服务,下载地址SaveAsPDFandXPS.exe。

1 using System; 2 using System.Data; 3 using System.Configuration; 4 using System.Web; 5 using System.Web.Security; 6 using System.Web.UI; 7 using System.Web.UI.WebControls; 8 using System.Web.UI.WebControls.WebParts; 9 using System.Web.UI.HtmlControls; 10 11 using System.IO; 12 using Word = Microsoft.Office.Interop.Word; 13 using Excel = Microsoft.Office.Interop.Excel; 14 using PowerPoint = Microsoft.Office.Interop.PowerPoint; 15 using Microsoft.Office.Core; 16 17 18 public class Office2Pdf 19 { 20 public Office2Pdf() 21 { 22 23 } 24 25 26 public static bool WordToPDF(string sourcePath) 27 { 28 string targetPath = sourcePath.ToLower().Replace( 29 Path.GetExtension(sourcePath).ToLower(), ".pdf"); 30 bool result = false; 31 Word.WdExportFormat exportFormat = Word.WdExportFormat.wdExportFormatPDF; 32 object paramMissing = Type.Missing; 33 Word.ApplicationClass wordApplication = new Word.ApplicationClass(); 34 Word.Document wordDocument = null; 35 try 36 { 37 object paramSourceDocPath = sourcePath; 38 string paramExportFilePath = targetPath; 39 Word.WdExportFormat paramExportFormat = exportFormat; 40 bool paramOpenAfterExport = false; 41 Word.WdExportOptimizeFor paramExportOptimizeFor = Word.WdExportOptimizeFor.wdExportOptimizeForPrint; 42 Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument; 43 int paramStartPage = 0; 44 int paramEndPage = 0; 45 Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent; 46 bool paramIncludeDocProps = true; 47 bool paramKeepIRM = true; 48 Word.WdExportCreateBookmarks paramCreateBookmarks = Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks; 49 bool paramDocStructureTags = true; 50 bool paramBitmapMissingFonts = true; 51 bool paramUseISO19005_1 = false; 52 wordDocument = wordApplication.Documents.Open( 53 ref paramSourceDocPath, ref paramMissing, ref paramMissing, 54 ref paramMissing, ref paramMissing, ref paramMissing, 55 ref paramMissing, ref paramMissing, ref paramMissing, 56 ref paramMissing, ref paramMissing, ref paramMissing, 57 ref paramMissing, ref paramMissing, ref paramMissing, 58 ref paramMissing); 59 if (wordDocument != null) 60 wordDocument.ExportAsFixedFormat(paramExportFilePath, 61 paramExportFormat, paramOpenAfterExport, 62 paramExportOptimizeFor, paramExportRange, paramStartPage, 63 paramEndPage, paramExportItem, paramIncludeDocProps, 64 paramKeepIRM, paramCreateBookmarks, paramDocStructureTags, 65 paramBitmapMissingFonts, paramUseISO19005_1, 66 ref paramMissing); 67 result = true; 68 } 69 catch 70 { 71 result = false; 72 } 73 finally 74 { 75 if (wordDocument != null) 76 { 77 wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing); 78 wordDocument = null; 79 } 80 if (wordApplication != null) 81 { 82 wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing); 83 wordApplication = null; 84 } 85 GC.Collect(); 86 GC.WaitForPendingFinalizers(); 87 GC.Collect(); 88 GC.WaitForPendingFinalizers(); 89 } 90 return result; 91 } 92 93 public static bool ExcelToPDF(string sourcePath) 94 { 95 string targetPath = sourcePath.ToLower().Replace( 96 Path.GetExtension(sourcePath).ToLower(), ".pdf"); 97 bool result = false; 98 Excel.XlFixedFormatType targetType = Excel.XlFixedFormatType.xlTypePDF; 99 object missing = Type.Missing; 100 Excel.ApplicationClass application = null; 101 Excel.Workbook workBook = null; 102 try 103 { 104 application = new Excel.ApplicationClass(); 105 object target = targetPath; 106 object type = targetType; 107 workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing, 108 missing, missing, missing, missing, missing, missing, missing, missing, missing); 109 workBook.ExportAsFixedFormat(targetType, target, Excel.XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing); 110 result = true; 111 } 112 catch 113 { 114 result = false; 115 } 116 finally 117 { 118 if (workBook != null) 119 { 120 workBook.Close(true, missing, missing); 121 workBook = null; 122 } 123 if (application != null) 124 { 125 application.Quit(); 126 application = null; 127 } 128 GC.Collect(); 129 GC.WaitForPendingFinalizers(); 130 GC.Collect(); 131 GC.WaitForPendingFinalizers(); 132 } 133 return result; 134 } 135 ///<summary> 136 /// 把PowerPoint文件转换成PDF格式文件 137 ///</summary> 138 ///<param name="sourcePath">源文件路径</param> 139 ///<param name="targetPath">目标文件路径</param> 140 ///<returns>true=转换成功</returns> 141 public static bool PPTToPDF(string sourcePath) 142 { 143 string targetPath = sourcePath.ToLower().Replace( 144 Path.GetExtension(sourcePath).ToLower(), ".pdf"); 145 bool result; 146 PowerPoint.PpSaveAsFileType targetFileType = PowerPoint.PpSaveAsFileType.ppSaveAsPDF; 147 object missing = Type.Missing; 148 PowerPoint.ApplicationClass application = null; 149 PowerPoint.Presentation persentation = null; 150 try 151 { 152 application = new PowerPoint.ApplicationClass(); 153 persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse); 154 persentation.SaveAs(targetPath, targetFileType, MsoTriState.msoTrue); 155 result = true; 156 } 157 catch 158 { 159 result = false; 160 } 161 finally 162 { 163 if (persentation != null) 164 { 165 persentation.Close(); 166 persentation = null; 167 } 168 if (application != null) 169 { 170 application.Quit(); 171 application = null; 172 } 173 GC.Collect(); 174 GC.WaitForPendingFinalizers(); 175 GC.Collect(); 176 GC.WaitForPendingFinalizers(); 177 } 178 return result; 179 } 180 }
程序员的基础教程:菜鸟程序员
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现