【源码】Word转PDF V1.0.1 小软件,供新手参考
昨天有一朋友让我帮忙找一款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 小软件,供新手参考的更多相关文章
- 保姆级教程——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版)的亲身经历,包括遇到的问题及解决办法,也有一些 ...
- [笔记] Ubuntu 18.04源码编译安装OpenCV 4.0流程
标准常规安装方法安装的OpenCV版本比较低,想尝鲜使用4.0版本,只好源码安装. 安装环境 OS:Ubuntu 18.04 64 bit 显卡:NVidia GTX 1080 CUDA:10.0 c ...
- Android音乐播放器源码(歌词.均衡器.收藏.qq5.0菜单.通知)
一款Android音乐播放器源码,基本功能都实现了 qq5.0菜单(歌词.均衡器.收藏.qq5.0菜单.通知) 只有向右滑动出现,菜单键和指定按钮都还没有添加. 源码下载:http://code.66 ...
- springmvc工作原理以及源码分析(基于spring3.1.0)
springmvc是一个基于spring的web框架.本篇文章对它的工作原理以及源码进行深入分析. 一.springmvc请求处理流程 二.springmvc的工作机制 三.springmvc核心源码 ...
- CentOS 6.4 64位 源码编译hadoop 2.2.0
搭建环境:Centos 6.4 64bit 1.安装JDK 参考这里2.安装mavenmaven官方下载地址,可以选择源码编码安装,这里就直接下载编译好的wget http://mirror.bit. ...
- 源码编译安装 PHP5.5.0,解决curl_exec访问HTTPS返回502错误的问题(修改PATH路径)
最近碰到一个奇怪的问题, PHP使用 curl_exec 访问 HTTPS 网页时, 返回502错误, 访问HTTP网页时没有问题, 用 echo phpinfo() ; 查看, 支持op ...
- Android -- 从源码带你从EventBus2.0飚到EventBus3.0(一)
1,最近看了不少的面试题,不管是百度.网易.阿里的面试题,都会问到EventBus源码和RxJava源码,而自己只是在项目中使用过,却没有去用心的了解它底层是怎么实现的,所以今天就和大家一起来学习学习 ...
- 源码阅读之mongoengine(0)
最近工作上用到了mongodb,之前只是草草了解了一下.对于NoSQL的了解也不是太多.所以想趁机多学习一下. 工作的项目直接用了pymongo来操作直接操作mongodb.对于用惯了Djongo O ...
- 【 js 基础 】【 源码学习 】 setTimeout(fn, 0) 的作用
在 zepto 源码中,$.fn 对象 有个 ready 函数,其中有这样一句 setTimeout(fn,0); $.fn = { ready: function(callback){ // don ...
随机推荐
- 配置gradle.properties
在一些项目中会分拆app 和 lib , 这时候引用support的时候,一旦更改版本会出现需要同步更改两个地方的问题.这种情况,可以通过配置gradle.properties实现替换. 在项目编译过 ...
- Python读取文件内容并将内容插入到SSDB中
import os import linecache import time from SSDB import SSDB ssdb = SSDB('127.0.0.1', 8888) print(&q ...
- 用VB实现SmartQQ机器人
这里为了便于介绍程序设计的流程,更多以代码形式给出,具体可用火狐浏览器的firebug插件来抓包分析,或者用谷歌浏览器的开发者工具进行抓包.抓包地址是:http://w.qq.com 第一步,是二维码 ...
- [Android] 多重使用Fragment 中的onFragmentInteraction
新建的一个Fragment,被一个Activity使用,那么这个Activity需要继承一个接口: public class MainActivity extends Activity impleme ...
- 查看sbt版本
进入 sbt 命令行模式, 键入sbtVersion 得到[info]0.13.12
- Ionic实战 自动升级APP(Android版)
Ionic 框架介绍 Ionic是一个基于Angularjs.可以使用HTML5构建混合移动应用的用户界面框架,它自称为是"本地与HTML5的结合".该框架提供了很多基本的移动用户 ...
- bzoj3631: [JLOI2014]松鼠的新家(LCA+差分)
题目大意:一棵树,以一定顺序走完n个点,求每个点经过多少遍 可以树链剖分,也可以直接在树上做差分序列的标记 后者打起来更舒适一点.. 具体实现: 先求x,y的lca,且dep[x]<dep[y] ...
- 奇怪的UnexpectedRollbackException异常
今天在使用一个原来常用的功能的时候,突然发现在某些场景下会报异常,内容如下: 通过断点调试发现一路都很顺畅,就是在从controller层返回前段的时候会报该异常,没办法,只能通过排除法定位问题,后来 ...
- php-(/usr/local/php)安装编译选项
./configure \ --prefix=/usr/local/php \ --with-config-file-path=/usr/local/php/etc \ --enable-fpm \ ...
- C++文本处理_文件读写
QT在进行文本读写时和C++一样,是基于文本流操作的. QT在读取全部文本时,相对比较便捷.使用readAll()函数,配合split()进行分隔符的拆分(例如行结束符"\n"), ...