昨天有一朋友让我帮忙找一款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, truetruefalseref 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, truetruefalseref 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, truetruefalseref 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,如需转载请自行联系原作者

【源码】Word转PDF V1.0.1 小软件,供新手参考相关推荐

  1. 小程序源码:洗衣店v2.5.0微信小程序

    新增功能 1.订单状态 通知 2.管理员手机端增加会员充值功能 3.优化下单页面功能 4.增加管理员端 跑腿人员结账功能 5.增加管理员收银功能 6.增加套餐卡(月/季/年卡)包月功能 7.增加会员功 ...

  2. html答题赚钱源码,WTS在线答题系统 v1.0.0

    WTS在线答题系统为在线答题系统(在线考试),支持在线考试.在线练习等功能... 支持题型:单选题.多选题.填空题.问答题.判断题.附件题.材料题.视频题.音频题支持答题类型:手工配置 试卷答题.随机 ...

  3. 2023去水印小程序saas系统源码修复独立版v1.0.3+uniapp前端

  4. 2023 号卡推广管理系统PHP源码 有后台版v1.0

    前台页面演示 管理后台演示 最近在做号卡代理,前几天发布了一个只有前台页面的号卡产品推广页面,但是考虑到每次修改产品都要在源文件修改就抽空用PHP搞了个后台,非常简单哈,不过满足正常使用. 使用说明 ...

  5. 保姆级教程——Ubuntu16.04 Server下深度学习环境搭建:安装CUDA8.0,cuDNN6.0,Bazel0.5.4,源码编译安装TensorFlow1.4.0(GPU版)...

    写在前面 本文叙述了在Ubuntu16.04 Server下安装CUDA8.0,cuDNN6.0以及源码编译安装TensorFlow1.4.0(GPU版)的亲身经历,包括遇到的问题及解决办法,也有一些 ...

  6. clickhouse原理解析与开发实战 pdf_重识SSM,“超高频面试点+源码解析+实战PDF”,一次性干掉全拿走...

    重识SSM,"超高频面试点"+"源码解析"+"实战PDF",一次性干掉全拿走!! 01 超高频面试点知识篇 1.1 Spring超高频面试点 ...

  7. android6.0源码分析之Camera API2.0下的Preview(预览)流程分析

    1.Camera2 preview的应用层流程分析 preview流程都是从startPreview开始的,所以来看startPreview方法的代码: <code class="hl ...

  8. opencv4.0在linux下编译,Ubuntu 18.04源码编译安装OpenCV 4.0步骤

    Ubuntu 18.04下标准常规安装方法安装的OpenCV版本比较低,想尝鲜使用4.0版本,只好源码安装. 安装环境 OS:Ubuntu 18.04 64 bit 显卡:NVidia GTX 108 ...

  9. access驱动程序_Linux驱动程序学习二 (续) scull 源码在内核5.4.0上的编译调试

    <LINUX设备驱动程序>第三章提供了源码scull,但是由于我用的是5.4.0内核,书中的是2.6.10内核,内核发生了很大的变化,因此编译scull源码花费了不少时间,下面是编译调试记 ...

最新文章

  1. MS SQL入门基础:创建索引
  2. 使用Ansible批量部署SSH免密登录远程主机
  3. Python pip工具初步学习
  4. 避免重蹈欧美“超级电厂”覆辙 瑞星全力保障国内电力行业信息安全
  5. Liferay例子学习,如何部署简单的jsp portlet
  6. Selenium WebDriver- actionchians模拟鼠标悬停操作
  7. 电脑关闭计算机怎么重启计算机,教您电脑关机后总是重启怎么办
  8. fcntl函数(网络编程会用)
  9. GoEasy小程序即时通讯源码 v1.1.0基于GoEasy提供的websocket通讯服务
  10. 消息中间件的技术选型心得-RabbitMQ ActiveMQ和ZeroMQ
  11. QT每日一练day5:QLabel和按钮窗口打印功能
  12. 浅析ASP.NET应用Autofac获取页面服务
  13. html5表单提交json数据库,使用html5的FormData对象,通过 Ajax表单异步提交文件数据...
  14. oracle加载日记账直服务器,Oracle EBS GL 总账日记账打开报错此职责无可用函数
  15. 【自动化__GUI自动化】__java__模拟功能操作__鼠标
  16. 同济大学 线性代数 第六版 pdf_【课后习题答案】工程数学线性代数同济第六版+课后习题答案...
  17. tomcat 启动编码设置(UTF-8,乱码)
  18. QQ机器人实现RSS订阅(github项目)
  19. 【Halcon轮廓提取】
  20. C盘哪些文件可以删除?windows7瘦身攻略

热门文章

  1. 【撸码师读书笔记】 大型网站技术架构——核心原理与案例分析
  2. Android 个人学习笔记- 导入android项目,无法自动生成R文件的解决方法
  3. makefile讲义(1)——makefile基础(概述)
  4. Visual Studio 2017 15.7预览版发布
  5. iOS 开发_..和self...的区别以及使用
  6. 【管理心得之一】不要用“可有可无”的事,挑战他人对这件事的原则和底线...
  7. Redis与Memcached简要比较
  8. 会计的思考(41):会计的领悟—每一位业务骨干都是优秀的管理会计师
  9. Visual Studio 2008 和 .NET 3.5 发布了
  10. 理解Golang的Time结构