昨天有一朋友让我帮忙找一款Word转PDF的软件,今天自己捣鼓出点成果封装个Helper供大家使用~

开源地址:https://github.com/dunitian/WordConvertPDF

软件下载:https://github.com/dunitian/WordConvertPDF/tree/master/Bin

封装了一个Helper类,供大家调用:

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
}
}

  

【源码】Word转PDF V1.0.1 小软件,供新手参考的更多相关文章

  1. 保姆级教程——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版)的亲身经历,包括遇到的问题及解决办法,也有一些 ...

  2. [笔记] Ubuntu 18.04源码编译安装OpenCV 4.0流程

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

  3. Android音乐播放器源码(歌词.均衡器.收藏.qq5.0菜单.通知)

    一款Android音乐播放器源码,基本功能都实现了 qq5.0菜单(歌词.均衡器.收藏.qq5.0菜单.通知) 只有向右滑动出现,菜单键和指定按钮都还没有添加. 源码下载:http://code.66 ...

  4. springmvc工作原理以及源码分析(基于spring3.1.0)

    springmvc是一个基于spring的web框架.本篇文章对它的工作原理以及源码进行深入分析. 一.springmvc请求处理流程 二.springmvc的工作机制 三.springmvc核心源码 ...

  5. CentOS 6.4 64位 源码编译hadoop 2.2.0

    搭建环境:Centos 6.4 64bit 1.安装JDK 参考这里2.安装mavenmaven官方下载地址,可以选择源码编码安装,这里就直接下载编译好的wget http://mirror.bit. ...

  6. 源码编译安装 PHP5.5.0,解决curl_exec访问HTTPS返回502错误的问题(修改PATH路径)

    最近碰到一个奇怪的问题, PHP使用 curl_exec 访问 HTTPS 网页时, 返回502错误, 访问HTTP网页时没有问题,  用   echo   phpinfo() ;  查看, 支持op ...

  7. Android -- 从源码带你从EventBus2.0飚到EventBus3.0(一)

    1,最近看了不少的面试题,不管是百度.网易.阿里的面试题,都会问到EventBus源码和RxJava源码,而自己只是在项目中使用过,却没有去用心的了解它底层是怎么实现的,所以今天就和大家一起来学习学习 ...

  8. 源码阅读之mongoengine(0)

    最近工作上用到了mongodb,之前只是草草了解了一下.对于NoSQL的了解也不是太多.所以想趁机多学习一下. 工作的项目直接用了pymongo来操作直接操作mongodb.对于用惯了Djongo O ...

  9. 【 js 基础 】【 源码学习 】 setTimeout(fn, 0) 的作用

    在 zepto 源码中,$.fn 对象 有个 ready 函数,其中有这样一句 setTimeout(fn,0); $.fn = { ready: function(callback){ // don ...

随机推荐

  1. [资料分享]7天搞定Node.js微信公众号开发

  2. BestCoder#49

    Untitled Accepts: 504 Submissions: 1542 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/6 ...

  3. 【转】《从入门到精通云服务器》第四讲—DDOS攻击

    上周咱们深入分析了云服务器的配置问题,好了,现在手上有了云服务器之后,我们又不得不提它:DDOS攻击.这是所有运维者的心头痛,也是任何公司听闻后都将心惊胆战的强大对手.下面我们将用浅显易懂的方式讲述什 ...

  4. Exchange超级实用命令行

    发现Powershell很强大以后,就欲罢不能了.来点干货

  5. bzoj3110树套树

    wa一片,最后一个T,终于心碎了... 为什么没人告诉我要开longlong 为什么所有人都说没有负数 #include<cstdio> #include<algorithm> ...

  6. hdu3549还是网络流

    最后一次训练模板(比较熟练了) 接下来训练网络流的建图 #include <cstdio> #define INF 2147483647 int n,m,ans,x,y,z,M,h,t,T ...

  7. 弱省互测#2 t2

    题意 给两个树,大小分别为n和m,现在两棵树各选一些点(包括1),使得这棵树以1号点为根同构(同构就是每个点的孩子数目相同),求最大的同构树.(n, m<=500) 分析 我们从两棵树中各取出一 ...

  8. android 开发 gradle 自己会容易混淆的东西

    使用intellij idea 开发android ,关于 gradle 和 android gradle plugin 容易混淆地方,做下记录: 一. build.gradle 文件有两个地方存在, ...

  9. 谷歌浏览器如何查看或获取Cookie字符串

    注:此博客仅供非web开发人员查看,以下内容都基于谷歌浏览器. 在网页空白处点击鼠标右键,在弹出菜单中选择[审查元素],可以看到网页下方出现审查元素相关界面. 在审查元素相关界面,点击[Network ...

  10. bootstrap之移动支持

    为了让 Bootstrap 开发的网站对移动设备友好,确保适当的绘制和触屏缩放,需要在网页的 head 之中添加 viewport meta 标签,如下所示: <meta name=" ...