【源码】Word转PDF V1.0.1 小软件,供新手参考
昨天有一朋友让我帮忙找一款Word转PDF的软件,今天自己捣鼓出点成果封装个Helper供大家使用~
开源地址:https://github.com/dunitian/WordConvertPDF
软件下载:https://github.com/dunitian/WordConvertPDF/tree/master/Bin
封装了一个Helper类,供大家调用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Office.Interop.Word; using System.IO; namespace WordConvertPDF { public static class WordToPDFHelper { /// <summary> /// Word转换成PDF(单个文件转换推荐使用) /// </summary> /// <param name="inputPath">载入完整路径</param> /// <param name="outputPath">保存完整路径</param> /// <param name="startPage">初始页码(默认为第一页[0])</param> /// <param name="endPage">结束页码(默认为最后一页)</param> public static bool WordToPDF( string inputPath, string outputPath, int startPage = 0, int endPage = 0) { bool b = true ; #region 初始化 //初始化一个application Application wordApplication = new Application(); //初始化一个document Document wordDocument = null ; #endregion #region 参数设置~~我去累死宝宝了~~(所谓的参数都是根据这个方法来的:ExportAsFixedFormat) //word路径 object wordPath = Path.GetFullPath(inputPath); //输出路径 string pdfPath = Path.GetFullPath(outputPath); //导出格式为PDF WdExportFormat wdExportFormat = WdExportFormat.wdExportFormatPDF; //导出大文件 WdExportOptimizeFor wdExportOptimizeFor = WdExportOptimizeFor.wdExportOptimizeForPrint; //导出整个文档 WdExportRange wdExportRange = WdExportRange.wdExportAllDocument; //开始页码 int startIndex = startPage; //结束页码 int endIndex = endPage; //导出不带标记的文档(这个可以改) WdExportItem wdExportItem = WdExportItem.wdExportDocumentContent; //包含word属性 bool includeDocProps = true ; //导出书签 WdExportCreateBookmarks paramCreateBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks; //默认值 object paramMissing = Type.Missing; #endregion #region 转换 try { //打开word wordDocument = wordApplication.Documents.Open( ref wordPath, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing); //转换成指定格式 if (wordDocument != null ) { wordDocument.ExportAsFixedFormat(pdfPath, wdExportFormat, false , wdExportOptimizeFor, wdExportRange, startIndex, endIndex, wdExportItem, includeDocProps, true , paramCreateBookmarks, true , true , false , ref paramMissing); } } catch (Exception ex) { b = false ; } finally { //关闭 if (wordDocument != null ) { wordDocument.Close( ref paramMissing, ref paramMissing, ref paramMissing); wordDocument = null ; } //退出 if (wordApplication != null ) { wordApplication.Quit( ref paramMissing, ref paramMissing, ref paramMissing); wordApplication = null ; } } return b; #endregion } /// <summary> /// Word转换成PDF(批量文件转换推荐使用) /// </summary> /// <param name="inputPath">文件完整路径</param> /// <param name="outputPath">保存路径</param> public static int WordsToPDFs( string [] inputPaths, string outputPath) { int count = 0; #region 初始化 //初始化一个application Application wordApplication = new Application(); //初始化一个document Document wordDocument = null ; #endregion //默认值 object paramMissing = Type.Missing; for ( int i = 0; i < inputPaths.Length; i++) { #region 参数设置~~我去累死宝宝了~~(所谓的参数都是根据这个方法来的:ExportAsFixedFormat) //word路径 object wordPath = Path.GetFullPath(inputPaths[i]); //获取文件名 string outputName = Path.GetFileNameWithoutExtension(inputPaths[i]); //输出路径 string pdfPath = Path.GetFullPath(outputPath + @"\" + outputName + ".pdf"); //导出格式为PDF WdExportFormat wdExportFormat = WdExportFormat.wdExportFormatPDF; //导出大文件 WdExportOptimizeFor wdExportOptimizeFor = WdExportOptimizeFor.wdExportOptimizeForPrint; //导出整个文档 WdExportRange wdExportRange = WdExportRange.wdExportAllDocument; //开始页码 int startIndex = 0; //结束页码 int endIndex = 0; //导出不带标记的文档(这个可以改) WdExportItem wdExportItem = WdExportItem.wdExportDocumentContent; //包含word属性 bool includeDocProps = true ; //导出书签 WdExportCreateBookmarks paramCreateBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks; #endregion #region 转换 try { //打开word wordDocument = wordApplication.Documents.Open( ref wordPath, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing); //转换成指定格式 if (wordDocument != null ) { wordDocument.ExportAsFixedFormat(pdfPath, wdExportFormat, false , wdExportOptimizeFor, wdExportRange, startIndex, endIndex, wdExportItem, includeDocProps, true , paramCreateBookmarks, true , true , false , ref paramMissing); } count++; } catch (Exception ex) { } finally { //关闭 if (wordDocument != null ) { wordDocument.Close( ref paramMissing, ref paramMissing, ref paramMissing); wordDocument = null ; } } } //退出 if (wordApplication != null ) { wordApplication.Quit( ref paramMissing, ref paramMissing, ref paramMissing); wordApplication = null ; } return count; #endregion } #region 其他 /// <summary> /// Word转换成PDF(带日记) /// </summary> /// <param name="inputPath">载入完整路径</param> /// <param name="outputPath">保存完整路径</param> /// <param name="log">转换日记</param> /// <param name="startPage">初始页码(默认为第一页[0])</param> /// <param name="endPage">结束页码(默认为最后一页)</param> public static void WordToPDFCreateLog( string inputPath, string outputPath, out string log, int startPage = 0, int endPage = 0) { log = "success" ; #region 初始化 //初始化一个application Application wordApplication = new Application(); //初始化一个document Document wordDocument = null ; #endregion #region 参数设置~~我去累死宝宝了~~ //word路径 object wordPath = Path.GetFullPath(inputPath); //输出路径 string pdfPath = Path.GetFullPath(outputPath); //导出格式为PDF WdExportFormat wdExportFormat = WdExportFormat.wdExportFormatPDF; //导出大文件 WdExportOptimizeFor wdExportOptimizeFor = WdExportOptimizeFor.wdExportOptimizeForPrint; //导出整个文档 WdExportRange wdExportRange = WdExportRange.wdExportAllDocument; //开始页码 int startIndex = startPage; //结束页码 int endIndex = endPage; //导出不带标记的文档(这个可以改) WdExportItem wdExportItem = WdExportItem.wdExportDocumentContent; //包含word属性 bool includeDocProps = true ; //导出书签 WdExportCreateBookmarks paramCreateBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks; //默认值 object paramMissing = Type.Missing; #endregion #region 转换 try { //打开word wordDocument = wordApplication.Documents.Open( ref wordPath, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing); //转换成指定格式 if (wordDocument != null ) { wordDocument.ExportAsFixedFormat(pdfPath, wdExportFormat, false , wdExportOptimizeFor, wdExportRange, startIndex, endIndex, wdExportItem, includeDocProps, true , paramCreateBookmarks, true , true , false , ref paramMissing); } } catch (Exception ex) { if (ex != null ) { log = ex.ToString(); } } finally { //关闭 if (wordDocument != null ) { wordDocument.Close( ref paramMissing, ref paramMissing, ref paramMissing); wordDocument = null ; } //退出 if (wordApplication != null ) { wordApplication.Quit( ref paramMissing, ref paramMissing, ref paramMissing); wordApplication = null ; } GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } #endregion } #endregion } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异