【源码】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
     }
}

  

 

本文转自毒逆天博客园博客,原文链接:http://www.cnblogs.com/dunitian/p/5042724.html,如需转载请自行联系原作者


相关文章
|
22天前
|
Java Apache Maven
将word文档转换成pdf文件方法
在Java中,将Word文档转换为PDF文件可采用多种方法:1) 使用Apache POI和iText库,适合处理基本转换需求;2) Aspose.Words for Java,提供更高级的功能和性能;3) 利用LibreOffice命令行工具,适用于需要开源解决方案的场景。每种方法都有其适用范围,可根据具体需求选择。
|
22天前
|
Java Apache Maven
Java将word文档转换成pdf文件的方法?
【10月更文挑战第13天】Java将word文档转换成pdf文件的方法?
109 1
|
3月前
|
Linux Python Windows
Python PDF文件转Word格式,只需要3秒(附打包)
Python PDF文件转Word格式,只需要3秒(附打包)
81 3
Python PDF文件转Word格式,只需要3秒(附打包)
|
3月前
|
Python
Python——将PPT和Word转为PDF文件
Python——将PPT和Word转为PDF文件
63 1
|
3月前
|
Python
还不会免费将PDF转为Word?你可以试试这3种工具!
还不会免费将PDF转为Word?你可以试试这3种工具!
|
3月前
|
XML 缓存 JSON
为什么浏览器中有些图片、PDF等文件点击后有些是预览,有些是下载
为什么浏览器中有些图片、PDF等文件点击后有些是预览,有些是下载
244 0
|
1月前
|
索引 Python
PDF文件页面提取操作小指南
PDF文件页面提取操作小指南
|
1月前
|
Python
Python对PDF文件页面的旋转和切割
Python对PDF文件页面的旋转和切割
|
1月前
|
计算机视觉 Python
Python操作PDF文件
Python操作PDF文件
|
1月前
|
JSON 数据格式
LangChain-20 Document Loader 文件加载 加载MD DOCX EXCEL PPT PDF HTML JSON 等多种文件格式 后续可通过FAISS向量化 增强检索
LangChain-20 Document Loader 文件加载 加载MD DOCX EXCEL PPT PDF HTML JSON 等多种文件格式 后续可通过FAISS向量化 增强检索
62 2
下一篇
无影云桌面